Documentation ¶
Overview ¶
Example (CustomMarshalJSON) ¶
package main import ( customizablejson "github.com/shogo82148/go-customizable-json" "fmt" "log" "strings" ) type Animal int const ( Unknown Animal = iota Gopher Zebra ) func (a *Animal) UnmarshalJSON(b []byte) error { var s string if err := customizablejson.Unmarshal(b, &s); err != nil { return err } switch strings.ToLower(s) { default: *a = Unknown case "gopher": *a = Gopher case "zebra": *a = Zebra } return nil } func (a Animal) MarshalJSON() ([]byte, error) { var s string switch a { default: s = "unknown" case Gopher: s = "gopher" case Zebra: s = "zebra" } return customizablejson.Marshal(s) } func main() { blob := `["gopher","armadillo","zebra","unknown","gopher","bee","gopher","zebra"]` var zoo []Animal if err := customizablejson.Unmarshal([]byte(blob), &zoo); err != nil { log.Fatal(err) } census := make(map[Animal]int) for _, animal := range zoo { census[animal] += 1 } fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n", census[Gopher], census[Zebra], census[Unknown]) }
Output: Zoo Census: * Gophers: 3 * Zebras: 2 * Unknown: 3
Example (TextMarshalJSON) ¶
package main import ( "encoding/json" "fmt" "log" "strings" ) type Size int const ( Unrecognized Size = iota Small Large ) func (s *Size) UnmarshalText(text []byte) error { switch strings.ToLower(string(text)) { default: *s = Unrecognized case "small": *s = Small case "large": *s = Large } return nil } func (s Size) MarshalText() ([]byte, error) { var name string switch s { default: name = "unrecognized" case Small: name = "small" case Large: name = "large" } return []byte(name), nil } func main() { blob := `["small","regular","large","unrecognized","small","normal","small","large"]` var inventory []Size if err := json.Unmarshal([]byte(blob), &inventory); err != nil { log.Fatal(err) } counts := make(map[Size]int) for _, size := range inventory { counts[size] += 1 } fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n", counts[Small], counts[Large], counts[Unrecognized]) }
Output: Inventory Counts: * Small: 3 * Large: 2 * Unrecognized: 3
Index ¶
- func HTMLEscape(dst *bytes.Buffer, src []byte)
- func Marshal(v interface{}) ([]byte, error)
- func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) error
- func Valid(data []byte) bool
- type Decoder
- type Delim
- type Encoder
- type InvalidUnmarshalError
- type JSONDecoder
- type JSONEncoder
- func (enc *JSONEncoder) Marshal(v interface{}) ([]byte, error)
- func (enc *JSONEncoder) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
- func (enc *JSONEncoder) NewEncoder(w io.Writer) *Encoder
- func (enc *JSONEncoder) Register(val interface{}, f func(v interface{}) ([]byte, error))
- type Marshaler
- type MarshalerError
- type Number
- type RawMessage
- type Token
- type UnmarshalFieldError
- type UnmarshalTypeError
- type Unmarshaler
- type UnsupportedTypeError
- type UnsupportedValueError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTMLEscape ¶
HTMLEscape is an alias of json.HTMLEscape
Example ¶
package main import ( "bytes" "encoding/json" "os" ) func main() { var out bytes.Buffer json.HTMLEscape(&out, []byte(`{"Name":"<b>HTML content</b>"}`)) out.WriteTo(os.Stdout) }
Output: {"Name":"\u003cb\u003eHTML content\u003c/b\u003e"}
func Marshal ¶
Marshal xxx
Example ¶
package main import ( "fmt" "os" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { type ColorGroup struct { ID int Name string Colors []string } group := ColorGroup{ ID: 1, Name: "Reds", Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, } b, err := customizablejson.Marshal(group) if err != nil { fmt.Println("error:", err) } os.Stdout.Write(b) }
Output: {"Colors":["Crimson","Red","Ruby","Maroon"],"ID":1,"Name":"Reds"}
func MarshalIndent ¶
MarshalIndent is like Marshal but applies Indent to format the output.
Example ¶
package main import ( "fmt" "log" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { data := map[string]int{ "a": 1, "b": 2, } json, err := customizablejson.MarshalIndent(data, "<prefix>", "<indent>") if err != nil { log.Fatal(err) } fmt.Println(string(json)) }
Output: { <prefix><indent>"a": 1, <prefix><indent>"b": 2 <prefix>}
func Unmarshal ¶
Unmarshal xxx
Example ¶
package main import ( "fmt" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { var jsonBlob = []byte(`[ {"Name": "Platypus", "Order": "Monotremata"}, {"Name": "Quoll", "Order": "Dasyuromorphia"} ]`) type Animal struct { Name string Order string } var animals []Animal err := customizablejson.Unmarshal(jsonBlob, &animals) if err != nil { fmt.Println("error:", err) } fmt.Printf("%+v", animals) }
Output: [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
func Valid ¶ added in v0.0.3
Valid is an alias of json.Valid.
Example ¶
package main import ( "encoding/json" "fmt" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { goodJSON := `{"example": 1}` badJSON := `{"example":2:]}}` fmt.Println(json.Valid([]byte(goodJSON)), customizablejson.Valid([]byte(badJSON))) }
Output: true false
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes JSON values from an input stream.
Example ¶
This example uses a Decoder to decode a stream of distinct JSON values.
package main import ( "fmt" "io" "log" "strings" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { const jsonStream = ` {"Name": "Ed", "Text": "Knock knock."} {"Name": "Sam", "Text": "Who's there?"} {"Name": "Ed", "Text": "Go fmt."} {"Name": "Sam", "Text": "Go fmt who?"} {"Name": "Ed", "Text": "Go fmt yourself!"} ` type Message struct { Name, Text string } dec := customizablejson.NewDecoder(strings.NewReader(jsonStream)) for { var m Message if err := dec.Decode(&m); err == io.EOF { break } else if err != nil { log.Fatal(err) } fmt.Printf("%s: %s\n", m.Name, m.Text) } }
Output: Ed: Knock knock. Sam: Who's there? Ed: Go fmt. Sam: Go fmt who? Ed: Go fmt yourself!
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.
func (*Decoder) Buffered ¶ added in v0.0.3
Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode.
func (*Decoder) Decode ¶
Decode xxx
Example (Stream) ¶
This example uses a Decoder to decode a streaming array of JSON objects.
package main import ( "fmt" "log" "strings" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { const jsonStream = ` [ {"Name": "Ed", "Text": "Knock knock."}, {"Name": "Sam", "Text": "Who's there?"}, {"Name": "Ed", "Text": "Go fmt."}, {"Name": "Sam", "Text": "Go fmt who?"}, {"Name": "Ed", "Text": "Go fmt yourself!"} ] ` type Message struct { Name, Text string } dec := customizablejson.NewDecoder(strings.NewReader(jsonStream)) // read open bracket t, err := dec.Token() if err != nil { log.Fatal(err) } fmt.Printf("%T: %v\n", t, t) // while the array contains values for dec.More() { var m Message // decode an array value (Message) err := dec.Decode(&m) if err != nil { log.Fatal(err) } fmt.Printf("%v: %v\n", m.Name, m.Text) } // read closing bracket t, err = dec.Token() if err != nil { log.Fatal(err) } fmt.Printf("%T: %v\n", t, t) }
Output: json.Delim: [ Ed: Knock knock. Sam: Who's there? Ed: Go fmt. Sam: Go fmt who? Ed: Go fmt yourself! json.Delim: ]
func (*Decoder) DisallowUnknownFields ¶
func (dec *Decoder) DisallowUnknownFields()
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
func (*Decoder) More ¶ added in v0.0.3
More reports whether there is another element in the current array or object being parsed.
func (*Decoder) Token ¶ added in v0.0.3
Token returns the next JSON token in the input stream. At the end of the input stream, Token returns nil, io.EOF.
Example ¶
This example uses a Decoder to decode a stream of distinct JSON values.
package main import ( "fmt" "io" "log" "strings" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { const jsonStream = ` {"Message": "Hello", "Array": [1, 2, 3], "Null": null, "Number": 1.234} ` dec := customizablejson.NewDecoder(strings.NewReader(jsonStream)) for { t, err := dec.Token() if err == io.EOF { break } if err != nil { log.Fatal(err) } fmt.Printf("%T: %v", t, t) if dec.More() { fmt.Printf(" (more)") } fmt.Printf("\n") } }
Output: json.Delim: { (more) string: Message (more) string: Hello (more) string: Array (more) json.Delim: [ (more) float64: 1 (more) float64: 2 (more) float64: 3 json.Delim: ] (more) string: Null (more) <nil>: <nil> (more) string: Number (more) float64: 1.234 json.Delim: }
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes JSON values to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
func (*Encoder) SetEscapeHTML ¶
SetEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings. The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML.
In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.
type InvalidUnmarshalError ¶
type InvalidUnmarshalError = json.InvalidUnmarshalError
InvalidUnmarshalError is an alias of json.InvalidUnmarshalError.
type JSONDecoder ¶
type JSONDecoder struct {
// contains filtered or unexported fields
}
JSONDecoder xxx
func (*JSONDecoder) NewDecoder ¶ added in v0.0.3
func (dec *JSONDecoder) NewDecoder(r io.Reader) *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.
func (*JSONDecoder) Register ¶
func (dec *JSONDecoder) Register(val interface{}, f func(v interface{}, data []byte) error)
Register records a type and a function for encoding.
func (*JSONDecoder) Unmarshal ¶
func (dec *JSONDecoder) Unmarshal(data []byte, v interface{}) error
Unmarshal xxx
type JSONEncoder ¶
type JSONEncoder struct {
// contains filtered or unexported fields
}
JSONEncoder xxx
func (*JSONEncoder) Marshal ¶
func (enc *JSONEncoder) Marshal(v interface{}) ([]byte, error)
Marshal xxx
func (*JSONEncoder) MarshalIndent ¶
func (enc *JSONEncoder) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
MarshalIndent is like Marshal but applies Indent to format the output.
func (*JSONEncoder) NewEncoder ¶ added in v0.0.3
func (enc *JSONEncoder) NewEncoder(w io.Writer) *Encoder
NewEncoder returns a new encoder that writes to w.
func (*JSONEncoder) Register ¶
func (enc *JSONEncoder) Register(val interface{}, f func(v interface{}) ([]byte, error))
Register records a type and a function for encoding.
type MarshalerError ¶
type MarshalerError = json.MarshalerError
MarshalerError is an alias of json.MarshalerError
type RawMessage ¶
type RawMessage = json.RawMessage
RawMessage is an alias of json.RawMessage.
Example (Marshal) ¶
This example uses RawMessage to use a precomputed JSON during marshal.
package main import ( "encoding/json" "fmt" "os" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { h := customizablejson.RawMessage(`{"precomputed": true}`) c := struct { Header *json.RawMessage `json:"header"` Body string `json:"body"` }{Header: &h, Body: "Hello Gophers!"} b, err := customizablejson.MarshalIndent(&c, "", "\t") if err != nil { fmt.Println("error:", err) } os.Stdout.Write(b) }
Output: { "body": "Hello Gophers!", "header": { "precomputed": true } }
Example (Unmarshal) ¶
This example uses RawMessage to delay parsing part of a JSON message.
package main import ( "fmt" "log" customizablejson "github.com/shogo82148/go-customizable-json" ) func main() { type Color struct { Space string Point customizablejson.RawMessage // delay parsing until we know the color space } type RGB struct { R uint8 G uint8 B uint8 } type YCbCr struct { Y uint8 Cb int8 Cr int8 } var j = []byte(`[ {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}}, {"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}} ]`) var colors []Color err := customizablejson.Unmarshal(j, &colors) if err != nil { log.Fatalln("error:", err) } for _, c := range colors { var dst interface{} switch c.Space { case "RGB": dst = new(RGB) case "YCbCr": dst = new(YCbCr) } err := customizablejson.Unmarshal(c.Point, dst) if err != nil { log.Fatalln("error:", err) } fmt.Println(c.Space, dst) } }
Output: YCbCr &{255 0 -10} RGB &{98 218 255}
type UnmarshalFieldError ¶
type UnmarshalFieldError = json.UnmarshalFieldError
UnmarshalFieldError is an alias of json.UnmarshalFieldError.
type UnmarshalTypeError ¶
type UnmarshalTypeError = json.UnmarshalTypeError
UnmarshalTypeError is an alias of json.UnmarshalTypeError.
type UnsupportedTypeError ¶
type UnsupportedTypeError = json.UnsupportedTypeError
UnsupportedTypeError is an alias of json.UnsupportedTypeError
type UnsupportedValueError ¶
type UnsupportedValueError = json.UnsupportedValueError
UnsupportedValueError is an alias of UnsupportedValueError