CJSON is a data file format(inspired from JSON), but supports logical expressions too. Having extended language support to NodeJS, Python and Java, users has experienced data reusability. For features and examples, please refer to this documentation as base document.
<!-- https://mvnrepository.com/artifact/io.github.subhendushekhar.cjson/cjson -->
<dependency>
<groupId>io.github.subhendushekhar.cjson</groupId>
<artifactId>cjson</artifactId>
<version>x.x.x</version>
</dependency>
{
"source": $import "path/to/source.json",
"target": {
"fruit": "Apple",
"size": "Large",
"color": "Red"
}
}
import com.codedjson.CJson;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws Exception {
CJson<SerializableClass> cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
SerializableClass target = cJson.deserialize(SerializableClass.class);
}
}
{
"source": {
// source.json content
},
"target": {
"fruit": "Apple",
"size": "Large",
"color": "Red"
}
}
Below example shows color
variable is calling data from fruit
variable
{
"target": {
"fruit": "Orange",
"size": "Medium",
"color": $.target.fruit
}
}
import com.codedjson.CJson;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws Exception {
CJson<SerializableClass> cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
SerializableClass target = cJson.deserialize(SerializableClass.class);
}
}
{
"target": {
"fruit": "Orange",
"size": "Medium",
"color": "Orange"
}
}
{
"target": {
"fruit": "Orange",
"size": "Medium",
"color": "Orangle",
"sellerId": <id>
}
}
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<String, String> data = new HashMap<String, String>();
data.put("id", "ID01");
CJson<SerializableClass> cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
SerializableClass target = cJson.inject(SerializableClass.class, data);
}
}
{
"target": {
"fruit": "Orange",
"size": "Medium",
"color": "Orangle",
"sellerId": "ID01"
}
}
For single line comments, use //
For multi line comments, use like below:
// This is first line comment
// This is the second one
{
"name": "Amrut" // This is not allowed
}
{
"target": {
"fruit": "Orange",
"size": "Medium",
"color": "Orangle"
}
}
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<SerializableClass> cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
cJson.deserialize(SerializableClass.class);
SerializableClass value = cJson.remove("$.target.fruit");
}
}
{
"target": {
"size": "Medium",
"color": "Orangle"
}
}
{
"target": {
"fruit": "Orange",
"size": "Medium",
"color": "Orangle"
}
}
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 {
CJson<SerializableClass> cJson = new CJson<>(new Paths("file/path/to/file.cjson"));
cJson.deserialize(SerializableClass.class);
List<String> li = Arrays.asList("$.target.fruit", "$.target.size");
SerializableClass value = cJson.remove(li);
}
}
{
"target": {
"color": "Orangle"
}
}
Any import path used must be absolute. Otherwise, you will receive AbsolutePathConstraintError
exception
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 {
String cjsonCotent = "{\n" +
" \"source\": $import \"" + pureJsonfilePath.toString() + "\",\n" +
" \"target\": {\n" +
" \"fruit\": \"Apple\",\n" +
" \"size\": \"Large\",\n" +
" \"color\": \"Red\"\n" +
" }\n" +
"}";
CJson<SerializableClass> cJson = new CJson<>(cjsonCotent);
SerializableClass target = cJson.deserialize(SerializableClass.class);
}
}
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<String, Questions> 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);
}
}
{
"source":{
"quiz":{
"sport":{
"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
}
}
}
}