Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

minjunli/jsonc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonc

jsonc ('c' for 'Config') extends the json syntax to make it serves better as a configuration langurage.

  • C/JS-style comments support
    • single-line comment and inline comment started with //
    • multi-line comment with /* */
  • Expandable keyword support
    • Import data from other json / jsonc file by "_include_json": "path_to_json"

Jsonc package is built on python standard library, and provide the same functions call as python standard library. It can be imported by import jsonc as json, and keep the rest of the code that using json module unchanged.

Examples

By default, duplicated key form sub jsonc files will be overwritten by the main jsonc file.

{
    // file 1.json
    // sub json path can be a relative path or absolute path
    "_include_json": "2.json",
    "key1": 1,
    "dict1":
    {
        "_include_json": "2.json",
        "key2": 2,
        "key4": 4,  // trailing comma is also allowed
    }
}

{
    // file 2.json
    // this key will be overwritten in 'dict1'
    "key2": 22,
    "key3": 23
}
>>> import jsonc as json 
>>> print(json.dumps(json.load(open('1.json', 'r')), indent=4, sort_keys=True))
{
    "_include_json": "2.json",
    "dict1": {
        "_include_json": "2.json",
        "key2": 2,
        "key3": 23,
        "key4": 4
    },
    "key1": 1,
    "key2": 22,
    "key3": 23
}

Tips

  • VSCode users can set syntax highlight for jsonc files to built-in syntax 'Json with Comments'

References

This repo is based on the following awesome projects:

About

json for config files

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages