blob: f371bfed76400ccaee600ef0d53a15ac91fd3e38 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package jsoniter
2
3import (
4 "bytes"
5 "io"
6)
7
8// RawMessage to make replace json with jsoniter
9type RawMessage []byte
10
11// Unmarshal adapts to json/encoding Unmarshal API
12//
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
15func Unmarshal(data []byte, v interface{}) error {
16 return ConfigDefault.Unmarshal(data, v)
17}
18
19// UnmarshalFromString convenient method to read from string instead of []byte
20func UnmarshalFromString(str string, v interface{}) error {
21 return ConfigDefault.UnmarshalFromString(str, v)
22}
23
24// Get quick method to get value from deeply nested JSON structure
25func Get(data []byte, path ...interface{}) Any {
26 return ConfigDefault.Get(data, path...)
27}
28
29// Marshal adapts to json/encoding Marshal API
30//
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
33func Marshal(v interface{}) ([]byte, error) {
34 return ConfigDefault.Marshal(v)
35}
36
37// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
38func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
39 return ConfigDefault.MarshalIndent(v, prefix, indent)
40}
41
42// MarshalToString convenient method to write as string instead of []byte
43func MarshalToString(v interface{}) (string, error) {
44 return ConfigDefault.MarshalToString(v)
45}
46
47// NewDecoder adapts to json/stream NewDecoder API.
48//
49// NewDecoder returns a new decoder that reads from r.
50//
51// Instead of a json/encoding Decoder, an Decoder is returned
52// Refer to https://godoc.org/encoding/json#NewDecoder for more information
53func NewDecoder(reader io.Reader) *Decoder {
54 return ConfigDefault.NewDecoder(reader)
55}
56
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)
59type Decoder struct {
60 iter *Iterator
61}
62
63// Decode decode JSON into interface{}
64func (adapter *Decoder) Decode(obj interface{}) error {
65 if adapter.iter.head == adapter.iter.tail && adapter.iter.reader != nil {
66 if !adapter.iter.loadMore() {
67 return io.EOF
68 }
69 }
70 adapter.iter.ReadVal(obj)
71 err := adapter.iter.Error
72 if err == io.EOF {
73 return nil
74 }
75 return adapter.iter.Error
76}
77
78// More is there more?
79func (adapter *Decoder) More() bool {
80 iter := adapter.iter
81 if iter.Error != nil {
82 return false
83 }
84 if iter.head != iter.tail {
85 return true
86 }
87 return iter.loadMore()
88}
89
90// Buffered remaining buffer
91func (adapter *Decoder) Buffered() io.Reader {
92 remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
93 return bytes.NewReader(remaining)
94}
95
96// UseNumber causes the Decoder to unmarshal a number into an interface{} as a
97// Number instead of as a float64.
98func (adapter *Decoder) UseNumber() {
99 cfg := adapter.iter.cfg.configBeforeFrozen
100 cfg.UseNumber = true
101 adapter.iter.cfg = cfg.frozeWithCacheReuse()
102}
103
104// DisallowUnknownFields causes the Decoder to return an error when the destination
105// is a struct and the input contains object keys which do not match any
106// non-ignored, exported fields in the destination.
107func (adapter *Decoder) DisallowUnknownFields() {
108 cfg := adapter.iter.cfg.configBeforeFrozen
109 cfg.DisallowUnknownFields = true
110 adapter.iter.cfg = cfg.frozeWithCacheReuse()
111}
112
113// NewEncoder same as json.NewEncoder
114func NewEncoder(writer io.Writer) *Encoder {
115 return ConfigDefault.NewEncoder(writer)
116}
117
118// Encoder same as json.Encoder
119type Encoder struct {
120 stream *Stream
121}
122
123// Encode encode interface{} as JSON to io.Writer
124func (adapter *Encoder) Encode(val interface{}) error {
125 adapter.stream.WriteVal(val)
126 adapter.stream.WriteRaw("\n")
127 adapter.stream.Flush()
128 return adapter.stream.Error
129}
130
131// SetIndent set the indention. Prefix is not supported
132func (adapter *Encoder) SetIndent(prefix, indent string) {
133 config := adapter.stream.cfg.configBeforeFrozen
134 config.IndentionStep = len(indent)
135 adapter.stream.cfg = config.frozeWithCacheReuse()
136}
137
138// SetEscapeHTML escape html by default, set to false to disable
139func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
140 config := adapter.stream.cfg.configBeforeFrozen
141 config.EscapeHTML = escapeHTML
142 adapter.stream.cfg = config.frozeWithCacheReuse()
143}
144
145// Valid reports whether data is a valid JSON encoding.
146func Valid(data []byte) bool {
147 return ConfigDefault.Valid(data)
148}