Documentation ¶
Overview ¶
Package jx implements RFC 7159 json encoding and decoding.
Example ¶
package main import ( "fmt" "github.com/go-faster/jx" ) func main() { var e jx.Encoder e.Obj(func(e *jx.Encoder) { e.FieldStart("data") e.Base64([]byte("hello")) }) fmt.Println(e) if err := jx.DecodeBytes(e.Bytes()).Obj(func(d *jx.Decoder, key string) error { v, err := d.Base64() fmt.Printf("%s: %s\n", key, v) return err }); err != nil { panic(err) } }
Output: {"data":"aGVsbG8="} data: hello
Index ¶
- func PutDecoder(d *Decoder)
- func PutEncoder(e *Encoder)
- func PutWriter(e *Writer)
- func Valid(data []byte) bool
- type ArrIter
- type Decoder
- func (d *Decoder) Arr(f func(d *Decoder) error) error
- func (d *Decoder) ArrIter() (ArrIter, 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) Int16() (int16, error)
- func (d *Decoder) Int32() (int32, error)
- func (d *Decoder) Int64() (int64, error)
- func (d *Decoder) Int8() (int8, 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) ObjIter() (ObjIter, 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) UInt16() (uint16, error)
- func (d *Decoder) UInt32() (uint32, error)
- func (d *Decoder) UInt64() (uint64, error)
- func (d *Decoder) UInt8() (uint8, error)
- func (d *Decoder) Validate() error
- type Encoder
- func (e *Encoder) Arr(f func(e *Encoder)) (fail bool)
- func (e *Encoder) ArrEmpty() bool
- func (e *Encoder) ArrEnd() bool
- func (e *Encoder) ArrStart() (fail bool)
- func (e *Encoder) Base64(data []byte) bool
- func (e *Encoder) Bool(v bool) bool
- func (e *Encoder) ByteStr(v []byte) bool
- func (e *Encoder) ByteStrEscape(v []byte) bool
- func (e Encoder) Bytes() []byte
- func (e *Encoder) Close() error
- func (e *Encoder) Field(name string, f func(e *Encoder)) (fail bool)
- func (e *Encoder) FieldStart(field string) (fail bool)
- func (e *Encoder) Float32(v float32) bool
- func (e *Encoder) Float64(v float64) bool
- func (e *Encoder) Grow(n int)
- func (e *Encoder) Int(v int) bool
- func (e *Encoder) Int16(v int16) bool
- func (e *Encoder) Int32(v int32) bool
- func (e *Encoder) Int64(v int64) bool
- func (e *Encoder) Int8(v int8) bool
- func (e *Encoder) Null() bool
- func (e *Encoder) Num(v Num) bool
- func (e *Encoder) Obj(f func(e *Encoder)) (fail bool)
- func (e *Encoder) ObjEmpty() bool
- func (e *Encoder) ObjEnd() bool
- func (e *Encoder) ObjStart() (fail bool)
- func (e *Encoder) Raw(b []byte) bool
- func (e *Encoder) RawStr(v string) bool
- func (e *Encoder) Reset()
- func (e *Encoder) ResetWriter(out io.Writer)
- func (e *Encoder) SetBytes(buf []byte)
- func (e *Encoder) SetIdent(n int)
- func (e *Encoder) Str(v string) bool
- func (e *Encoder) StrEscape(v string) bool
- func (e Encoder) String() string
- func (e *Encoder) UInt(v uint) bool
- func (e *Encoder) UInt16(v uint16) bool
- func (e *Encoder) UInt32(v uint32) bool
- func (e *Encoder) UInt64(v uint64) bool
- func (e *Encoder) UInt8(v uint8) bool
- 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) Format(f fmt.State, verb rune)
- 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 ObjIter
- type Raw
- type Type
- type Writer
- func (w *Writer) ArrEnd() bool
- func (w *Writer) ArrStart() bool
- func (w *Writer) Base64(data []byte) bool
- func (w *Writer) Bool(v bool) bool
- func (w *Writer) ByteStr(v []byte) bool
- func (w *Writer) ByteStrEscape(v []byte) bool
- func (w *Writer) Close() error
- func (w *Writer) Comma() bool
- func (w *Writer) False() bool
- func (w *Writer) FieldStart(field string) bool
- func (w *Writer) Float(v float64, bits int) bool
- func (w *Writer) Float32(v float32) bool
- func (w *Writer) Float64(v float64) bool
- func (w *Writer) Grow(n int)
- func (w *Writer) Int(v int) bool
- func (w *Writer) Int16(v int16) (fail bool)
- func (w *Writer) Int32(v int32) (fail bool)
- func (w *Writer) Int64(v int64) (fail bool)
- func (w *Writer) Int8(v int8) (fail bool)
- func (w *Writer) Null() bool
- func (w *Writer) Num(v Num) bool
- func (w *Writer) ObjEnd() bool
- func (w *Writer) ObjStart() bool
- func (w *Writer) Raw(b []byte) bool
- func (w *Writer) RawStr(v string) bool
- func (w *Writer) Reset()
- func (w *Writer) ResetWriter(out io.Writer)
- func (w *Writer) Str(v string) bool
- func (w *Writer) StrEscape(v string) bool
- func (w Writer) String() string
- func (w *Writer) True() bool
- func (w *Writer) UInt(v uint) bool
- func (w *Writer) UInt16(v uint16) (fail bool)
- func (w *Writer) UInt32(v uint32) (fail bool)
- func (w *Writer) UInt64(v uint64) (fail bool)
- func (w *Writer) UInt8(v uint8) bool
- func (w *Writer) Write(p []byte) (n int, err error)
- func (w *Writer) WriteTo(t io.Writer) (n int64, err error)
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/go-faster/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 ArrIter ¶ added in v0.33.0
type ArrIter struct {
// contains filtered or unexported fields
}
ArrIter is decoding array iterator.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes json.
Can decode 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/go-faster/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 ¶
Base64 decodes base64 encoded data from string.
Same as encoding/json, base64.StdEncoding or RFC 4648.
Example ¶
package main import ( "fmt" "github.com/go-faster/jx" ) func main() { data, _ := jx.DecodeStr(`"SGVsbG8="`).Base64() fmt.Printf("%s", data) }
Output: Hello
func (*Decoder) Base64Append ¶
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.
Example ¶
package main import ( "fmt" "github.com/go-faster/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 skips to the start of next array element, returning true boolean if element exists.
Can be called before or in Array.
func (*Decoder) Num ¶
Num decodes number.
Do not retain returned value, it references underlying buffer.
Example ¶
package main import ( "fmt" "github.com/go-faster/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 ¶
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/go-faster/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) 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.
func (*Decoder) StrBytes ¶
StrBytes returns string value as sub-slice of internal buffer.
Bytes are valid only until next call to any Decoder method.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes json to underlying buffer.
Zero value is valid.
func NewStreamingEncoder ¶ added in v1.0.0
NewStreamingEncoder creates new streaming encoder.
func (*Encoder) Arr ¶ added in v0.21.0
Arr writes start of array, invokes callback and writes end of array.
If callback is nil, writes empty array.
func (*Encoder) ArrEnd ¶
ArrEnd writes end of array, performing indentation if needed.
Use Arr as convenience helper for writing arrays.
func (*Encoder) ArrStart ¶
ArrStart writes start of array, performing indentation if needed.
Use Arr as convenience helper for writing arrays.
func (*Encoder) Base64 ¶
Base64 encodes data as standard base64 encoded string.
Same as encoding/json, base64.StdEncoding or RFC 4648.
Example ¶
package main import ( "fmt" "github.com/go-faster/jx" ) func main() { var e jx.Encoder e.Base64([]byte("Hello")) fmt.Println(e) data, _ := jx.DecodeBytes(e.Bytes()).Base64() fmt.Printf("%s", data) }
Output: "SGVsbG8=" Hello
func (*Encoder) ByteStr ¶ added in v0.34.0
ByteStr encodes byte slice without html escaping.
Use ByteStrEscape to escape html, this is default for encoding/json and should be used by default for untrusted strings.
func (*Encoder) ByteStrEscape ¶ added in v0.34.0
ByteStrEscape encodes string with html special characters escaping.
func (*Encoder) Close ¶ added in v1.0.0
Close flushes underlying buffer to writer in streaming mode. Otherwise, it does nothing.
func (*Encoder) Field ¶ added in v0.19.0
Field encodes field start and then invokes callback.
Has ~5ns overhead over FieldStart.
func (*Encoder) FieldStart ¶ added in v0.22.0
FieldStart encodes field name and writes colon.
For non-zero indentation also writes single space after colon.
Use Field as convenience helper for encoding fields.
func (*Encoder) Obj ¶ added in v0.21.0
Obj writes start of object, invokes callback and writes end of object.
If callback is nil, writes empty object.
func (*Encoder) ObjEnd ¶
ObjEnd writes end of object token, performing indentation if needed.
Use Obj as convenience helper for writing objects.
func (*Encoder) ObjStart ¶
ObjStart writes object start, performing indentation if needed.
Use Obj as convenience helper for writing objects.
func (*Encoder) Reset ¶
func (e *Encoder) Reset()
Reset resets underlying buffer.
If e is in streaming mode, it is reset to non-streaming mode.
func (*Encoder) ResetWriter ¶ added in v1.0.0
ResetWriter resets underlying buffer and sets output writer.
func (*Encoder) SetIdent ¶
SetIdent sets length of single indentation step.
Example ¶
package main import ( "fmt" "github.com/go-faster/jx" ) func main() { var e jx.Encoder e.SetIdent(2) e.ObjStart() e.FieldStart("data") e.ArrStart() e.Int(1) e.Int(2) e.ArrEnd() e.ObjEnd() fmt.Println(e) }
Output: { "data": [ 1, 2 ] }
func (*Encoder) Str ¶
Str encodes string without html escaping.
Use StrEscape to escape html, this is default for encoding/json and should be used by default for untrusted strings.
func (Encoder) String ¶
String returns string of underlying buffer.
Example ¶
package main import ( "fmt" "github.com/go-faster/jx" ) func main() { var e jx.Encoder e.ObjStart() // { e.FieldStart("values") // "values": e.ArrStart() // [ for _, v := range []int{4, 8, 15, 16, 23, 42} { 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 ¶
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) Int64 ¶
Int64 decodes number as a signed 64-bit integer. Works on floats with zero fractional part.
type ObjIter ¶ added in v0.35.0
type ObjIter struct {
// contains filtered or unexported fields
}
ObjIter is decoding object iterator.
func (*ObjIter) Err ¶ added in v0.35.0
Err returns the error, if any, that was encountered during iteration.
type Writer ¶ added in v0.26.0
type Writer struct { Buf []byte // underlying buffer // contains filtered or unexported fields }
Writer writes json tokens to underlying buffer.
Zero value is valid.
func (*Writer) Base64 ¶ added in v0.26.0
Base64 encodes data as standard base64 encoded string.
Same as encoding/json, base64.StdEncoding or RFC 4648.
func (*Writer) ByteStr ¶ added in v0.34.0
ByteStr encodes string without html escaping.
Use ByteStrEscape to escape html, this is default for encoding/json and should be used by default for untrusted strings.
func (*Writer) ByteStrEscape ¶ added in v0.34.0
ByteStrEscape encodes string with html special characters escaping.
func (*Writer) Close ¶ added in v1.0.0
Close flushes underlying buffer to writer in streaming mode. Otherwise, it does nothing.
func (*Writer) FieldStart ¶ added in v0.26.0
FieldStart encodes field name and writes colon.
func (*Writer) Float32 ¶ added in v0.26.0
Float32 encodes float32.
NB: Infinities and NaN are represented as null.
func (*Writer) Float64 ¶ added in v0.26.0
Float64 encodes float64.
NB: Infinities and NaN are represented as null.
func (*Writer) Grow ¶ added in v1.1.0
Grow grows the underlying buffer.
Calls (*bytes.Buffer).Grow(n int) on w.Buf.
func (*Writer) Reset ¶ added in v0.26.0
func (w *Writer) Reset()
Reset resets underlying buffer.
If w is in streaming mode, it is reset to non-streaming mode.
func (*Writer) ResetWriter ¶ added in v1.0.0
ResetWriter resets underlying buffer and sets output writer.
func (*Writer) Str ¶ added in v0.26.0
Str encodes string without html escaping.
Use StrEscape to escape html, this is default for encoding/json and should be used by default for untrusted strings.
func (*Writer) StrEscape ¶ added in v0.26.0
StrEscape encodes string with html special characters escaping.
Source Files ¶
- dec.go
- dec_arr.go
- dec_arr_iter.go
- dec_b64.go
- dec_bool.go
- dec_capture.go
- dec_depth.go
- dec_error.go
- dec_float.go
- dec_float_big.go
- dec_int.gen.go
- dec_int.go
- dec_null.go
- dec_num.go
- dec_obj.go
- dec_obj_iter.go
- dec_raw.go
- dec_read.go
- dec_skip.go
- dec_str.go
- dec_validate.go
- enc.go
- enc_b64.go
- enc_comma.go
- enc_float.go
- enc_int.go
- enc_num.go
- enc_str.go
- enc_str_escape.go
- enc_stream.go
- generate.go
- jx.go
- num.go
- w.go
- w_b64.go
- w_float.go
- w_float_bits.go
- w_int.gen.go
- w_int.go
- w_num.go
- w_str.go
- w_str_escape.go
- w_stream.go
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
byteseq
Package byteseq provides a Byteseq type that can be used to represent a sequence of bytes.
|
Package byteseq provides a Byteseq type that can be used to represent a sequence of bytes. |
tools
|
|
mkint
Command mkencint generates integer encoding/decoding functions.
|
Command mkencint generates integer encoding/decoding functions. |