Package json
Package json
Overview
Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions.
See "JSON and Go" for an introduction to this package: https://golang.org/doc/articles/json_and_go.html
Example (CustomMarshalJSON)
Code:
package json_test
import (
"encoding/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 := json.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 json.Marshal(s)
}
func Example_customMarshalJSON() {
blob := `["gopher","armadillo","zebra","unknown","gopher","bee","gopher","zebra"]`
var zoo []Animal
if err := json.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)
Code:
package json_test
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 Example_textMarshalJSON() {
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 Compact(dst *bytes.Buffer, src []byte) error
- func HTMLEscape(dst *bytes.Buffer, src []byte)
- func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
- 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
- func NewDecoder(r io.Reader) *Decoder
- func (dec *Decoder) Buffered() io.Reader
- func (dec *Decoder) Decode(v interface{}) error
- func (dec *Decoder) DisallowUnknownFields()
- func (dec *Decoder) InputOffset() int64
- func (dec *Decoder) More() bool
- func (dec *Decoder) Token() (Token, error)
- func (dec *Decoder) UseNumber()
- type Delim
- func (d Delim) String() string
- type Encoder
- func NewEncoder(w io.Writer) *Encoder
- func (enc *Encoder) Encode(v interface{}) error
- func (enc *Encoder) SetEscapeHTML(on bool)
- func (enc *Encoder) SetIndent(prefix, indent string)
- type InvalidUTF8Error
- func (e *InvalidUTF8Error) Error() string
- type InvalidUnmarshalError
- func (e *InvalidUnmarshalError) Error() string
- type Marshaler
- type MarshalerError
- func (e *MarshalerError) Error() string
- func (e *MarshalerError) Unwrap() error
- type Number
- func (n Number) Float64() (float64, error)
- func (n Number) Int64() (int64, error)
- func (n Number) String() string
- type RawMessage
- func (m RawMessage) MarshalJSON() ([]byte, error)
- func (m *RawMessage) UnmarshalJSON(data []byte) error
- type SyntaxError
- func (e *SyntaxError) Error() string
- type Token
- type UnmarshalFieldError
- func (e *UnmarshalFieldError) Error() string
- type UnmarshalTypeError
- func (e *UnmarshalTypeError) Error() string
- type Unmarshaler
- type UnsupportedTypeError
- func (e *UnsupportedTypeError) Error() string
- type UnsupportedValueError
- func (e *UnsupportedValueError) Error() string
Examples
Decoder Decoder.Decode (Stream) Decoder.Token HTMLEscape Indent Marshal MarshalIndent RawMessage (Marshal) RawMessage (Unmarshal) Unmarshal Valid Package (CustomMarshalJSON) Package (TextMarshalJSON)
Package files
decode.go encode.go fold.go indent.go scanner.go stream.go tables.go tags.go
func Compact
func Compact(dst *bytes.Buffer, src []byte) error
Compact appends to dst the JSON-encoded src with insignificant space characters elided.
func HTMLEscape
func HTMLEscape(dst *bytes.Buffer, src []byte)
HTMLEscape appends to dst the JSON-encoded src with , &, U+2028 and U+2029 characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 so that the JSON will be safe to embed inside HTML