Documentation
¶
Index ¶
Examples ¶
Constants ¶
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 )
Variables ¶
This section is empty.
Functions ¶
func NewDecoder ¶
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 ¶
package main
import (
"fmt"
"reflect"
"strings"
json "github.com/akutz/gdj"
)
func main() {
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) ¶
package main
import (
"fmt"
"reflect"
"strings"
json "github.com/akutz/gdj"
)
type Person struct {
Name string `json:"name"`
Attributes []interface{} `json:"attributes,omitempty"`
}
func (p Person) GetName() string {
return p.Name
}
func (p *Person) SetName(s string) {
p.Name = s
}
type Spouse struct {
Person
}
func main() {
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)
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
Example ¶
package main
import (
"fmt"
"os"
json "github.com/akutz/gdj"
)
func main() {
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}, // Crimson
"Red",
CMYK{Cyan: 0, Magenta: 92, Yellow: 58, Key: 12}, // Ruby
0x800000, // Maroon
},
}
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) ¶
package main
import (
"os"
json "github.com/akutz/gdj"
)
type Person struct {
Name string `json:"name"`
Attributes []interface{} `json:"attributes,omitempty"`
}
func (p Person) GetName() string {
return p.Name
}
func (p *Person) SetName(s string) {
p.Name = s
}
type Spouse struct {
Person
}
func main() {
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 ¶
type DiscriminatorEncodeMode ¶
type DiscriminatorEncodeMode = json.DiscriminatorEncodeMode
DiscriminatorEncodeMode is a mask that describes the different encode options.
type DiscriminatorToTypeFunc ¶
type DiscriminatorToTypeFunc = json.DiscriminatorToTypeFunc
DiscriminatorToTypeFunc is used to get a reflect.Type from its discriminator.
Directories
¶
| Path | Synopsis |
|---|---|
|
go1
|
|
|
1.17/1.17.13
Package json implements encoding and decoding of JSON as defined in RFC 7159.
|
Package json implements encoding and decoding of JSON as defined in RFC 7159. |
|
1.18/1.18.9
Package json implements encoding and decoding of JSON as defined in RFC 7159.
|
Package json implements encoding and decoding of JSON as defined in RFC 7159. |
|
1.19/1.19.4
Package json implements encoding and decoding of JSON as defined in RFC 7159.
|
Package json implements encoding and decoding of JSON as defined in RFC 7159. |
|
1.20/1.20rc1
Package json implements encoding and decoding of JSON as defined in RFC 7159.
|
Package json implements encoding and decoding of JSON as defined in RFC 7159. |