8 // RawMessage to make replace json with jsoniter
11 // Unmarshal adapts to json/encoding Unmarshal API
13 // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
14 // Refer to https://godoc.org/encoding/json#Unmarshal for more information
15 func Unmarshal(data []byte, v interface{}) error {
16 return ConfigDefault.Unmarshal(data, v)
19 // UnmarshalFromString convenient method to read from string instead of []byte
20 func UnmarshalFromString(str string, v interface{}) error {
21 return ConfigDefault.UnmarshalFromString(str, v)
24 // Get quick method to get value from deeply nested JSON structure
25 func Get(data []byte, path ...interface{}) Any {
26 return ConfigDefault.Get(data, path...)
29 // Marshal adapts to json/encoding Marshal API
31 // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
32 // Refer to https://godoc.org/encoding/json#Marshal for more information
33 func Marshal(v interface{}) ([]byte, error) {
34 return ConfigDefault.Marshal(v)
37 // MarshalIndent same as json.MarshalIndent. Prefix is not supported.
38 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
39 return ConfigDefault.MarshalIndent(v, prefix, indent)
42 // MarshalToString convenient method to write as string instead of []byte
43 func MarshalToString(v interface{}) (string, error) {
44 return ConfigDefault.MarshalToString(v)
47 // NewDecoder adapts to json/stream NewDecoder API.
49 // NewDecoder returns a new decoder that reads from r.
51 // Instead of a json/encoding Decoder, an Decoder is returned
52 // Refer to https://godoc.org/encoding/json#NewDecoder for more information
53 func NewDecoder(reader io.Reader) *Decoder {
54 return ConfigDefault.NewDecoder(reader)
57 // Decoder reads and decodes JSON values from an input stream.
58 // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
63 // Decode decode JSON into interface{}
64 func (adapter *Decoder) Decode(obj interface{}) error {
65 if adapter.iter.head == adapter.iter.tail && adapter.iter.reader != nil {
66 if !adapter.iter.loadMore() {
70 adapter.iter.ReadVal(obj)
71 err := adapter.iter.Error
75 return adapter.iter.Error
78 // More is there more?
79 func (adapter *Decoder) More() bool {
81 if iter.Error != nil {
89 return c != ']' && c != '}'
92 // Buffered remaining buffer
93 func (adapter *Decoder) Buffered() io.Reader {
94 remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
95 return bytes.NewReader(remaining)
98 // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
99 // Number instead of as a float64.
100 func (adapter *Decoder) UseNumber() {
101 cfg := adapter.iter.cfg.configBeforeFrozen
103 adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
106 // DisallowUnknownFields causes the Decoder to return an error when the destination
107 // is a struct and the input contains object keys which do not match any
108 // non-ignored, exported fields in the destination.
109 func (adapter *Decoder) DisallowUnknownFields() {
110 cfg := adapter.iter.cfg.configBeforeFrozen
111 cfg.DisallowUnknownFields = true
112 adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
115 // NewEncoder same as json.NewEncoder
116 func NewEncoder(writer io.Writer) *Encoder {
117 return ConfigDefault.NewEncoder(writer)
120 // Encoder same as json.Encoder
121 type Encoder struct {
125 // Encode encode interface{} as JSON to io.Writer
126 func (adapter *Encoder) Encode(val interface{}) error {
127 adapter.stream.WriteVal(val)
128 adapter.stream.WriteRaw("\n")
129 adapter.stream.Flush()
130 return adapter.stream.Error
133 // SetIndent set the indention. Prefix is not supported
134 func (adapter *Encoder) SetIndent(prefix, indent string) {
135 config := adapter.stream.cfg.configBeforeFrozen
136 config.IndentionStep = len(indent)
137 adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
140 // SetEscapeHTML escape html by default, set to false to disable
141 func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
142 config := adapter.stream.cfg.configBeforeFrozen
143 config.EscapeHTML = escapeHTML
144 adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
147 // Valid reports whether data is a valid JSON encoding.
148 func Valid(data []byte) bool {
149 return ConfigDefault.Valid(data)