Documentation ¶
Overview ¶
Package json2go implements decoding json strings to go type representation.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONParser ¶
type JSONParser struct {
// contains filtered or unexported fields
}
JSONParser parses successive json inputs and returns go representation as string
func NewJSONParser ¶
func NewJSONParser(rootTypeName string, opts ...JSONParserOpt) *JSONParser
NewJSONParser creates new json Parser
Example ¶
inputs := []string{ `{"line":{"start":{"x":12.1,"y":2.8},"end":{"x":12.1,"y":5.67}}}`, `{"triangle":[{"x":2.34,"y":2.1}, {"x":45.1,"y":6.7}, {"x":4,"y":94.6}]}`, } parser := NewJSONParser( "Document", OptExtractCommonTypes(true), ) for _, in := range inputs { _ = parser.FeedBytes([]byte(in)) } res := parser.String() fmt.Println(res)
Output: type Document struct { Line *struct { End XY `json:"end"` Start XY `json:"start"` } `json:"line,omitempty"` Triangle []XY `json:"triangle,omitempty"` } type XY struct { X float64 `json:"x"` Y float64 `json:"y"` }
func (*JSONParser) ASTDecls ¶
func (p *JSONParser) ASTDecls() []ast.Decl
ASTDecls returns ast type declarations
func (*JSONParser) FeedBytes ¶
func (p *JSONParser) FeedBytes(input []byte) error
FeedBytes consumes json input as bytes. If input is invalid, json unmarshalling error is returned
func (*JSONParser) FeedValue ¶
func (p *JSONParser) FeedValue(input interface{})
FeedValue consumes one of:
- simple type (int, float, string, etc.)
- []interface{} - each value must meet these requirements
- map[string]interface{} - each value must meet these requirements
json.Unmarshal to empty interface value provides perfect input (see example)
Example ¶
var v interface{} _ = json.Unmarshal([]byte(`{"line":{"start":{"x":12.1,"y":2.8},"end":{"x":12.1,"y":5.67}}}`), &v) parser := NewJSONParser("Document") parser.FeedValue(v) res := parser.String() fmt.Println(res)
Output: type Document struct { Line struct { End struct { X float64 `json:"x"` Y float64 `json:"y"` } `json:"end"` Start struct { X float64 `json:"x"` Y float64 `json:"y"` } `json:"start"` } `json:"line"` }
func (*JSONParser) String ¶
func (p *JSONParser) String() string
String returns string representation of go struct fitting parsed json values
type JSONParserOpt ¶
type JSONParserOpt func(*options)
JSONParserOpt is a type for setting parser options.
func OptExtractCommonTypes ¶
func OptExtractCommonTypes(v bool) JSONParserOpt
OptExtractCommonTypes toggles extracting common json nodes as separate types.
func OptMakeMaps ¶
func OptMakeMaps(v bool, minAttributes uint) JSONParserOpt
OptMakeMaps defines if parser should try to use maps instead of structs when possible. minAttributes defines minimum number of attributes in object to try converting it to a map.
func OptSkipEmptyKeys ¶
func OptSkipEmptyKeys(v bool) JSONParserOpt
OptSkipEmptyKeys toggles skipping keys in input that were only nulls.
func OptStringPointersWhenKeyMissing ¶
func OptStringPointersWhenKeyMissing(v bool) JSONParserOpt
OptStringPointersWhenKeyMissing toggles wether missing string key in one of documents should result in pointer string.
func OptTimeAsString ¶
func OptTimeAsString(v bool) JSONParserOpt
OptTimeAsString toggles using time.Time for valid time strings or just a string.