Fork of golang's encoding/json: now with more validation and error context
Switch branches/tags
Nothing to show
Clone or download
Latest commit e521cbd Apr 12, 2018

Koki's JSON library

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


  • Annotates decoder errors with the path (e.g. $.pod.container.0.env) where the error occurred. The golang built-in implementation doesn't contextualize errors, so it's hard to track down where they came from.

  • Check for extraneous fields in your JSON, which is useful for detecting misspelled keys and other typos.

  • omitempty actually omits zero-valued structs.

How to use it 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)
... 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}