You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-18 22:57:33 +02:00
#68 fuzzy all kinds of integer
This commit is contained in:
@ -5,16 +5,16 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxUint = ^uint(0)
|
const MaxUint = ^uint(0)
|
||||||
const MinUint = 0
|
|
||||||
const MaxInt = int(MaxUint >> 1)
|
const MaxInt = int(MaxUint >> 1)
|
||||||
const MinInt = -MaxInt - 1
|
const MinInt = -MaxInt - 1
|
||||||
|
|
||||||
func RegisterFuzzyDecoders() {
|
func RegisterFuzzyDecoders() {
|
||||||
jsoniter.RegisterTypeDecoder("string", &FuzzyStringDecoder{})
|
jsoniter.RegisterTypeDecoder("string", &FuzzyStringDecoder{})
|
||||||
jsoniter.RegisterTypeDecoder("int", &FuzzyNumberDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
jsoniter.RegisterTypeDecoder("int", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
if isFloat {
|
if isFloat {
|
||||||
val := iter.ReadFloat64()
|
val := iter.ReadFloat64()
|
||||||
if val > float64(MaxInt) || val < float64(MinInt) {
|
if val > float64(MaxInt) || val < float64(MinInt) {
|
||||||
@ -26,6 +26,114 @@ func RegisterFuzzyDecoders() {
|
|||||||
*((*int)(ptr)) = iter.ReadInt()
|
*((*int)(ptr)) = iter.ReadInt()
|
||||||
}
|
}
|
||||||
}})
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("uint", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(MaxUint) || val < 0 {
|
||||||
|
errorReporter.ReportError("fuzzy decode uint", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint)(ptr)) = uint(val)
|
||||||
|
} else {
|
||||||
|
*((*uint)(ptr)) = iter.ReadUint()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("int8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
|
||||||
|
errorReporter.ReportError("fuzzy decode int8", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int8)(ptr)) = int8(val)
|
||||||
|
} else {
|
||||||
|
*((*int8)(ptr)) = iter.ReadInt8()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("uint8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxUint8) || val < 0 {
|
||||||
|
errorReporter.ReportError("fuzzy decode uint8", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint8)(ptr)) = uint8(val)
|
||||||
|
} else {
|
||||||
|
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("int16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
|
||||||
|
errorReporter.ReportError("fuzzy decode int16", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int16)(ptr)) = int16(val)
|
||||||
|
} else {
|
||||||
|
*((*int16)(ptr)) = iter.ReadInt16()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("uint16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxUint16) || val < 0 {
|
||||||
|
errorReporter.ReportError("fuzzy decode uint16", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint16)(ptr)) = uint16(val)
|
||||||
|
} else {
|
||||||
|
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("int32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
|
||||||
|
errorReporter.ReportError("fuzzy decode int32", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int32)(ptr)) = int32(val)
|
||||||
|
} else {
|
||||||
|
*((*int32)(ptr)) = iter.ReadInt32()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("uint32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxUint32) || val < 0 {
|
||||||
|
errorReporter.ReportError("fuzzy decode uint32", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint32)(ptr)) = uint32(val)
|
||||||
|
} else {
|
||||||
|
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("int64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
|
||||||
|
errorReporter.ReportError("fuzzy decode int64", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int64)(ptr)) = int64(val)
|
||||||
|
} else {
|
||||||
|
*((*int64)(ptr)) = iter.ReadInt64()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("uint64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(math.MaxUint64) || val < 0 {
|
||||||
|
errorReporter.ReportError("fuzzy decode uint64", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint64)(ptr)) = uint64(val)
|
||||||
|
} else {
|
||||||
|
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||||
|
}
|
||||||
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
type FuzzyStringDecoder struct {
|
type FuzzyStringDecoder struct {
|
||||||
@ -45,11 +153,11 @@ func (decoder *FuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Ite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type FuzzyNumberDecoder struct {
|
type FuzzyIntegerDecoder struct {
|
||||||
fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator)
|
fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator, errorReporter *jsoniter.Iterator)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *FuzzyNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
func (decoder *FuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
valueType := iter.WhatIsNext()
|
valueType := iter.WhatIsNext()
|
||||||
var str string
|
var str string
|
||||||
switch valueType {
|
switch valueType {
|
||||||
@ -60,7 +168,7 @@ func (decoder *FuzzyNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Ite
|
|||||||
case jsoniter.String:
|
case jsoniter.String:
|
||||||
str = iter.ReadString()
|
str = iter.ReadString()
|
||||||
default:
|
default:
|
||||||
iter.ReportError("FuzzyNumberDecoder", "not number or string")
|
iter.ReportError("FuzzyIntegerDecoder", "not number or string")
|
||||||
}
|
}
|
||||||
newIter := iter.Config().BorrowIterator([]byte(str))
|
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||||
defer iter.Config().ReturnIterator(newIter)
|
defer iter.Config().ReturnIterator(newIter)
|
||||||
|
Reference in New Issue
Block a user