GithubHelp home page GithubHelp logo

jangocheng / json2go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mohae/json2go

0.0 1.0 1.0 1.21 MB

Generate Go structs from JSON with struct tags: can specify type name, package name, and additional field tag keys with the CLI app.

License: Other

Go 100.00%

json2go's Introduction

json2go

GoDocBuild Status

Generate Go structs from JSON with struct tags: can specify type name, package name, and additional field tag keys with the CLI app.

The type may be one of the following:

* struct
* map[string]T
* map[string][]T

By default, a struct will be generated.

If the source JSON is an array of objects, the first element in the array will be used to generate the definition(s). Any objects within the JSON will result in additional embedded struct types.

The generated Go code will be part of package main unless another package name is set. Optionally, the import statement for encoding/json can be added to the Go source code.

The source JSON can also be written to a provided writer.

Keys with underscores, _, are converted to MixedCase. If any part of a key with underscores matches the list of common initialisms, that element is uppercased, e.g "person_id" becomes "personID". Keys starting with characters that are invalid for Go variable names have those characters discarded, unless they are a number, 0-9, which are converted to their word equivalents. All fields are exported and the JSON field tag for the field is generated using the original JSON key value.

If a field's value is null, the field's type will be interface{}, as that field's type is not determinable.

There is also a json2go CLI app. See that README for more info and examples; including how to install it.

Examples:

map[string][]T

Source JSON:

{
  "Blackhawks": [
    {
      "name": "Tony Esposito",
      "number": 35,
      "position": "Goal Tender"
    },
    {
      "name": "Stan Mikita",
      "number": 21,
      "position": "Center"
    }
  ]
}

Using Team as the parent type name and Player as the struct name:

package main

type Team map[string][]Player

type Player struct {
	Name     string `json:"name"`
	Number   int    `json:"number"`
	Position string `json:"position"`
}

struct

Source JSON from http://json.org/example.html:

{
	"widget": {
		"debug": "on",
		"window": {
 			"title": "Sample Konfabulator Widget",
         		"name": "main_window",
         		"width": 500,
         		"height": 500
 		},
 		"image": {
         		"src": "Images/Sun.png",
         		"name": "sun1",
         		"hOffset": 250,
         		"vOffset": 250,
         		"alignment": "center"
 		},
 		"text": {
 			"data": "Click Here",
         		"size": 36,
         		"style": "bold",
         		"name": "text1",
         		"hOffset": 250,
         		"vOffset": 100,
         		"alignment": "center",
         		"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
 		}
	}
}

Using Thing as the parent type name results in:

package main

type Thing struct {
	 Widget `json:"widget"
}
type Widget struct {
	Debug  string `json:"debug"`
	Image  `json:"image"`
	Text   `json:"text"`
	Window `json:"window"`
}

type Image struct {
	Alignment string `json:"alignment"`
	HOffset   int    `json:"hOffset"`
	Name      string `json:"name"`
	Src       string `json:"src"`
	VOffset   int    `json:"vOffset"`
}

type Text struct {
	Alignment string `json:"alignment"`
	Data      string `json:"data"`
	HOffset   int    `json:"hOffset"`
	Name      string `json:"name"`
	OnMouseUp string `json:"onMouseUp"`
	Size      int    `json:"size"`
	Style     string `json:"style"`
	VOffset   int    `json:"vOffset"`
}

type Window struct {
	Height int    `json:"height"`
	Name   string `json:"name"`
	Title  string `json:"title"`
	Width  int    `json:"width"`
}

json2go's People

Contributors

mohae avatar pwaller avatar zph avatar

Watchers

 avatar

Forkers

jangocity

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.