json
v0.0.0-20230109022746-c7e26b7d2ec4
github.com/akutz/gdj
Documentation
pkg.go.dev ↗Constants
DiscriminatorEncodeTypeNameIfRequired (4 declarations)
const ( // DiscriminatorEncodeTypeNameIfRequired is the default behavior when // the discriminator is set, and the type name is only encoded if required. DiscriminatorEncodeTypeNameIfRequired = json.DiscriminatorEncodeTypeNameIfRequired // DiscriminatorEncodeTypeNameRootValue causes the type name to be encoded // for the root value. DiscriminatorEncodeTypeNameRootValue = json.DiscriminatorEncodeTypeNameRootValue // DiscriminatorEncodeTypeNameAllObjects causes the type name to be encoded // for all struct and map values. Please note this specifically does not // apply to the root value. DiscriminatorEncodeTypeNameAllObjects = json.DiscriminatorEncodeTypeNameAllObjects // DiscriminatorEncodeTypeNameWithPath causes the type name to be encoded // prefixed with the type's full package path. DiscriminatorEncodeTypeNameWithPath = json.DiscriminatorEncodeTypeNameWithPath )
Functions
NewDecoder
func NewDecoder(r io.Reader) *json.Decoder
NewDecoder returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the JSON values requested.
Example
{
var jsonBlob = `{
"ID":1,
"Name":"Reds",
"Colors":[
{"_t":"[3]int","_v":[220,20,60]},
{"_t":"string","_v":"Red"},
{"_t":"CMYK","Cyan":0,"Magenta":92,"Yellow":58,"Key":12},
{"_t":"int","_v":8388608}
]}`
type CMYK struct {
Cyan int
Magenta int
Yellow int
Key int
}
type ColorGroup struct {
ID int
Name string
Colors []interface{}
}
dec := json.NewDecoder(strings.NewReader(jsonBlob))
dec.SetDiscriminator("_t", "_v", func(s string) (reflect.Type, bool) {
switch s {
case "CMYK":
return reflect.TypeOf(CMYK{}), true
case "ColorGroup":
return reflect.TypeOf(ColorGroup{}), true
}
return nil, false
})
var group ColorGroup
if err := dec.Decode(&group); err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", group)
}
Output:
{ID:1 Name:Reds Colors:[[220 20 60] Red {Cyan:0 Magenta:92 Yellow:58 Key:12} 8388608]}
Example (empty_interface)
{
var jsonBlob = `{
"name":"Andrew",
"attributes":[
{"type":"string", "value": "Austin"},
{"type":"uint8", "value":42}
]
}
{
"name":"Mandy",
"attributes":[
{"type":"Spouse", "name": "Andrew"}
]
}`
dec := json.NewDecoder(strings.NewReader(jsonBlob))
dec.SetDiscriminator("type", "value", func(s string) (reflect.Type, bool) {
switch s {
case "Person":
return reflect.TypeOf(Person{}), true
case "Spouse":
return reflect.TypeOf(Spouse{}), true
}
return nil, false
})
var p Person
dec.Decode(&p)
fmt.Printf("%[1]T(%[1]d)\n", p.Attributes[1])
dec.Decode(&p)
fmt.Printf("%[1]T(%[1]s)\n", p.Attributes[0].(Spouse).Name)
}
Output:
uint8(42) string(Andrew)
NewEncoder
func NewEncoder(w io.Writer) *json.Encoder
NewEncoder returns a new encoder that writes to w.
Example
{
type CMYK struct {
Cyan int
Magenta int
Yellow int
Key int
}
type ColorGroup struct {
ID int
Name string
Colors []interface{}
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []interface{}{
[3]int{220, 20, 60},
"Red",
CMYK{Cyan: 0, Magenta: 92, Yellow: 58, Key: 12},
0x800000,
},
}
enc := json.NewEncoder(os.Stdout)
enc.SetDiscriminator("_t", "_v", 0)
if err := enc.Encode(group); err != nil {
fmt.Println("error:", err)
}
}
Output:
{"ID":1,"Name":"Reds","Colors":[{"_t":"[3]int","_v":[220,20,60]},{"_t":"string","_v":"Red"},{"_t":"CMYK","Cyan":0,"Magenta":92,"Yellow":58,"Key":12},{"_t":"int","_v":8388608}]}
Example (empty_interface)
{
enc := json.NewEncoder(os.Stdout)
enc.SetDiscriminator("type", "value", 0)
enc.Encode(Person{"Andrew", []interface{}{"Austin", uint8(42)}})
enc.Encode(Person{"Mandy", []interface{}{Spouse{Person{"Andrew", nil}}}})
}
Output:
{"name":"Andrew","attributes":[{"type":"string","value":"Austin"},{"type":"uint8","value":42}]}
{"name":"Mandy","attributes":[{"type":"Spouse","name":"Andrew"}]}
Types
DiscriminatorEncodeMode
type DiscriminatorEncodeMode = json.DiscriminatorEncodeMode
DiscriminatorEncodeMode is a mask that describes the different encode options.
DiscriminatorToTypeFunc
type DiscriminatorToTypeFunc = json.DiscriminatorToTypeFunc
DiscriminatorToTypeFunc is used to get a reflect.Type from its discriminator.