1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-18 22:57:33 +02:00
Files
benchmarks
extra
fuzzy_decoder.go
fuzzy_decoder_test.go
naming_strategy.go
naming_strategy_test.go
privat_fields.go
private_fields_test.go
time_as_int64_codec.go
time_as_int64_codec_test.go
output_tests
skip_tests
.codecov.yml
.gitignore
.travis.yml
Gopkg.lock
Gopkg.toml
LICENSE
README.md
build.sh
compatible_test.go
example_test.go
feature_adapter.go
feature_any.go
feature_any_array.go
feature_any_bool.go
feature_any_float.go
feature_any_int32.go
feature_any_int64.go
feature_any_invalid.go
feature_any_nil.go
feature_any_number.go
feature_any_object.go
feature_any_string.go
feature_any_uint32.go
feature_any_uint64.go
feature_config.go
feature_config_with_sync_map.go
feature_config_without_sync_map.go
feature_iter.go
feature_iter_array.go
feature_iter_float.go
feature_iter_int.go
feature_iter_object.go
feature_iter_skip.go
feature_iter_skip_sloppy.go
feature_iter_skip_strict.go
feature_iter_string.go
feature_json_number.go
feature_pool.go
feature_reflect.go
feature_reflect_array.go
feature_reflect_extension.go
feature_reflect_map.go
feature_reflect_native.go
feature_reflect_object.go
feature_reflect_optional.go
feature_reflect_slice.go
feature_reflect_struct_decoder.go
feature_stream.go
feature_stream_float.go
feature_stream_int.go
feature_stream_string.go
fuzzy_mode_convert_table.md
jsoniter.go
jsoniter_1dot8_only_test.go
jsoniter_adapter_test.go
jsoniter_alias_test.go
jsoniter_any_array_test.go
jsoniter_any_bool_test.go
jsoniter_any_float_test.go
jsoniter_any_int_test.go
jsoniter_any_map_test.go
jsoniter_any_null_test.go
jsoniter_any_object_test.go
jsoniter_any_string_test.go
jsoniter_array_test.go
jsoniter_bool_test.go
jsoniter_customize_test.go
jsoniter_demo_test.go
jsoniter_encode_interface_test.go
jsoniter_enum_marshaler_test.go
jsoniter_fixed_array_test.go
jsoniter_float_test.go
jsoniter_int_test.go
jsoniter_interface_test.go
jsoniter_invalid_test.go
jsoniter_io_test.go
jsoniter_iterator_test.go
jsoniter_large_file_test.go
jsoniter_map_test.go
jsoniter_must_be_valid_test.go
jsoniter_nested_test.go
jsoniter_null_test.go
jsoniter_object_test.go
jsoniter_optional_test.go
jsoniter_raw_message_test.go
jsoniter_reader_test.go
jsoniter_reflect_native_test.go
jsoniter_skip_test.go
jsoniter_sloppy_test.go
jsoniter_stream_test.go
jsoniter_string_test.go
jsoniter_struct_decoder_test.go
jsoniter_struct_encoder_test.go
jsoniter_wrap_test.go
test.sh
unmarshal_input_test.go
json-iterator/extra/fuzzy_decoder.go

279 lines
8.0 KiB
Go
Raw Normal View History

2017-06-20 15:11:01 +08:00
package extra
import (
"encoding/json"
"io"
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"
"github.com/json-iterator/go"
2017-06-20 15:11:01 +08:00
)
2017-07-09 14:21:12 +08:00
const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1)
const minInt = -maxInt - 1
2017-06-20 15:46:22 +08:00
2017-07-09 14:21:12 +08:00
// RegisterFuzzyDecoders decode input from PHP with tolerance.
// It will handle string/number auto conversation, and treat empty [] as empty struct.
2017-06-20 15:11:01 +08:00
func RegisterFuzzyDecoders() {
2017-06-20 16:36:22 +08:00
jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
2017-07-09 14:21:12 +08:00
jsoniter.RegisterTypeDecoder("string", &fuzzyStringDecoder{})
jsoniter.RegisterTypeDecoder("float32", &fuzzyFloat32Decoder{})
jsoniter.RegisterTypeDecoder("float64", &fuzzyFloat64Decoder{})
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()
2017-07-09 14:21:12 +08:00
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-07-09 14:21:12 +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()
2017-07-09 14:21:12 +08:00
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-07-09 14:21:12 +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-07-09 14:21:12 +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-07-09 14:21:12 +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-07-09 14:21:12 +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-07-09 14:21:12 +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-07-09 14:21:12 +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-07-09 14:21:12 +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-07-09 14:21:12 +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.ArrayValue {
2017-06-20 16:36:22 +08:00
iter.Skip()
2017-07-09 16:09:23 +08:00
newIter := iter.Pool().BorrowIterator([]byte("{}"))
defer iter.Pool().ReturnIterator(newIter)
2017-06-20 16:36:22 +08:00
decoder.valDecoder.Decode(ptr, newIter)
} else {
decoder.valDecoder.Decode(ptr, iter)
}
}
2017-07-09 14:21:12 +08:00
type fuzzyStringDecoder struct {
2017-06-20 15:11:01 +08:00
}
2017-07-09 14:21:12 +08:00
func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 15:11:01 +08:00
valueType := iter.WhatIsNext()
switch valueType {
case jsoniter.NumberValue:
2017-06-20 15:11:01 +08:00
var number json.Number
iter.ReadVal(&number)
*((*string)(ptr)) = string(number)
case jsoniter.StringValue:
2017-06-20 15:11:01 +08:00
*((*string)(ptr)) = iter.ReadString()
default:
2017-07-09 14:21:12 +08:00
iter.ReportError("fuzzyStringDecoder", "not number or string")
2017-06-20 15:11:01 +08:00
}
}
2017-06-20 15:18:24 +08:00
2017-07-09 14:21:12 +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-07-09 14:21:12 +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.NumberValue:
2017-06-20 15:46:22 +08:00
var number json.Number
iter.ReadVal(&number)
str = string(number)
case jsoniter.StringValue:
2017-06-20 15:46:22 +08:00
str = iter.ReadString()
case jsoniter.BoolValue:
2017-09-06 13:14:54 +08:00
if iter.ReadBool() {
str = "1"
} else {
str = "0"
}
2017-06-20 15:18:24 +08:00
default:
2017-07-09 14:21:12 +08:00
iter.ReportError("fuzzyIntegerDecoder", "not number or string")
2017-06-20 15:18:24 +08:00
}
2017-07-09 16:09:23 +08:00
newIter := iter.Pool().BorrowIterator([]byte(str))
defer iter.Pool().ReturnIterator(newIter)
2017-06-20 15:46:22 +08:00
isFloat := strings.IndexByte(str, '.') != -1
2017-06-20 16:17:00 +08:00
decoder.fun(isFloat, ptr, newIter)
if newIter.Error != nil && newIter.Error != io.EOF {
2017-06-20 16:17:00 +08:00
iter.Error = newIter.Error
}
}
2017-07-09 14:21:12 +08:00
type fuzzyFloat32Decoder struct {
2017-06-20 16:17:00 +08:00
}
2017-07-09 14:21:12 +08:00
func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:17:00 +08:00
valueType := iter.WhatIsNext()
var str string
switch valueType {
case jsoniter.NumberValue:
2017-06-20 16:17:00 +08:00
*((*float32)(ptr)) = iter.ReadFloat32()
case jsoniter.StringValue:
2017-06-20 16:17:00 +08:00
str = iter.ReadString()
2017-07-09 16:09:23 +08:00
newIter := iter.Pool().BorrowIterator([]byte(str))
defer iter.Pool().ReturnIterator(newIter)
2017-06-20 16:17:00 +08:00
*((*float32)(ptr)) = newIter.ReadFloat32()
if newIter.Error != nil && newIter.Error != io.EOF {
2017-06-20 16:17:00 +08:00
iter.Error = newIter.Error
}
case jsoniter.BoolValue:
// support bool to float32
2017-09-06 13:14:54 +08:00
if iter.ReadBool() {
*((*float32)(ptr)) = 1
} else {
*((*float32)(ptr)) = 0
}
2017-06-20 16:17:00 +08:00
default:
2017-07-09 14:21:12 +08:00
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
2017-06-20 16:17:00 +08:00
}
2017-06-20 15:18:24 +08:00
}
2017-06-20 16:20:56 +08:00
2017-07-09 14:21:12 +08:00
type fuzzyFloat64Decoder struct {
2017-06-20 16:20:56 +08:00
}
2017-07-09 14:21:12 +08:00
func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
2017-06-20 16:20:56 +08:00
valueType := iter.WhatIsNext()
var str string
switch valueType {
case jsoniter.NumberValue:
2017-06-20 16:20:56 +08:00
*((*float64)(ptr)) = iter.ReadFloat64()
case jsoniter.StringValue:
2017-06-20 16:20:56 +08:00
str = iter.ReadString()
2017-07-09 16:09:23 +08:00
newIter := iter.Pool().BorrowIterator([]byte(str))
defer iter.Pool().ReturnIterator(newIter)
2017-06-20 16:20:56 +08:00
*((*float64)(ptr)) = newIter.ReadFloat64()
if newIter.Error != nil && newIter.Error != io.EOF {
2017-06-20 16:20:56 +08:00
iter.Error = newIter.Error
}
case jsoniter.BoolValue:
// support bool to float64
2017-09-06 13:14:54 +08:00
if iter.ReadBool() {
*((*float64)(ptr)) = 1
} else {
*((*float64)(ptr)) = 0
}
2017-06-20 16:20:56 +08:00
default:
2017-07-09 14:21:12 +08:00
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
2017-06-20 16:20:56 +08:00
}
}