2017-06-13 10:58:53 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-06-19 17:43:53 +02:00
|
|
|
"encoding/json"
|
2017-06-17 15:11:23 +02:00
|
|
|
"errors"
|
2017-06-13 11:47:40 +02:00
|
|
|
"io"
|
2017-06-13 10:58:53 +02:00
|
|
|
"reflect"
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2017-06-13 11:47:40 +02:00
|
|
|
IndentionStep int
|
|
|
|
MarshalFloatWith6Digits bool
|
|
|
|
SupportUnexportedStructFields bool
|
2017-06-16 10:46:30 +02:00
|
|
|
EscapeHtml bool
|
|
|
|
SortMapKeys bool
|
2017-06-18 09:22:37 +02:00
|
|
|
UseNumber bool
|
2017-06-13 12:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type frozenConfig struct {
|
2017-06-16 10:46:30 +02:00
|
|
|
configBeforeFrozen Config
|
|
|
|
sortMapKeys bool
|
|
|
|
indentionStep int
|
|
|
|
decoderCache unsafe.Pointer
|
|
|
|
encoderCache unsafe.Pointer
|
|
|
|
extensions []ExtensionFunc
|
2017-06-17 08:23:02 +02:00
|
|
|
streamPool chan *Stream
|
|
|
|
iteratorPool chan *Iterator
|
2017-06-13 10:58:53 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 07:43:22 +02:00
|
|
|
type Api interface {
|
|
|
|
MarshalToString(v interface{}) (string, error)
|
|
|
|
Marshal(v interface{}) ([]byte, error)
|
|
|
|
UnmarshalFromString(str string, v interface{}) error
|
|
|
|
Unmarshal(data []byte, v interface{}) error
|
|
|
|
Get(data []byte, path ...interface{}) Any
|
|
|
|
NewEncoder(writer io.Writer) *AdaptedEncoder
|
|
|
|
NewDecoder(reader io.Reader) *AdaptedDecoder
|
|
|
|
}
|
|
|
|
|
2017-06-18 09:22:37 +02:00
|
|
|
var ConfigDefault = Config{
|
|
|
|
EscapeHtml: true,
|
|
|
|
}.Froze()
|
2017-06-15 17:54:43 +02:00
|
|
|
|
|
|
|
// Trying to be 100% compatible with standard library behavior
|
|
|
|
var ConfigCompatibleWithStandardLibrary = Config{
|
2017-06-16 10:46:30 +02:00
|
|
|
EscapeHtml: true,
|
|
|
|
SortMapKeys: true,
|
2017-06-15 17:54:43 +02:00
|
|
|
}.Froze()
|
2017-06-13 10:58:53 +02:00
|
|
|
|
2017-06-17 04:21:37 +02:00
|
|
|
var ConfigFastest = Config{
|
|
|
|
MarshalFloatWith6Digits: true,
|
|
|
|
}.Froze()
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg Config) Froze() *frozenConfig {
|
|
|
|
frozenConfig := &frozenConfig{
|
2017-06-17 04:21:37 +02:00
|
|
|
sortMapKeys: cfg.SortMapKeys,
|
2017-06-13 12:49:35 +02:00
|
|
|
indentionStep: cfg.IndentionStep,
|
2017-06-17 08:23:02 +02:00
|
|
|
streamPool: make(chan *Stream, 16),
|
|
|
|
iteratorPool: make(chan *Iterator, 16),
|
2017-06-13 12:49:35 +02:00
|
|
|
}
|
|
|
|
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
|
|
|
|
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
|
|
|
if cfg.MarshalFloatWith6Digits {
|
|
|
|
frozenConfig.marshalFloatWith6Digits()
|
|
|
|
}
|
|
|
|
if cfg.SupportUnexportedStructFields {
|
|
|
|
frozenConfig.supportUnexportedStructFields()
|
2017-06-13 11:47:40 +02:00
|
|
|
}
|
2017-06-15 17:54:43 +02:00
|
|
|
if cfg.EscapeHtml {
|
|
|
|
frozenConfig.escapeHtml()
|
|
|
|
}
|
2017-06-18 09:22:37 +02:00
|
|
|
if cfg.UseNumber {
|
|
|
|
frozenConfig.useNumber()
|
|
|
|
}
|
2017-06-15 18:10:05 +02:00
|
|
|
frozenConfig.configBeforeFrozen = cfg
|
2017-06-13 12:49:35 +02:00
|
|
|
return frozenConfig
|
2017-06-13 11:47:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 09:22:37 +02:00
|
|
|
func (cfg *frozenConfig) useNumber() {
|
|
|
|
cfg.addDecoderToCache(reflect.TypeOf((*interface{})(nil)).Elem(), &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
|
|
|
|
if iter.WhatIsNext() == Number {
|
|
|
|
*((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
|
|
|
|
} else {
|
|
|
|
*((*interface{})(ptr)) = iter.Read()
|
|
|
|
}
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
2017-06-13 11:47:40 +02:00
|
|
|
// RegisterExtension can register a custom extension
|
2017-06-15 17:54:43 +02:00
|
|
|
func (cfg *frozenConfig) registerExtension(extension ExtensionFunc) {
|
2017-06-13 11:47:40 +02:00
|
|
|
cfg.extensions = append(cfg.extensions, extension)
|
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) supportUnexportedStructFields() {
|
2017-06-15 17:54:43 +02:00
|
|
|
cfg.registerExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
|
2017-06-13 11:47:40 +02:00
|
|
|
return []string{field.Name}, nil, nil
|
|
|
|
})
|
2017-06-13 10:58:53 +02:00
|
|
|
}
|
2017-06-13 11:47:40 +02:00
|
|
|
|
|
|
|
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
|
|
|
// for float variables for better performance.
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) marshalFloatWith6Digits() {
|
2017-06-13 11:47:40 +02:00
|
|
|
// for better performance
|
|
|
|
cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
val := *((*float32)(ptr))
|
|
|
|
stream.WriteFloat32Lossy(val)
|
|
|
|
}})
|
|
|
|
cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
val := *((*float64)(ptr))
|
|
|
|
stream.WriteFloat64Lossy(val)
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
2017-06-20 01:46:13 +02:00
|
|
|
type htmlEscapedStringEncoder struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *htmlEscapedStringEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
str := *((*string)(ptr))
|
|
|
|
stream.WriteStringWithHtmlEscaped(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *htmlEscapedStringEncoder) encodeInterface(val interface{}, stream *Stream) {
|
|
|
|
writeToStream(val, stream, encoder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *htmlEscapedStringEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return *((*string)(ptr)) == ""
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:54:43 +02:00
|
|
|
func (cfg *frozenConfig) escapeHtml() {
|
|
|
|
// for better performance
|
2017-06-20 01:46:13 +02:00
|
|
|
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &htmlEscapedStringEncoder{})
|
2017-06-15 17:54:43 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
2017-06-13 10:58:53 +02:00
|
|
|
done := false
|
|
|
|
for !done {
|
|
|
|
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
|
|
|
cache := *(*map[reflect.Type]Decoder)(ptr)
|
|
|
|
copied := map[reflect.Type]Decoder{}
|
|
|
|
for k, v := range cache {
|
|
|
|
copied[k] = v
|
|
|
|
}
|
|
|
|
copied[cacheKey] = decoder
|
|
|
|
done = atomic.CompareAndSwapPointer(&cfg.decoderCache, ptr, unsafe.Pointer(&copied))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
|
2017-06-13 10:58:53 +02:00
|
|
|
done := false
|
|
|
|
for !done {
|
|
|
|
ptr := atomic.LoadPointer(&cfg.encoderCache)
|
|
|
|
cache := *(*map[reflect.Type]Encoder)(ptr)
|
|
|
|
copied := map[reflect.Type]Encoder{}
|
|
|
|
for k, v := range cache {
|
|
|
|
copied[k] = v
|
|
|
|
}
|
|
|
|
copied[cacheKey] = encoder
|
|
|
|
done = atomic.CompareAndSwapPointer(&cfg.encoderCache, ptr, unsafe.Pointer(&copied))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) Decoder {
|
2017-06-13 10:58:53 +02:00
|
|
|
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
|
|
|
cache := *(*map[reflect.Type]Decoder)(ptr)
|
|
|
|
return cache[cacheKey]
|
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) Encoder {
|
2017-06-13 10:58:53 +02:00
|
|
|
ptr := atomic.LoadPointer(&cfg.encoderCache)
|
|
|
|
cache := *(*map[reflect.Type]Encoder)(ptr)
|
|
|
|
return cache[cacheKey]
|
|
|
|
}
|
|
|
|
|
2017-06-19 07:43:22 +02:00
|
|
|
// cleanDecoders cleans decoders registered or cached
|
|
|
|
func (cfg *frozenConfig) cleanDecoders() {
|
2017-06-13 10:58:53 +02:00
|
|
|
typeDecoders = map[string]Decoder{}
|
|
|
|
fieldDecoders = map[string]Decoder{}
|
|
|
|
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
|
|
|
|
}
|
|
|
|
|
2017-06-19 07:43:22 +02:00
|
|
|
// cleanEncoders cleans encoders registered or cached
|
|
|
|
func (cfg *frozenConfig) cleanEncoders() {
|
2017-06-13 10:58:53 +02:00
|
|
|
typeEncoders = map[string]Encoder{}
|
|
|
|
fieldEncoders = map[string]Encoder{}
|
|
|
|
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
|
|
|
}
|
2017-06-13 11:47:40 +02:00
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
|
2017-06-17 11:14:34 +02:00
|
|
|
stream := cfg.BorrowStream(nil)
|
|
|
|
defer cfg.ReturnStream(stream)
|
2017-06-17 08:36:05 +02:00
|
|
|
stream.WriteVal(v)
|
|
|
|
if stream.Error != nil {
|
2017-06-17 08:36:38 +02:00
|
|
|
return "", stream.Error
|
2017-06-13 11:47:40 +02:00
|
|
|
}
|
2017-06-17 08:36:05 +02:00
|
|
|
return string(stream.Buffer()), nil
|
2017-06-13 11:47:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
2017-06-17 11:14:34 +02:00
|
|
|
stream := cfg.BorrowStream(nil)
|
|
|
|
defer cfg.ReturnStream(stream)
|
2017-06-13 11:47:40 +02:00
|
|
|
stream.WriteVal(v)
|
|
|
|
if stream.Error != nil {
|
|
|
|
return nil, stream.Error
|
|
|
|
}
|
2017-06-17 08:36:05 +02:00
|
|
|
result := stream.Buffer()
|
|
|
|
copied := make([]byte, len(result))
|
|
|
|
copy(copied, result)
|
|
|
|
return copied, nil
|
2017-06-13 11:47:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:35 +02:00
|
|
|
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
2017-06-13 11:47:40 +02:00
|
|
|
data := []byte(str)
|
|
|
|
data = data[:lastNotSpacePos(data)]
|
2017-06-17 11:14:34 +02:00
|
|
|
iter := cfg.BorrowIterator(data)
|
|
|
|
defer cfg.ReturnIterator(iter)
|
2017-06-13 11:47:40 +02:00
|
|
|
iter.ReadVal(v)
|
|
|
|
if iter.head == iter.tail {
|
|
|
|
iter.loadMore()
|
|
|
|
}
|
|
|
|
if iter.Error == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if iter.Error == nil {
|
|
|
|
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
|
|
|
}
|
|
|
|
return iter.Error
|
|
|
|
}
|
2017-06-15 18:10:05 +02:00
|
|
|
|
2017-06-18 16:21:54 +02:00
|
|
|
func (cfg *frozenConfig) Get(data []byte, path ...interface{}) Any {
|
2017-06-17 11:14:34 +02:00
|
|
|
iter := cfg.BorrowIterator(data)
|
|
|
|
defer cfg.ReturnIterator(iter)
|
2017-06-18 16:21:54 +02:00
|
|
|
return locatePath(iter, path)
|
2017-06-15 18:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
|
|
|
|
data = data[:lastNotSpacePos(data)]
|
2017-06-17 11:14:34 +02:00
|
|
|
iter := cfg.BorrowIterator(data)
|
|
|
|
defer cfg.ReturnIterator(iter)
|
2017-06-15 18:10:05 +02:00
|
|
|
typ := reflect.TypeOf(v)
|
|
|
|
if typ.Kind() != reflect.Ptr {
|
|
|
|
// return non-pointer error
|
|
|
|
return errors.New("the second param must be ptr type")
|
|
|
|
}
|
|
|
|
iter.ReadVal(v)
|
|
|
|
if iter.head == iter.tail {
|
|
|
|
iter.loadMore()
|
|
|
|
}
|
|
|
|
if iter.Error == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if iter.Error == nil {
|
|
|
|
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
|
|
|
|
}
|
|
|
|
return iter.Error
|
2017-06-16 10:46:30 +02:00
|
|
|
}
|
2017-06-17 08:23:02 +02:00
|
|
|
|
|
|
|
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *AdaptedEncoder {
|
|
|
|
stream := NewStream(cfg, writer, 512)
|
|
|
|
return &AdaptedEncoder{stream}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *AdaptedDecoder {
|
|
|
|
iter := Parse(cfg, reader, 512)
|
|
|
|
return &AdaptedDecoder{iter}
|
|
|
|
}
|