Features

CJSON format features

Import Statements

One of the strong feature which we have introduced is importing CJson/Json files in another CJson file. It works in the similar way, how we import libraries in programming languages. All you need to import it using $import "path/to/target/file" and deserialize the file.
{
    "source": $import "./source.json",
    "target": {
        "fruit": "apple",
        "size": "Large",
        "color": "Red",
        "quantity": 1
    }
}
                        

Relative Variable Referencing

Unlike XPATH for XML files, we have JPATH for JSON files. But as the drawback of plain data files, we cannot refer any variable inside a json object to another variable. This feature is useful when you have to duplicate the json key, but the value will be pulled from another variable which is already present in the json file. You can also refer to a variable which will be loaded after importing the file.
{
    "target": {
        "fruit": "Apple",
        "size": "Large",
        "color": "Red",
        "secColor": $.target.color,
        "colorList": [ $.target.color, $.target.secColor ],
        "digitCheck": 1.5,
        "digitImport": $.target.digitCheck,
        "digitArrayImport": [ $.target.digitCheck, $.target.digitImport ]
    }
}
                        

Dynamic Variable Injection

You can inject a variable dynamically also. Instead of replacing a variable value by parsing as `gson` object, put a key in format
<keyToBeReplaced> like below:
{
    "idValue": <id>
}
Now create a HashMap with key as <id> and store relevant value in it. While invoking <inject> function, pass the HashMap as the second parameter.
{
    "target": {
        "types": "asd",
        "fruit": ,
        "quantity": ,
        "size": "Large",
        "fruitType": $.target.fruit,
        "color": "Red",
        "secColor": $.target.color,
        "colorList": [ $.target.color, $.target.secColor ],
        "digitCheck": 1.5,
        "digitImport": $.target.digitCheck,
        "digitArrayImport": [ $.target.digitCheck, $.target.digitImport ]
    },
    "jsonInjection": 
}
                        

Adding Comments

CJSON also supports commented lines by adding // at the start of the line. Only single line comments are supported with the latest version. We will update once we deploy multi-line comments.
{
    //  This is multi line comment.
    // And this should also be ignored
    "sport": {
        // This is single line comment
        "q1": {
            "question": "Which one is correct team name in NBA?",
            "options": [
                "New York Bulls",
                "Los Angeles Kings",
                "Golden State Warriros",
                "Huston Rocket"
            ],
            "answer": "Huston Rocket"
        },
        "q2": null
    }
}
                        

CJSON Utility Features

Deserializing CJSON/JSON string content

Parsing of CJSON string content is also possible now. You can create CJSON object with a second parameter(specific to language). For language specific details, refer below.
import com.codedjson.CJson;
import java.util.HashMap;
import java.nio.file.Paths;
                        
public class Main {
    public static void main(String[] args) throws Exception {
                                    
        HashMap data = new HashMap();
        data.put("id", "ID01");
                                
        CJson cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
        SerializableClass target = cJson.inject(SerializableClass.class, data);
                        
    }
}                            
                        

Remove a variable

Any JSON key value pair can be removed by providing its JPath. Please Note, if you use remove before deserialize you will receive UndeserializedCJSON exception. This is because, unless the content is deserialized, CJSON engine has not processed the CJSON content. This function returns deserialized class object post removal.
import com.codedjson.CJson;
import java.util.HashMap;
import java.nio.file.Paths;
                        
public class Main {
    public static void main(String[] args) throws Exception {
        CJson cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
        cJson.deserialize(SerializableClass.class);
        SerializableClass value = cJson.remove("$.target.fruit");
    }
}
                        

Convert class object to JSON string

Any Java object can be converted to json string by using `toString()` function. It accepts pure java object and returns JSON in string.
import com.codedjson.CJson;

import java.util.Arrays;
import java.util.HashMap;
import java.nio.file.Paths;
import java.util.List;
                            
public class Main {
    public static void main(String[] args) throws Exception {
        Target target = new Target();
        target.source = new Pure();
        target.source.quiz = new HashMap<>();
                        
        HashMap questionsHashMap = new HashMap<>();
        Questions questions = new Questions();
        questions.question = "Which one is correct team name in NBA?";
        questions.options = Arrays.asList("New York Bulls",
                "Los Angeles Kings",
                "Golden State Warriros",
                "Huston Rocket");
        questions.answer = "Huston Rocket";
                        
        questionsHashMap.put("q1", questions);
        target.source.quiz.put("sport", questionsHashMap);
                        
        String targetString = CJson.toString(target);
    }
}                            
                        

Parse data

This function finds the value of the provided JPath as parameter. This function returns Object type data. So a type cast will be required to unlock native functions.
import com.codedjson.CJson;
import java.nio.file.Paths;
                        
public class Main {
    public static void main(String[] args) throws Exception {
                                    
        CJson cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
        SerializableClass target = cJson.deserialize(SerializableClass.class);
        Object variable = cJson.parse("$.path.to.variable");
    }
}
                        

Validate JSON

isContentJSON() does this activity for you. If you want to validate given content as JSON, you cna use this check.
This function returns boolean
import com.codedjson.CJson;
                        
public class Main {
    public static void main(String[] args) throws Exception {
                                    
        String testStr = "{" +
            \"hello\": \"world\" +
            \"name\": \"test\" +
        "}";

        boolean result = CJson.isContentJson(testStr);      // value is true

        // Testing CJson content
        testStr = "{" +
            \"hello\": \"world\" +
            \"data\": $import \"./datafile.json\" +
        "}";

        result = CJson.isContentJson(testStr);      // value is false
    }
}
                        

Writing JPath

Unline XPath in xml format, JSON has JPath format. THe difference is, instead of // or /, JPath starts with $.
Below is an example:
$.path.to.variables
If any array is encountered, below is the way index is provided:
$.path.to.array[index].variable