Documentation ¶
Overview ¶
Example ¶
data := `{ // A comment! You normally can't put these in JSON "obj1": { "foo": "bar", // <-- A trailing comma! No worries. }, /* This style of comments will also be safely removed. */ "array": [1, 2, 3, ], // Trailing comma in array. "include": @incl("testdata.json"), // Include another json file. identifier_simple1: 1234, $identifierSimple2: "abc", "obj2": { "foo": "bar", /* Another style inline comment. */ }, // <-- Another trailing comma! 'py_true': True, // Single quote string and True as true value. py_false: False, // Simple identifier and Python False as false value. py_none: None, /* Simple identifier and Python None as null value. */ "test_env": @env("SOME_ENV"), // Read environment variable. "test_ref1": @ref("obj1.foo"), // Reference to other values, wil be "bar". "test_ref2": @ref("array.2"), // Another reference, will be 3. "test_ref3": @ref("array.#"), // Get length of "array", will be 3. "friends": [ {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} ], "test_ref4": @ref("friends.#.first"), // Will be ["Dale","Roger","Jane"]. }` os.Setenv("SOME_ENV", "some-env-value") var clean json.RawMessage err := Unmarshal([]byte(data), &clean, EnableEnv()) if err != nil { panic(err) } out, _ := json.MarshalIndent(clean, "", " ") fmt.Println(string(out))
Output: { "obj1": { "foo": "bar" }, "array": [ 1, 2, 3 ], "include": { "foo": "bar" }, "identifier_simple1": 1234, "$identifierSimple2": "abc", "obj2": { "foo": "bar" }, "py_true": true, "py_false": false, "py_none": null, "test_env": "some-env-value", "test_ref1": "bar", "test_ref2": 3, "test_ref3": 3, "friends": [ { "first": "Dale", "last": "Murphy", "age": 44, "nets": [ "ig", "fb", "tw" ] }, { "first": "Roger", "last": "Craig", "age": 68, "nets": [ "fb", "tw" ] }, { "first": "Jane", "last": "Murphy", "age": 47, "nets": [ "ig", "tw" ] } ], "test_ref4": [ "Dale", "Roger", "Jane" ] }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clean ¶
Clean parses data with extended feature and returns it as normal spec-compliant JSON data.
func Dump ¶
Dump writes v to the named file at path using JSON encoding. It disables HTMLEscape. Optionally indent can be applied to the output, empty prefix and indent disables indentation. The output is friendly to read by humans.
func Load ¶
Load reads JSON-encoded data from the named file at path and stores the result in the value pointed to by v.
In additional to features of encoding/json, it enables extended features such as "trailing comma", "comments", "file including", "refer" etc. The extended features are documented in the README file.
func Unmarshal ¶
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
In addition to features of encoding/json, it enables extended features such as "trailing comma", "comments", "file including", "refer", etc. The extended features are documented in the README file.
Types ¶
type ExtOption ¶
type ExtOption struct {
// contains filtered or unexported fields
}
ExtOption represents an option to customize the extended features.
func EnableEnv ¶ added in v0.2.0
func EnableEnv() ExtOption
EnableEnv enables reading environment variables. By default, it is disabled for security consideration.
func IncludeRoot ¶
IncludeRoot specifies the root directory to use with the extended file including feature.
func WithFuncMap ¶ added in v0.3.1
WithFuncMap specifies additional functions to use with the "@fn" directive.
type FuncMap ¶ added in v0.3.1
type FuncMap map[string]interface{}
FuncMap is the type of the map defining the mapping from names to functions. Each function must have either a single return value, or two return values of which the second has type error. In that case, if the second (error) return value evaluates to non-nil during execution, execution terminates and the error will be returned.