Documentation ¶
Overview ¶
Package jx implements RFC 7159 json encoding and decoding.
Index ¶
- func PutDecoder(d *Decoder)
- func PutEncoder(e *Encoder)
- func Valid(data []byte) bool
- type Decoder
- func (d *Decoder) Arr(f func(d *Decoder) error) error
- func (d *Decoder) Base64() ([]byte, error)
- func (d *Decoder) Base64Append(b []byte) ([]byte, error)
- func (d *Decoder) BigFloat() (*big.Float, error)
- func (d *Decoder) BigInt() (*big.Int, error)
- func (d *Decoder) Bool() (bool, error)
- func (d *Decoder) Capture(f func(d *Decoder) error) error
- func (d *Decoder) Elem() (ok bool, err error)
- func (d *Decoder) Float32() (float32, error)
- func (d *Decoder) Float64() (float64, error)
- func (d *Decoder) Int() (int, error)
- func (d *Decoder) Int32() (int32, error)
- func (d *Decoder) Int64() (int64, error)
- func (d *Decoder) Next() Type
- func (d *Decoder) Null() error
- func (d *Decoder) Num() (Num, error)
- func (d *Decoder) NumAppend(v Num) (Num, error)
- func (d *Decoder) Obj(f func(d *Decoder, key string) error) error
- func (d *Decoder) ObjBytes(f func(d *Decoder, key []byte) error) error
- func (d *Decoder) Raw() (Raw, error)
- func (d *Decoder) RawAppend(buf Raw) (Raw, error)
- func (d *Decoder) Reset(reader io.Reader)
- func (d *Decoder) ResetBytes(input []byte)
- func (d *Decoder) Skip() error
- func (d *Decoder) Str() (string, error)
- func (d *Decoder) StrAppend(b []byte) ([]byte, error)
- func (d *Decoder) StrBytes() ([]byte, error)
- func (d *Decoder) Uint() (uint, error)
- func (d *Decoder) Uint32() (uint32, error)
- func (d *Decoder) Uint64() (uint64, error)
- type Encoder
- func (e *Encoder) ArrEmpty()
- func (e *Encoder) ArrEnd()
- func (e *Encoder) ArrStart()
- func (e *Encoder) Base64(data []byte)
- func (e *Encoder) Bool(val bool)
- func (e Encoder) Bytes() []byte
- func (e *Encoder) False()
- func (e *Encoder) Float32(v float32)
- func (e *Encoder) Float64(v float64)
- func (e *Encoder) Int(val int)
- func (e *Encoder) Int32(nval int32)
- func (e *Encoder) Int64(nval int64)
- func (e *Encoder) More()
- func (e *Encoder) Null()
- func (e *Encoder) Num(v Num)
- func (e *Encoder) ObjEmpty()
- func (e *Encoder) ObjEnd()
- func (e *Encoder) ObjField(field string)
- func (e *Encoder) ObjStart()
- func (e *Encoder) Raw(v string)
- func (e *Encoder) RawBytes(b []byte)
- func (e *Encoder) Reset()
- func (e *Encoder) SetBytes(buf []byte)
- func (e *Encoder) SetIdent(n int)
- func (e *Encoder) Str(v string)
- func (e *Encoder) StrEscape(v string)
- func (e Encoder) String() string
- func (e *Encoder) True()
- func (e *Encoder) Uint(val uint)
- func (e *Encoder) Uint32(val uint32)
- func (e *Encoder) Uint64(val uint64)
- func (e *Encoder) Write(p []byte) (n int, err error)
- func (e *Encoder) WriteTo(w io.Writer) (n int64, err error)
- type Num
- func (n Num) Equal(v Num) bool
- func (n Num) Float64() (float64, error)
- func (n Num) Int64() (int64, error)
- func (n Num) IsInt() bool
- func (n Num) Negative() bool
- func (n Num) Positive() bool
- func (n Num) Sign() int
- func (n Num) Str() bool
- func (n Num) String() string
- func (n Num) Uint64() (uint64, error)
- func (n Num) Zero() bool
- type Raw
- type Type
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Valid ¶
Valid reports whether data is valid json.
Example ¶
package main import ( "fmt" "github.com/ogen-go/jx" ) func main() { fmt.Println(jx.Valid([]byte(`{"field": "value"}`))) fmt.Println(jx.Valid([]byte(`"Hello, world!"`))) fmt.Println(jx.Valid([]byte(`["foo"}`))) }
Output: true true false
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is streaming json decoder.
Can read from io.Reader or byte slice directly.
func DecodeBytes ¶
DecodeBytes creates a Decoder that reads json from byte slice.
func DecodeStr ¶
DecodeStr creates a Decoder that reads string as json.
Example ¶
package main import ( "fmt" "github.com/ogen-go/jx" ) func main() { d := jx.DecodeStr(`{"values":[4,8,15,16,23,42]}`) // Save all integers from "values" array to slice. var values []int // Iterate over each object field. if err := d.Obj(func(d *jx.Decoder, key string) error { switch key { case "values": // Iterate over each array element. return d.Arr(func(d *jx.Decoder) error { v, err := d.Int() if err != nil { return err } values = append(values, v) return nil }) default: // Skip unknown fields if any. return d.Skip() } }); err != nil { panic(err) } fmt.Println(values) }
Output: [4 8 15 16 23 42]
func (*Decoder) Base64 ¶ added in v0.16.0
Base64 decodes base64 encoded data from string.
Same as encoding/json, base64.StdEncoding or RFC 4648.
func (*Decoder) Base64Append ¶ added in v0.16.0
Base64Append appends base64 encoded data from string.
Same as encoding/json, base64.StdEncoding or RFC 4648.
func (*Decoder) Capture ¶
Capture calls f and then rolls back to state before call.
Does not work with reader.
Example ¶
package main import ( "fmt" "github.com/ogen-go/jx" ) func main() { d := jx.DecodeStr(`["foo", "bar", "baz"]`) var elems int // NB: Currently Capture does not support io.Reader, only buffers. if err := d.Capture(func(d *jx.Decoder) error { // Everything decoded in this callback will be rolled back. return d.Arr(func(d *jx.Decoder) error { elems++ return d.Skip() }) }); err != nil { panic(err) } // Decoder is rolled back to state before "Capture" call. fmt.Println("Read", elems, "elements on first pass") fmt.Println("Next element is", d.Next(), "again") }
Output: Read 3 elements on first pass Next element is array again
func (*Decoder) Elem ¶
Elem reads array element and reports whether array has more elements to read.
func (*Decoder) Num ¶ added in v0.15.0
Num decodes number.
Do not retain returned value, it references underlying buffer.
Example ¶
package main import ( "fmt" "github.com/ogen-go/jx" ) func main() { // Can decode numbers and number strings. d := jx.DecodeStr(`{"foo": "10531.0"}`) var n jx.Num if err := d.Obj(func(d *jx.Decoder, key string) error { v, err := d.Num() if err != nil { return err } n = v return nil }); err != nil { panic(err) } fmt.Println(n) fmt.Println("positive:", n.Positive()) // Can decode floats with zero fractional part as integers: v, err := n.Int64() if err != nil { panic(err) } fmt.Println("int64:", v) }
Output: "10531.0" positive: true int64: 10531
func (*Decoder) Obj ¶
Obj reads json object, calling f on each field.
Use ObjBytes to reduce heap allocations for keys.
func (*Decoder) ObjBytes ¶
ObjBytes calls f for every key in object, using byte slice as key.
The key value is valid only until f is not returned.
func (*Decoder) Raw ¶ added in v0.14.0
Raw is like Skip(), but saves and returns skipped value as raw json.
Do not retain returned value, it references underlying buffer.
Example ¶
package main import ( "fmt" "github.com/ogen-go/jx" ) func main() { d := jx.DecodeStr(`{"foo": [1, 2, 3]}`) var raw jx.Raw if err := d.Obj(func(d *jx.Decoder, key string) error { v, err := d.Raw() if err != nil { return err } raw = v return nil }); err != nil { panic(err) } fmt.Println(raw.Type(), raw) }
Output: array [1, 2, 3]
func (*Decoder) RawAppend ¶ added in v0.14.0
RawAppend is Raw that appends saved raw json value to buf.
func (*Decoder) Reset ¶
Reset resets reader and underlying state, next reads will use provided io.Reader.
func (*Decoder) ResetBytes ¶
ResetBytes resets underlying state, next reads will use provided buffer.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes json to underlying buffer.
Zero value is valid.
func (*Encoder) ArrEnd ¶
func (e *Encoder) ArrEnd()
ArrEnd writes end of array, performing indentation if needed.
func (*Encoder) ArrStart ¶
func (e *Encoder) ArrStart()
ArrStart writes start of array, performing indentation if needed.
func (*Encoder) Base64 ¶ added in v0.16.0
Base64 encodes data as standard base64 encoded string.
Same as encoding/json, base64.StdEncoding or RFC 4648.
func (*Encoder) More ¶
func (e *Encoder) More()
More writes comma, performing indentation if needed.
func (*Encoder) ObjEnd ¶
func (e *Encoder) ObjEnd()
ObjEnd writes end of object token, performing indentation if needed.
func (*Encoder) ObjField ¶
ObjField writes field name and colon.
For non-zero indentation also writes single space after colon.
func (*Encoder) ObjStart ¶
func (e *Encoder) ObjStart()
ObjStart writes object start, performing indentation if needed
func (Encoder) String ¶
String returns string of underlying buffer.
Example ¶
package main import ( "fmt" "github.com/ogen-go/jx" ) func main() { var e jx.Encoder e.ObjStart() // { e.ObjField("values") // "values": e.ArrStart() // [ for i, v := range []int{4, 8, 15, 16, 23, 42} { if i != 0 { e.More() // , } e.Int(v) } e.ArrEnd() // ] e.ObjEnd() // } fmt.Println(e) fmt.Println("Buffer len:", len(e.Bytes())) }
Output: {"values":[4,8,15,16,23,42]} Buffer len: 28
type Num ¶ added in v0.15.0
type Num []byte
Num represents number, which can be raw json number or number string.
Same as Raw, but with number invariants.
Examples:
123.45 // Str: false, IsInt: false "123.45" // Str: true, IsInt: false "12345" // Str: true, IsInt: true 12345 // Str: false, IsInt: true
func (Num) Equal ¶ added in v0.15.0
Equal reports whether numbers are strictly equal, including their formats.
func (Num) Int64 ¶ added in v0.15.0
Int64 decodes number as a signed 64-bit integer. Works on floats with zero fractional part.
func (Num) Sign ¶ added in v0.15.0
Sign reports sign of number.
0 is zero, 1 is positive, -1 is negative.