podman
150 строк · 4.5 Кб
1package 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 {
16return ConfigDefault.Unmarshal(data, v)
17}
18
19// UnmarshalFromString is a convenient method to read from string instead of []byte
20func UnmarshalFromString(str string, v interface{}) error {
21return 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 {
26return 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) {
34return ConfigDefault.Marshal(v)
35}
36
37// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
38func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
39return ConfigDefault.MarshalIndent(v, prefix, indent)
40}
41
42// MarshalToString convenient method to write as string instead of []byte
43func MarshalToString(v interface{}) (string, error) {
44return 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 {
54return 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 {
60iter *Iterator
61}
62
63// Decode decode JSON into interface{}
64func (adapter *Decoder) Decode(obj interface{}) error {
65if adapter.iter.head == adapter.iter.tail && adapter.iter.reader != nil {
66if !adapter.iter.loadMore() {
67return io.EOF
68}
69}
70adapter.iter.ReadVal(obj)
71err := adapter.iter.Error
72if err == io.EOF {
73return nil
74}
75return adapter.iter.Error
76}
77
78// More is there more?
79func (adapter *Decoder) More() bool {
80iter := adapter.iter
81if iter.Error != nil {
82return false
83}
84c := iter.nextToken()
85if c == 0 {
86return false
87}
88iter.unreadByte()
89return c != ']' && c != '}'
90}
91
92// Buffered remaining buffer
93func (adapter *Decoder) Buffered() io.Reader {
94remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
95return bytes.NewReader(remaining)
96}
97
98// UseNumber causes the Decoder to unmarshal a number into an interface{} as a
99// Number instead of as a float64.
100func (adapter *Decoder) UseNumber() {
101cfg := adapter.iter.cfg.configBeforeFrozen
102cfg.UseNumber = true
103adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
104}
105
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.
109func (adapter *Decoder) DisallowUnknownFields() {
110cfg := adapter.iter.cfg.configBeforeFrozen
111cfg.DisallowUnknownFields = true
112adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
113}
114
115// NewEncoder same as json.NewEncoder
116func NewEncoder(writer io.Writer) *Encoder {
117return ConfigDefault.NewEncoder(writer)
118}
119
120// Encoder same as json.Encoder
121type Encoder struct {
122stream *Stream
123}
124
125// Encode encode interface{} as JSON to io.Writer
126func (adapter *Encoder) Encode(val interface{}) error {
127adapter.stream.WriteVal(val)
128adapter.stream.WriteRaw("\n")
129adapter.stream.Flush()
130return adapter.stream.Error
131}
132
133// SetIndent set the indention. Prefix is not supported
134func (adapter *Encoder) SetIndent(prefix, indent string) {
135config := adapter.stream.cfg.configBeforeFrozen
136config.IndentionStep = len(indent)
137adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
138}
139
140// SetEscapeHTML escape html by default, set to false to disable
141func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
142config := adapter.stream.cfg.configBeforeFrozen
143config.EscapeHTML = escapeHTML
144adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
145}
146
147// Valid reports whether data is a valid JSON encoding.
148func Valid(data []byte) bool {
149return ConfigDefault.Valid(data)
150}
151