1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-26 21:12:40 +02:00
json-iterator/extra/fuzzy_decoder.go

255 lines
7.4 KiB
Go
Raw Normal View History

2017-06-20 15:11:01 +08:00
package extra
import (
"encoding/json"
2017-06-21 00:26:18 +08:00
"github.com/json-iterator/go"
2017-06-20 16:07:30 +08:00
"math"
2017-06-20 16:36:22 +08:00
"reflect"
2017-06-21 00:26:18 +08:00
"strings"
"unsafe"
2017-06-20 15:11:01 +08:00
)
2017-06-20 15:46:22 +08:00
const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
2017-06-20 15:11:01 +08:00
func RegisterFuzzyDecoders() {
2017-06-20 16:36:22 +08:00
jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
2017-06-20 15:11:01 +08:00
jsoniter.RegisterTypeDecoder("string", &FuzzyStringDecoder{})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("float32", &FuzzyFloat32Decoder{})
2017-06-20 16:20:56 +08:00
jsoniter.RegisterTypeDecoder("float64", &FuzzyFloat64Decoder{})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("int", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 15:46:22 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(MaxInt) || val < float64(MinInt) {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode int", "exceed range")
2017-06-20 15:46:22 +08:00
return
}
*((*int)(ptr)) = int(val)
} else {
*((*int)(ptr)) = iter.ReadInt()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("uint", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(MaxUint) || val < 0 {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode uint", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*uint)(ptr)) = uint(val)
} else {
*((*uint)(ptr)) = iter.ReadUint()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("int8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode int8", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*int8)(ptr)) = int8(val)
} else {
*((*int8)(ptr)) = iter.ReadInt8()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("uint8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxUint8) || val < 0 {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode uint8", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*uint8)(ptr)) = uint8(val)
} else {
*((*uint8)(ptr)) = iter.ReadUint8()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("int16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode int16", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*int16)(ptr)) = int16(val)
} else {
*((*int16)(ptr)) = iter.ReadInt16()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("uint16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxUint16) || val < 0 {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode uint16", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*uint16)(ptr)) = uint16(val)
} else {
*((*uint16)(ptr)) = iter.ReadUint16()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("int32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode int32", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*int32)(ptr)) = int32(val)
} else {
*((*int32)(ptr)) = iter.ReadInt32()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("uint32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxUint32) || val < 0 {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode uint32", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*uint32)(ptr)) = uint32(val)
} else {
*((*uint32)(ptr)) = iter.ReadUint32()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("int64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode int64", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*int64)(ptr)) = int64(val)
} else {
*((*int64)(ptr)) = iter.ReadInt64()
}
}})
2017-06-20 16:17:00 +08:00
jsoniter.RegisterTypeDecoder("uint64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:07:30 +08:00
if isFloat {
val := iter.ReadFloat64()
if val > float64(math.MaxUint64) || val < 0 {
2017-06-20 16:17:00 +08:00
iter.ReportError("fuzzy decode uint64", "exceed range")
2017-06-20 16:07:30 +08:00
return
}
*((*uint64)(ptr)) = uint64(val)
} else {
*((*uint64)(ptr)) = iter.ReadUint64()
}
}})
2017-06-20 15:11:01 +08:00
}
2017-06-20 16:36:22 +08:00
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)
}
}
2017-06-20 15:11:01 +08:00
type FuzzyStringDecoder struct {
}
func (decoder *FuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
valueType := iter.WhatIsNext()
switch valueType {
case jsoniter.Number:
var number json.Number
iter.ReadVal(&number)
*((*string)(ptr)) = string(number)
case jsoniter.String:
*((*string)(ptr)) = iter.ReadString()
default:
iter.ReportError("FuzzyStringDecoder", "not number or string")
}
}
2017-06-20 15:18:24 +08:00
2017-06-20 16:07:30 +08:00
type FuzzyIntegerDecoder struct {
2017-06-20 16:17:00 +08:00
fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
2017-06-20 15:18:24 +08:00
}
2017-06-20 16:07:30 +08:00
func (decoder *FuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 15:18:24 +08:00
valueType := iter.WhatIsNext()
2017-06-20 15:46:22 +08:00
var str string
2017-06-20 15:18:24 +08:00
switch valueType {
case jsoniter.Number:
2017-06-20 15:46:22 +08:00
var number json.Number
iter.ReadVal(&number)
str = string(number)
2017-06-20 15:18:24 +08:00
case jsoniter.String:
2017-06-20 15:46:22 +08:00
str = iter.ReadString()
2017-06-20 15:18:24 +08:00
default:
2017-06-20 16:07:30 +08:00
iter.ReportError("FuzzyIntegerDecoder", "not number or string")
2017-06-20 15:18:24 +08:00
}
2017-06-20 15:46:22 +08:00
newIter := iter.Config().BorrowIterator([]byte(str))
defer iter.Config().ReturnIterator(newIter)
isFloat := strings.IndexByte(str, '.') != -1
2017-06-20 16:17:00 +08:00
decoder.fun(isFloat, ptr, newIter)
if newIter.Error != nil {
iter.Error = newIter.Error
}
}
type FuzzyFloat32Decoder struct {
}
func (decoder *FuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
valueType := iter.WhatIsNext()
var str string
switch valueType {
case jsoniter.Number:
*((*float32)(ptr)) = iter.ReadFloat32()
case jsoniter.String:
str = iter.ReadString()
newIter := iter.Config().BorrowIterator([]byte(str))
defer iter.Config().ReturnIterator(newIter)
*((*float32)(ptr)) = newIter.ReadFloat32()
if newIter.Error != nil {
iter.Error = newIter.Error
}
default:
iter.ReportError("FuzzyFloat32Decoder", "not number or string")
}
2017-06-20 15:18:24 +08:00
}
2017-06-20 16:20:56 +08:00
type FuzzyFloat64Decoder struct {
}
func (decoder *FuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
valueType := iter.WhatIsNext()
var str string
switch valueType {
case jsoniter.Number:
*((*float64)(ptr)) = iter.ReadFloat64()
case jsoniter.String:
str = iter.ReadString()
newIter := iter.Config().BorrowIterator([]byte(str))
defer iter.Config().ReturnIterator(newIter)
*((*float64)(ptr)) = newIter.ReadFloat64()
if newIter.Error != nil {
iter.Error = newIter.Error
}
default:
iter.ReportError("FuzzyFloat32Decoder", "not number or string")
}
}