SubhenduShekhar / cjson

CJSON is a data file format(inspired from JSON), but supports logical expressions too. Having extended language support to NodeJS, Python, Java and DotNet, users has experienced data reusability. For features and examples, please refer to this documentation as base document.
https://subhendushekhar.github.io/cjson/
MIT License
3 stars 1 forks source link
cjson coded-json codedjson json
CJSON Logo

Coded Javascript Object Notation


CJSON is a data file format(inspired from JSON), but supports logical expressions too. Having extended language support to NodeJS, Python, Java and DotNet, users has experienced data reusability. For features and examples, please refer to this documentation as base document.


Java TagDotNet TagNPM TagPython Tag
Python Status Java Status NPM Status DotNet Status




[!TIP] Please install CJSON plugin for language support in visual studio code




Content


Features (v2.0.0 or higher)

CJSON format Features

Import multiple JSON files

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.

Referencing another key

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.

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.

Injection can be performed single or bulk. Lets look at each one below:

Single injection

Single injection can be performed by providing key and value to the inject().

inject(String key, Object value)

This function returns deserialized class object.

Bulk injection

Bulk injection can be performed by storing the injection data in Dictionary or HashMap where key is the key where injection need to be performed and value is the value.

inject(HashMap<String, Object>)

This function returns deserialized class object.

Single/ Multiple line comments

CJSON also supports commented lines by adding // at the start of the line.

Please note, inline comments are not supported. Contributers create an issue here

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.

Deserialize as class object

deserialize is a function where CJSON content is compiled and converted into equivalent JSON content. Now, users can consume this content as a deserailized class object like any other parser works.

Refer to example for language specific syntax.

Deserialize as string

User can consume converted JSON content to String content too. Every language has a function deserializeAsString() which converts compiles and converts CJSON to JSON content and returns JSON content in string format

Removing key

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.

Here are the examples

Single removal

Single removal can be performed by simply providing the JPath as parameters.

Bulk removal

Bulk removal accepts List<String> JPaths to perform removal.

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.

Here are the examples

Parse data

Parse

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.

Get all possible keys

Returns all possible JPaths inside JSON.

Get all possible values

Returns all values to all possible JPaths inside JSON. Before executing this function, you need call for getting all possible keys function.

Validate JSON

This function validates whether the provided string is JSON content or not.

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

Examples

Keywords

Keywords Description
$import To import other json file
$.jpath Refer to a local variable inside JSON
Comments(Single/ Multi-line) //
<key> Expects a dynamic variable