1
0
mirror of https://github.com/json-iterator/go.git synced 2025-12-02 22:48:20 +02:00

#68 empty array to object/map

This commit is contained in:
Tao Wen
2017-06-20 16:36:22 +08:00
parent 086001225d
commit 85be06b145
4 changed files with 84 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"strings"
"math"
"reflect"
)
const MaxUint = ^uint(0)
@@ -13,6 +14,7 @@ const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
func RegisterFuzzyDecoders() {
jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
jsoniter.RegisterTypeDecoder("string", &FuzzyStringDecoder{})
jsoniter.RegisterTypeDecoder("float32", &FuzzyFloat32Decoder{})
jsoniter.RegisterTypeDecoder("float64", &FuzzyFloat64Decoder{})
@@ -138,6 +140,32 @@ func RegisterFuzzyDecoders() {
}})
}
type tolerateEmptyArrayExtension struct {
jsoniter.DummyExtension
}
func (extension *tolerateEmptyArrayExtension) DecorateDecoder(typ reflect.Type, decoder jsoniter.ValDecoder) jsoniter.ValDecoder {
if typ.Kind() == reflect.Struct || typ.Kind() == reflect.Map {
return &tolerateEmptyArrayDecoder{decoder}
}
return decoder
}
type tolerateEmptyArrayDecoder struct {
valDecoder jsoniter.ValDecoder
}
func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
if iter.WhatIsNext() == jsoniter.Array {
iter.Skip()
newIter := iter.Config().BorrowIterator([]byte("{}"))
defer iter.Config().ReturnIterator(newIter)
decoder.valDecoder.Decode(ptr, newIter)
} else {
decoder.valDecoder.Decode(ptr, iter)
}
}
type FuzzyStringDecoder struct {
}