Documentation
¶
Overview ¶
Package json2go implements decoding json strings to go type representation.
Index ¶
- Constants
- type JSONParser
- type JSONParserOpt
- func OptExtractCommonTypes(v bool) JSONParserOpt
- func OptExtractHeuristics(similarity float64, minSize, minOccurrences, minAddedFields int) JSONParserOpt
- func OptMakeMaps(v bool, minAttributes uint) JSONParserOpt
- func OptSkipEmptyKeys(v bool) JSONParserOpt
- func OptStringPointersWhenKeyMissing(v bool) JSONParserOpt
- func OptTimeAsString(v bool) JSONParserOpt
Examples ¶
Constants ¶
const ( // General options DefaultRootName = "Document" // Extraction heuristics DefaultSimilarityThreshold = 0.7 DefaultMinSubsetSize = 2 DefaultMinSubsetOccurrences = 2 DefaultMinAddedFields = 2 // Map conversion DefaultMakeMaps = true DefaultMakeMapsWhenMinAttributes = 5 // Feature toggles DefaultExtractCommonTypes = true DefaultStringPointersWhenKeyMissing = true DefaultSkipEmptyKeys = true DefaultTimeAsStr = false )
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 XY `json:"end"` Start XY `json:"start"` } `json:"line"` } type XY struct { X float64 `json:"x"` Y float64 `json:"y"` }
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 OptExtractHeuristics ¶ added in v1.2.0
func OptExtractHeuristics(similarity float64, minSize, minOccurrences, minAddedFields int) JSONParserOpt
OptExtractHeuristics sets thresholds for extracting similar types and shared subsets. similarity is the minimum score (0.0 to 1.0) for merging types. minSize is the minimum number of fields in a shared subset. minOccurrences is the minimum frequency of a subset to be extracted. minAddedFields is the minimum unique fields required for a new type.
func OptMakeMaps ¶
func OptMakeMaps(v bool, minAttributes uint) JSONParserOpt
OptMakeMaps defines if the parser should attempt to use maps instead of structs when objects have similar values and can be represented as map[string]T. minAttributes defines the minimum number of attributes an object must have to be considered for conversion to a map.
func OptSkipEmptyKeys ¶
func OptSkipEmptyKeys(v bool) JSONParserOpt
OptSkipEmptyKeys toggles skipping keys in the input that contained only null values.
func OptStringPointersWhenKeyMissing ¶
func OptStringPointersWhenKeyMissing(v bool) JSONParserOpt
OptStringPointersWhenKeyMissing toggles whether missing string keys in some documents should result in a pointer (*string) instead of a regular string.
func OptTimeAsString ¶
func OptTimeAsString(v bool) JSONParserOpt
OptTimeAsString toggles whether to treat valid time strings as time.Time or just as strings.