2017-06-11 15:32:58 +08:00
|
|
|
// Package jsoniter implements encoding and decoding of JSON as defined in
|
|
|
|
// RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
|
|
|
|
// Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter
|
|
|
|
// and variable type declarations (if any).
|
|
|
|
// jsoniter interfaces gives 100% compatibility with code using standard lib.
|
|
|
|
//
|
|
|
|
// "JSON and Go"
|
|
|
|
// (https://golang.org/doc/articles/json_and_go.html)
|
2017-06-11 16:28:31 +08:00
|
|
|
// gives a description of how Marshal/Unmarshal operate
|
2017-06-11 15:32:58 +08:00
|
|
|
// between arbitrary or predefined json objects and bytes,
|
2017-06-11 16:28:31 +08:00
|
|
|
// and it applies to jsoniter.Marshal/Unmarshal as well.
|
2016-12-04 22:50:53 +08:00
|
|
|
package jsoniter
|
|
|
|
|
2017-01-06 20:17:47 +08:00
|
|
|
import (
|
2017-01-07 12:28:16 +08:00
|
|
|
"bytes"
|
2017-06-06 23:27:00 +08:00
|
|
|
"encoding/json"
|
2017-06-06 19:36:33 +08:00
|
|
|
"io"
|
2017-06-06 23:15:15 +08:00
|
|
|
"unsafe"
|
2017-01-06 20:17:47 +08:00
|
|
|
)
|
2016-12-05 13:20:27 +08:00
|
|
|
|
2017-06-05 19:31:30 +08: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 19:57:20 +08:00
|
|
|
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
2016-12-04 22:50:53 +08:00
|
|
|
func Unmarshal(data []byte, v interface{}) error {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.Unmarshal(data, v)
|
2016-12-04 22:50:53 +08:00
|
|
|
}
|
2017-01-06 20:17:47 +08:00
|
|
|
|
2017-06-11 15:32:58 +08:00
|
|
|
// UnmarshalAny adapts to
|
2017-01-22 23:29:48 +08:00
|
|
|
func UnmarshalAny(data []byte) (Any, error) {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.UnmarshalAny(data)
|
2017-01-22 23:29:48 +08:00
|
|
|
}
|
|
|
|
|
2017-05-05 17:44:09 +08: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-01-07 12:28:16 +08:00
|
|
|
func UnmarshalFromString(str string, v interface{}) error {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.UnmarshalFromString(str, v)
|
2017-01-06 20:17:47 +08:00
|
|
|
}
|
2017-01-07 12:28:16 +08:00
|
|
|
|
2017-01-22 23:29:48 +08:00
|
|
|
func UnmarshalAnyFromString(str string) (Any, error) {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.UnmarshalAnyFromString(str)
|
2017-01-22 23:29:48 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:31:30 +08:00
|
|
|
// Marshal adapts to json/encoding Marshal API
|
2017-06-05 19:14:40 +08:00
|
|
|
//
|
|
|
|
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
2017-06-05 19:57:20 +08:00
|
|
|
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
2017-01-07 12:28:16 +08:00
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.Marshal(v)
|
2017-01-07 12:28:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalToString(v interface{}) (string, error) {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.MarshalToString(v)
|
2017-05-05 17:44:09 +08:00
|
|
|
}
|
2017-06-02 15:38:20 +08:00
|
|
|
|
2017-06-05 19:31:30 +08:00
|
|
|
// NewDecoder adapts to json/stream NewDecoder API.
|
|
|
|
//
|
|
|
|
// NewDecoder returns a new decoder that reads from r.
|
2017-06-05 19:57:20 +08:00
|
|
|
//
|
|
|
|
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
|
|
|
|
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
2017-06-02 15:38:20 +08:00
|
|
|
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.NewDecoder(reader)
|
2017-06-02 15:38:20 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:57:20 +08:00
|
|
|
// AdaptedDecoder reads and decodes JSON values from an input stream.
|
|
|
|
// AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
|
2017-06-02 15:38:20 +08:00
|
|
|
type AdaptedDecoder struct {
|
|
|
|
iter *Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
|
|
|
|
adapter.iter.ReadVal(obj)
|
2017-06-02 16:06:33 +08:00
|
|
|
err := adapter.iter.Error
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-02 15:38:20 +08:00
|
|
|
return adapter.iter.Error
|
2017-06-02 16:00:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedDecoder) More() bool {
|
|
|
|
return adapter.iter.head != adapter.iter.tail
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedDecoder) Buffered() io.Reader {
|
|
|
|
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
|
|
|
return bytes.NewReader(remaining)
|
2017-06-02 18:46:44 +08:00
|
|
|
}
|
|
|
|
|
2017-06-06 23:15:15 +08:00
|
|
|
func (decoder *AdaptedDecoder) UseNumber() {
|
|
|
|
RegisterTypeDecoder("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
|
|
|
|
if iter.WhatIsNext() == Number {
|
|
|
|
*((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
|
|
|
|
} else {
|
|
|
|
*((*interface{})(ptr)) = iter.Read()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-02 18:46:44 +08:00
|
|
|
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
2017-06-17 10:21:37 +08:00
|
|
|
return ConfigDefault.NewEncoder(writer)
|
2017-06-02 18:46:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type AdaptedEncoder struct {
|
|
|
|
stream *Stream
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedEncoder) Encode(val interface{}) error {
|
|
|
|
adapter.stream.WriteVal(val)
|
|
|
|
adapter.stream.Flush()
|
|
|
|
return adapter.stream.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
|
2017-06-13 18:49:35 +08:00
|
|
|
adapter.stream.cfg.indentionStep = len(indent)
|
2017-06-06 19:36:33 +08:00
|
|
|
}
|
2017-06-16 00:10:05 +08:00
|
|
|
|
|
|
|
func (adapter *AdaptedEncoder) SetEscapeHTML(escapeHtml bool) {
|
|
|
|
config := adapter.stream.cfg.configBeforeFrozen
|
|
|
|
config.EscapeHtml = escapeHtml
|
|
|
|
adapter.stream.cfg = config.Froze()
|
|
|
|
}
|