Skip to content

Latest commit

 

History

History
355 lines (284 loc) · 7.58 KB

README.md

File metadata and controls

355 lines (284 loc) · 7.58 KB
CJSON Logo

Coded Javascript Object Notation


For JAVA


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.


Java Tag
Test Status

Dependency

<!-- 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>

Examples

Importing a JSON file in CJSON file

file.cjson

{
    "source": $import "path/to/source.json",
    "target": {
        "fruit": "Apple",
        "size": "Large",
        "color": "Red"
    }
}

Code

    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);
        }
    }

Output:

{
    "source": {
        // source.json content
    },
    "target": {
        "fruit": "Apple",
        "size": "Large",
        "color": "Red"
    }
}

Calling relative keys using JPATH

Below example shows color variable is calling data from fruit variable

file.cjson

{
    "target": {
        "fruit": "Orange",
        "size": "Medium",
        "color": $.target.fruit
    }
}

Code

    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);
        }
    }

Output

{
    "target": {
        "fruit": "Orange",
        "size": "Medium",
        "color": "Orange"
    }
}

Variable Injection

file.cjson

{
    "target": {
        "fruit": "Orange",
        "size": "Medium",
        "color": "Orangle",
        "sellerId": <id>
    }
}

Code

    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 value

{
    "target": {
        "fruit": "Orange",
        "size": "Medium",
        "color": "Orangle",
        "sellerId": "ID01"
    }
}

Single/ Multiple line comments

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
}

Removing key

Removing using jpath key pair

file.cjson
{
    "target": {
        "fruit": "Orange",
        "size": "Medium",
        "color": "Orangle"
    }
}
Code
    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");
        }
    }
Output
{
    "target": {
        "size": "Medium",
        "color": "Orangle"
    }
}

Removing using list of jpath keys pair

file.cjson
{
    "target": {
        "fruit": "Orange",
        "size": "Medium",
        "color": "Orangle"
    }
}
Code
    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);
        }
    }
Output
{
    "target": {
        "color": "Orangle"
    }
}

Deserializing CJSON string content

Any import path used must be absolute. Otherwise, you will receive AbsolutePathConstraintError exception

Code

    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);
        }
    }

Convert JAVA Object to JSON String

Code

    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);
        }
    }

Output

{
  "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
      }
    }
  }
}