...
1 package jsoniter
2
3 import (
4 "bytes"
5 "io"
6 )
7
8
9 type RawMessage []byte
10
11
12
13
14
15 func Unmarshal(data []byte, v interface{}) error {
16 return ConfigDefault.Unmarshal(data, v)
17 }
18
19
20 func UnmarshalFromString(str string, v interface{}) error {
21 return ConfigDefault.UnmarshalFromString(str, v)
22 }
23
24
25 func Get(data []byte, path ...interface{}) Any {
26 return ConfigDefault.Get(data, path...)
27 }
28
29
30
31
32
33 func Marshal(v interface{}) ([]byte, error) {
34 return ConfigDefault.Marshal(v)
35 }
36
37
38 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
39 return ConfigDefault.MarshalIndent(v, prefix, indent)
40 }
41
42
43 func MarshalToString(v interface{}) (string, error) {
44 return ConfigDefault.MarshalToString(v)
45 }
46
47
48
49
50
51
52
53 func NewDecoder(reader io.Reader) *Decoder {
54 return ConfigDefault.NewDecoder(reader)
55 }
56
57
58
59 type Decoder struct {
60 iter *Iterator
61 }
62
63
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() {
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
79 func (adapter *Decoder) More() bool {
80 iter := adapter.iter
81 if iter.Error != nil {
82 return false
83 }
84 c := iter.nextToken()
85 if c == 0 {
86 return false
87 }
88 iter.unreadByte()
89 return c != ']' && c != '}'
90 }
91
92
93 func (adapter *Decoder) Buffered() io.Reader {
94 remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
95 return bytes.NewReader(remaining)
96 }
97
98
99
100 func (adapter *Decoder) UseNumber() {
101 cfg := adapter.iter.cfg.configBeforeFrozen
102 cfg.UseNumber = true
103 adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
104 }
105
106
107
108
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)
113 }
114
115
116 func NewEncoder(writer io.Writer) *Encoder {
117 return ConfigDefault.NewEncoder(writer)
118 }
119
120
121 type Encoder struct {
122 stream *Stream
123 }
124
125
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
131 }
132
133
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)
138 }
139
140
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)
145 }
146
147
148 func Valid(data []byte) bool {
149 return ConfigDefault.Valid(data)
150 }
151
View as plain text