Home

Awesome

adhocore/jsonc

Latest Version Software License Go Report Test Codecov Tweet Support

<!-- [![Donate 15](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square&label=donate+15)](https://www.paypal.me/ji10/15usd) [![Donate 25](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square&label=donate+25)](https://www.paypal.me/ji10/25usd) [![Donate 50](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square&label=donate+50)](https://www.paypal.me/ji10/50usd) -->

Example

This is example of the JSON that you can parse with adhocore/jsonc:

/*start*/
//..
{
    // this is line comment
    a: [ // unquoted key
        'bb', // single quoted string
        "cc", // double quoted string
    /* multi line
     * comment
     */
        123, // number
        +10, // +ve number, equivalent to 10
        -20, // -ve number
        .25, // floating number, equivalent to 0.25
        5.,  // floating number, equivalent to 5.0
        0xabcDEF, // hex base16 number, equivalent to base10 counterpart: 11259375
        {
            123: 0xf, // even number as key?
            xx: [1, .1, 'xyz',], y: '2', // array inside object, inside array
        },
        "// not a comment",
        "/* also not a comment */",
        ['', "", true, false, null, 1, .5, 2., 0xf, // all sort of data types
            {key:'val'/*comment*/,}], // object inside array, inside array
        'single quoted',
    ],
    /*aa*/aa: ['AA', {in: ['a', "b", ],},],
    'd': { // single quoted key
        t: /*creepy comment*/true, 'f': false,
        a_b: 1, _1_z: 2, Ḁẟḕỻ: 'ɷɻɐỨẞṏḉ', // weird keys?
        "n": null /*multiple trailing commas?*/,,,
        /* 1 */
        /* 2 */
    },
    "e": 'it\'s "good", eh?', // double quoted key, single quoted value with escaped quote
    // json with comment inside json with comment, read that again:
    "g": "/*comment*/{\"i\" : 1, \"url\" : \"http://foo.bar\" }//comment",
    "h": "a new line after word 'literal'
this text is in a new line as there is literal EOL just above. \
but this one is continued in same line due to backslash escape",
    // 1.
    // 2.
}
//..
/*end*/

Find jsonc in pkg.go.dev.

Installation

go get -u github.com/adhocore/jsonc

Usecase

You would ideally use this for organizing JSON configurations for humans to read and manage. The JSON5 input is processed down into JSON which can be Unmarshal'ed by encoding/json.

For performance reasons you may also use cached decoder to have a cached copy of processed JSON output.

Usage

Import and init library:

import (
	"fmt"
	"github.com/adhocore/jsonc"
)

j := jsonc.New()

Strip and parse:

json := []byte(`{
	// single line comment
	"a'b": "apple'ball",
	/* multi line
	   comment */
	"cat": [
		"dog",
		"// not a comment",
		"/* also not a comment */",
	],
	"longtext": "long text in
	multple lines",
}`)

var out map[string]interface{}

j.Unmarshall(json, &out)
fmt.Printf("%+v\n", out)

Strip comments/commas only:

json := []byte(`{"some":"json",}`)
json = j.Strip(json)

Using strings instead of byte array:

json := `{"json": "some
	text",// comment
	"array": ["a",]
}`
json = j.StripS(json)

Parsing from JSON file directly:

var out map[string]interface{}

j.UnmarshalFile("./examples/test.json5", &out)
fmt.Printf("%+v\n", out)

Cached Decoder

If you are weary of parsing same JSON5 source file over and over again, you can use cached decoder. The source file is preprocessed and cached into output file with extension .cached.json. It syncs the file mtime (aka modified time) from JSON5 source file to the cached JSON file to detect change.

The output file can then be consumed readily by encoding/json. Leave that cached output untouched for machine and deal with source file only.

(You can add *.cached.json to .gitignore if you so wish.)

As an example examples/test.json5 will be processed and cached into examples/test.cached.json.

Every change in source file examples/test.json5 is reflected to the cached output on next call to Decode() thus always maintaining the sync.

import (
    "fmt"
    "github.com/adhocore/jsonc"
)

var dest map[string]interface{}
err := jsonc.NewCachedDecoder().Decode("./examples/test.json5", &dest);
if err != nil {
    fmt.Printf("%+v", err)
} else {
    fmt.Printf("%+v", dest)
}

Run working examples with go run examples/main.go.


License

© MIT | 2022-2099, Jitendra Adhikari


Other projects

My other golang projects you might find interesting and useful: