2.1.1 • Published 1 year ago

coded-json v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Installation

npm i coded-json

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 Cjson from "coded-json";
    var cjson: Cjson = new Cjson("path/to/file.cjson");
    var b = cjson.deserialize();

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 { Cjson } from 'coded-json'; 
    var cjson = new Cjson(file/path/to/file.cjson);
    var b = cjson.deserialize();

Output

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

Dynamic variable injection

file.cjson

{
    "target": {
        "types": "asd",
        "fruit": <fruit>,
        "quantity": <quantity>,
    },
    "jsonInjection": <jsonTypeData>
}

Code

    var cjson = new Cjson(file/path/to/file.cjson);
    var injectObj = {
        fruit: "apple",
        quantity: 1,
        jsonTypeData: {
            injectedData: "jsonInjectionValue"
        }
    };
    var deserializedVal = cjson.inject(injectObj);

Output

{
    "target": {
        "types": "asd",
        "fruit": "apple,
        "quantity": 1,
    },
    "jsonInjection": {
        injectedData: "jsonInjectionValue"
    }
}

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
}
2.1.1

1 year ago

2.1.0

1 year ago

2.0.0

2 years ago

1.2.0

2 years ago

1.3.2

2 years ago

1.2.1

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago