2016-12-04 16:50:53 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-01-06 14:17:47 +02:00
|
|
|
import (
|
2017-01-07 06:28:16 +02:00
|
|
|
"bytes"
|
2017-06-06 13:36:33 +02:00
|
|
|
"io"
|
2017-01-06 14:17:47 +02:00
|
|
|
)
|
2016-12-05 07:20:27 +02:00
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// RawMessage to make replace json with jsoniter
|
2017-06-19 17:10:20 +02:00
|
|
|
type RawMessage []byte
|
|
|
|
|
2017-06-05 13:31:30 +02:00
|
|
|
// Unmarshal adapts to json/encoding Unmarshal API
|
|
|
|
//
|
|
|
|
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
2017-06-05 13:57:20 +02:00
|
|
|
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
2016-12-04 16:50:53 +02:00
|
|
|
func Unmarshal(data []byte, v interface{}) error {
|
2017-06-17 04:21:37 +02:00
|
|
|
return ConfigDefault.Unmarshal(data, v)
|
2016-12-04 16:50:53 +02:00
|
|
|
}
|
2017-01-06 14:17:47 +02:00
|
|
|
|
2017-05-05 11:44:09 +02:00
|
|
|
func lastNotSpacePos(data []byte) int {
|
|
|
|
for i := len(data) - 1; i >= 0; i-- {
|
|
|
|
if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// UnmarshalFromString convenient method to read from string instead of []byte
|
2017-01-07 06:28:16 +02:00
|
|
|
func UnmarshalFromString(str string, v interface{}) error {
|
2017-06-17 04:21:37 +02:00
|
|
|
return ConfigDefault.UnmarshalFromString(str, v)
|
2017-01-06 14:17:47 +02:00
|
|
|
}
|
2017-01-07 06:28:16 +02:00
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// Get quick method to get value from deeply nested JSON structure
|
2017-06-18 16:21:54 +02:00
|
|
|
func Get(data []byte, path ...interface{}) Any {
|
|
|
|
return ConfigDefault.Get(data, path...)
|
2017-01-22 17:29:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:31:30 +02:00
|
|
|
// Marshal adapts to json/encoding Marshal API
|
2017-06-05 13:14:40 +02:00
|
|
|
//
|
|
|
|
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
2017-06-05 13:57:20 +02:00
|
|
|
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
2017-01-07 06:28:16 +02:00
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
2017-06-17 04:21:37 +02:00
|
|
|
return ConfigDefault.Marshal(v)
|
2017-01-07 06:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
|
2017-06-29 14:48:27 +02:00
|
|
|
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
|
|
|
return ConfigDefault.MarshalIndent(v, prefix, indent)
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// MarshalToString convenient method to write as string instead of []byte
|
2017-01-07 06:28:16 +02:00
|
|
|
func MarshalToString(v interface{}) (string, error) {
|
2017-06-17 04:21:37 +02:00
|
|
|
return ConfigDefault.MarshalToString(v)
|
2017-05-05 11:44:09 +02:00
|
|
|
}
|
2017-06-02 09:38:20 +02:00
|
|
|
|
2017-06-05 13:31:30 +02:00
|
|
|
// NewDecoder adapts to json/stream NewDecoder API.
|
|
|
|
//
|
|
|
|
// NewDecoder returns a new decoder that reads from r.
|
2017-06-05 13:57:20 +02:00
|
|
|
//
|
2017-06-20 02:00:43 +02:00
|
|
|
// Instead of a json/encoding Decoder, an Decoder is returned
|
2017-06-05 13:57:20 +02:00
|
|
|
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
2017-06-20 02:00:43 +02:00
|
|
|
func NewDecoder(reader io.Reader) *Decoder {
|
2017-06-17 04:21:37 +02:00
|
|
|
return ConfigDefault.NewDecoder(reader)
|
2017-06-02 09:38:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 02:00:43 +02:00
|
|
|
// Decoder reads and decodes JSON values from an input stream.
|
|
|
|
// Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
|
|
|
|
type Decoder struct {
|
2017-06-02 09:38:20 +02:00
|
|
|
iter *Iterator
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// Decode decode JSON into interface{}
|
2017-06-20 02:00:43 +02:00
|
|
|
func (adapter *Decoder) Decode(obj interface{}) error {
|
2017-06-02 09:38:20 +02:00
|
|
|
adapter.iter.ReadVal(obj)
|
2017-06-02 10:06:33 +02:00
|
|
|
err := adapter.iter.Error
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-02 09:38:20 +02:00
|
|
|
return adapter.iter.Error
|
2017-06-02 10:00:12 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// More is there more?
|
2017-06-20 02:00:43 +02:00
|
|
|
func (adapter *Decoder) More() bool {
|
2017-06-02 10:00:12 +02:00
|
|
|
return adapter.iter.head != adapter.iter.tail
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// Buffered remaining buffer
|
2017-06-20 02:00:43 +02:00
|
|
|
func (adapter *Decoder) Buffered() io.Reader {
|
2017-06-02 10:00:12 +02:00
|
|
|
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
|
|
|
return bytes.NewReader(remaining)
|
2017-06-02 12:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 01:10:15 +02:00
|
|
|
// UseNumber for number JSON element, use float64 or json.NumberValue (alias of string)
|
2017-07-09 10:26:30 +02:00
|
|
|
func (adapter *Decoder) UseNumber() {
|
|
|
|
origCfg := adapter.iter.cfg.configBeforeFrozen
|
2017-06-18 09:22:37 +02:00
|
|
|
origCfg.UseNumber = true
|
2017-07-09 10:26:30 +02:00
|
|
|
adapter.iter.cfg = origCfg.Froze().(*frozenConfig)
|
2017-06-06 17:15:15 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// NewEncoder same as json.NewEncoder
|
2017-06-20 02:00:43 +02:00
|
|
|
func NewEncoder(writer io.Writer) *Encoder {
|
2017-06-17 04:21:37 +02:00
|
|
|
return ConfigDefault.NewEncoder(writer)
|
2017-06-02 12:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// Encoder same as json.Encoder
|
2017-06-20 02:00:43 +02:00
|
|
|
type Encoder struct {
|
2017-06-02 12:46:44 +02:00
|
|
|
stream *Stream
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// Encode encode interface{} as JSON to io.Writer
|
2017-06-20 02:00:43 +02:00
|
|
|
func (adapter *Encoder) Encode(val interface{}) error {
|
2017-06-02 12:46:44 +02:00
|
|
|
adapter.stream.WriteVal(val)
|
|
|
|
adapter.stream.Flush()
|
|
|
|
return adapter.stream.Error
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// SetIndent set the indention. Prefix is not supported
|
2017-06-20 02:00:43 +02:00
|
|
|
func (adapter *Encoder) SetIndent(prefix, indent string) {
|
2017-06-13 12:49:35 +02:00
|
|
|
adapter.stream.cfg.indentionStep = len(indent)
|
2017-06-06 13:36:33 +02:00
|
|
|
}
|
2017-06-15 18:10:05 +02:00
|
|
|
|
2017-07-09 10:26:30 +02:00
|
|
|
// SetEscapeHTML escape html by default, set to false to disable
|
|
|
|
func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
|
2017-06-15 18:10:05 +02:00
|
|
|
config := adapter.stream.cfg.configBeforeFrozen
|
2017-07-09 10:26:30 +02:00
|
|
|
config.EscapeHTML = escapeHTML
|
2017-07-09 10:09:23 +02:00
|
|
|
adapter.stream.cfg = config.Froze().(*frozenConfig)
|
2017-06-15 18:10:05 +02:00
|
|
|
}
|