koki / json

Fork of golang's encoding/json: now with more validation and error context
BSD 3-Clause "New" or "Revised" License
34 stars 1 forks source link
golang json path-validation type-validation

Koki's JSON library

It's a fork that makes encoding/json better for users.

Features

How to use it

github.com/koki/json is a drop-in replacement for encoding/json. The decoder implementation is different, but the library API remains unchanged. Use this package's json.Unmarshal if you want paths for your decoder errors. Use json.Marshal if you want to omit zero-valued structs.

// Deserialize
foo := Foo{}
err := json.Unmarshal(data, &foo)
...

// Serialize
bar := Bar{}
data, err := json.Marshal(bar)
...

github.com/koki/json/jsonutil is a brand-new package. Use jsonutil.ExtraneousFieldPaths to get a list of paths that weren't decoded into your Go object. i.e. potential typos.

// 1. Parse.
obj := make(map[string]interface{})
parsedObj := Foo{}
err := json.Unmarshal(data, &obj)
...
err = jsonutil.UnmarshalMap(obj, &parsedObj)
...

// 2. Check for unparsed fields--potential typos.
extraneousPaths, err := jsonutil.ExtraneousFieldPaths(obj, parsedObj)
...
if len(extraneousPaths) > 0 {
    return nil, &jsonutil.ExtraneousFieldsError{Paths: extraneousPaths}
}
...