Coder Social home page Coder Social logo

checkjson's Introduction

Check that a JSON object's keys correspond to a struct's exported members or JSON tags.

ANNOUNCEMENTS

2018.03.14 - Add ExistingJSONKeys()
2018.02.16 - Add test example of using go v1.10 (*Decoder)DisallowUnknownFields()
2017.02.13 - Handle "-" and "omitempty" JSON tags in struct definitions.
2017.02.08 - UnknownJSONKeys lists all JSON object keys that won't be decoded. 
2016.11.18 - MissingJSONKeys lists all struct members that won't be set by JSON object.

USAGE

https://godoc.org/github.com/clbanning/checkjson

	Example:
	
	data := `
	{
	   "elem1":"a simple element",
	   "elem2": {
	      "subelem":"something more complex", 
	      "notes":"take a look at this" }
	   "elem4":"extraneous" 
	}`

	type sub struct {
	   Subelem string `json:"subelem,omitempty"`
	   Another string `json:"another"`
	}
	type elem struct {
	   Elem1 string `json:"elem1"`
	   Elem2 sub    `json:"elem2"`
	   Elem3 bool   `json:"elem3"`
	}

	e := new(elem)
	result, _ := MissingJSONKeys([]byte(data), e)
	// result: [elem2.another elem3]

	result, _ = UnknownJSONKeys([]byte(data), e)
	// result: [elem2.notes elem4]
	// NOTE: using the stdlib json.Decoder with (*Decoder)DisallowUnknownFields() set
	//       will error on the first unknown key; it does not return a slice of all
	//       unknown keys - see: unknownfieldserr_test.go.

LIMITATION

This package does not support recursive struct definitions.

MOTIVATION

I make extensive use of JSON configuration files.  Sometimes the files are large or
complex and JSON keys can be prone to typos or case errors. The "encoding/json" decoder 
just ignores JSON keys that do not correspond to struct member names/tags; this can 
result in unexpected initialization errors or the failure to override defaults. 
The checkjson.Validate() function identifies JSON object keys that cannot be decoded 
to a member of the struct using the "encoding/json" package.

RELATED

There is a similar package for validating XML tags against structs in 
https://github.com/clbanning/checkxml.

checkjson's People

Contributors

clbanning avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.