Home

Awesome

Purpose

JSONGen is a tool for generating native Golang types from JSON objects. This automates what is otherwise a very tedious and error prone task when working with JSON.

Build Status GPLv3 License

Usage

$ jsongen -h
Usage of jsongen:
  -dump="NUL": Dump tree structure to file.
  -normalize=true: Squash arrays of struct and determine primitive array type.
  -title=true: Convert identifiers to title case, treating '_' and '-' as word boundaries.

Reading from stdin can be done as follows:

$ cat test.json | jsongen

Or a filename can be passed:

$ jsongen test.json

Using test.json as input the example will produce:

type _ struct {
	Bool              bool          `json:"bool"`
	Boollist          []bool        `json:"boollist"`
	Float             float64       `json:"float"`
	Floatlist         []float64     `json:"floatlist"`
	Heterogeneouslist []interface{} `json:"heterogeneouslist"`
	Int               int64         `json:"int"`
	Intlist           []int64       `json:"intlist"`
	Nil               interface{}   `json:"nil"`
	Nillist           []interface{} `json:"nillist"`
	Sanitary          string        `json:"_Sanitary"`
	Sanitary          string
	Sanitary0         string
	String            string   `json:"string"`
	Stringlist        []string `json:"stringlist"`
	Struct            struct {
		Bool   bool        `json:"bool"`
		Float  float64     `json:"float"`
		Int    int64       `json:"int"`
		Nil    interface{} `json:"nil"`
		String string      `json:"string"`
	} `json:"struct"`
	Structlist []struct {
		Bool   bool    `json:"bool"`
		Float  float64 `json:"float"`
		Int    int64   `json:"int"`
		String string  `json:"string"`
	} `json:"structlist"`
	Structlistsquash []struct {
		Bool   bool    `json:"bool"`
		Float  float64 `json:"float"`
		Int    int64   `json:"int"`
		String string  `json:"string"`
	} `json:"structlistsquash"`
	Structlistsquashconflict []struct {
		Bool     bool        `json:"bool"`
		Conflict interface{} `json:"conflict"`
		Float    float64     `json:"float"`
		Int      int64       `json:"int"`
		String   string      `json:"string"`
	} `json:"structlistsquashconflict"`
	TitleCase  string `json:"title case"`
	TitleCase  string `json:"title_case"`
	TitleCase  string `json:"title-case"`
	Titlecase  string `json:"titlecase"`
	Unsanitary string `json:"0Unsanitary"`
	_          string `json:"123"`
}

Parsing

Field Names

Types

Primitive

Object

Lists

Examples of all of the above can be found in test.json.

Caveats

License

The source of this project is licensed under GNU GPL v3.0, according to http://choosealicense.com/licenses/gpl-3.0/:

Required:

Permitted:

Forbidden:

Feedback

If you find a case that produces incorrect results or you have a feature suggestion, let me know: submit an issue.