123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package responses
- import (
- "encoding/json"
- "io"
- "math"
- "strconv"
- "strings"
- "unsafe"
- jsoniter "github.com/json-iterator/go"
- )
- const maxUint = ^uint(0)
- const maxInt = int(maxUint >> 1)
- const minInt = -maxInt - 1
- var jsonParser jsoniter.API
- func init() {
- registerBetterFuzzyDecoder()
- jsonParser = jsoniter.Config{
- EscapeHTML: true,
- SortMapKeys: true,
- ValidateJsonRawMessage: true,
- CaseSensitive: true,
- }.Froze()
- }
- func registerBetterFuzzyDecoder() {
- jsoniter.RegisterTypeDecoder("string", &nullableFuzzyStringDecoder{})
- jsoniter.RegisterTypeDecoder("bool", &fuzzyBoolDecoder{})
- jsoniter.RegisterTypeDecoder("float32", &nullableFuzzyFloat32Decoder{})
- jsoniter.RegisterTypeDecoder("float64", &nullableFuzzyFloat64Decoder{})
- jsoniter.RegisterTypeDecoder("int", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(maxInt) || val < float64(minInt) {
- iter.ReportError("fuzzy decode int", "exceed range")
- return
- }
- *((*int)(ptr)) = int(val)
- } else {
- *((*int)(ptr)) = iter.ReadInt()
- }
- }})
- jsoniter.RegisterTypeDecoder("uint", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(maxUint) || val < 0 {
- iter.ReportError("fuzzy decode uint", "exceed range")
- return
- }
- *((*uint)(ptr)) = uint(val)
- } else {
- *((*uint)(ptr)) = iter.ReadUint()
- }
- }})
- jsoniter.RegisterTypeDecoder("int8", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
- iter.ReportError("fuzzy decode int8", "exceed range")
- return
- }
- *((*int8)(ptr)) = int8(val)
- } else {
- *((*int8)(ptr)) = iter.ReadInt8()
- }
- }})
- jsoniter.RegisterTypeDecoder("uint8", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxUint8) || val < 0 {
- iter.ReportError("fuzzy decode uint8", "exceed range")
- return
- }
- *((*uint8)(ptr)) = uint8(val)
- } else {
- *((*uint8)(ptr)) = iter.ReadUint8()
- }
- }})
- jsoniter.RegisterTypeDecoder("int16", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
- iter.ReportError("fuzzy decode int16", "exceed range")
- return
- }
- *((*int16)(ptr)) = int16(val)
- } else {
- *((*int16)(ptr)) = iter.ReadInt16()
- }
- }})
- jsoniter.RegisterTypeDecoder("uint16", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxUint16) || val < 0 {
- iter.ReportError("fuzzy decode uint16", "exceed range")
- return
- }
- *((*uint16)(ptr)) = uint16(val)
- } else {
- *((*uint16)(ptr)) = iter.ReadUint16()
- }
- }})
- jsoniter.RegisterTypeDecoder("int32", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
- iter.ReportError("fuzzy decode int32", "exceed range")
- return
- }
- *((*int32)(ptr)) = int32(val)
- } else {
- *((*int32)(ptr)) = iter.ReadInt32()
- }
- }})
- jsoniter.RegisterTypeDecoder("uint32", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxUint32) || val < 0 {
- iter.ReportError("fuzzy decode uint32", "exceed range")
- return
- }
- *((*uint32)(ptr)) = uint32(val)
- } else {
- *((*uint32)(ptr)) = iter.ReadUint32()
- }
- }})
- jsoniter.RegisterTypeDecoder("int64", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
- iter.ReportError("fuzzy decode int64", "exceed range")
- return
- }
- *((*int64)(ptr)) = int64(val)
- } else {
- *((*int64)(ptr)) = iter.ReadInt64()
- }
- }})
- jsoniter.RegisterTypeDecoder("uint64", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- if isFloat {
- val := iter.ReadFloat64()
- if val > float64(math.MaxUint64) || val < 0 {
- iter.ReportError("fuzzy decode uint64", "exceed range")
- return
- }
- *((*uint64)(ptr)) = uint64(val)
- } else {
- *((*uint64)(ptr)) = iter.ReadUint64()
- }
- }})
- }
- type nullableFuzzyStringDecoder struct {
- }
- func (decoder *nullableFuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- valueType := iter.WhatIsNext()
- switch valueType {
- case jsoniter.NumberValue:
- var number json.Number
- iter.ReadVal(&number)
- *((*string)(ptr)) = string(number)
- case jsoniter.StringValue:
- *((*string)(ptr)) = iter.ReadString()
- case jsoniter.BoolValue:
- *((*string)(ptr)) = strconv.FormatBool(iter.ReadBool())
- case jsoniter.NilValue:
- iter.ReadNil()
- *((*string)(ptr)) = ""
- default:
- iter.ReportError("fuzzyStringDecoder", "not number or string or bool")
- }
- }
- type fuzzyBoolDecoder struct {
- }
- func (decoder *fuzzyBoolDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- valueType := iter.WhatIsNext()
- switch valueType {
- case jsoniter.BoolValue:
- *((*bool)(ptr)) = iter.ReadBool()
- case jsoniter.NumberValue:
- var number json.Number
- iter.ReadVal(&number)
- num, err := number.Int64()
- if err != nil {
- iter.ReportError("fuzzyBoolDecoder", "get value from json.number failed")
- }
- if num == 0 {
- *((*bool)(ptr)) = false
- } else {
- *((*bool)(ptr)) = true
- }
- case jsoniter.StringValue:
- strValue := strings.ToLower(iter.ReadString())
- if strValue == "true" {
- *((*bool)(ptr)) = true
- } else if strValue == "false" || strValue == "" {
- *((*bool)(ptr)) = false
- } else {
- iter.ReportError("fuzzyBoolDecoder", "unsupported bool value: "+strValue)
- }
- case jsoniter.NilValue:
- iter.ReadNil()
- *((*bool)(ptr)) = false
- default:
- iter.ReportError("fuzzyBoolDecoder", "not number or string or nil")
- }
- }
- type nullableFuzzyIntegerDecoder struct {
- fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
- }
- func (decoder *nullableFuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- valueType := iter.WhatIsNext()
- var str string
- switch valueType {
- case jsoniter.NumberValue:
- var number json.Number
- iter.ReadVal(&number)
- str = string(number)
- case jsoniter.StringValue:
- str = iter.ReadString()
- // support empty string
- if str == "" {
- str = "0"
- }
- case jsoniter.BoolValue:
- if iter.ReadBool() {
- str = "1"
- } else {
- str = "0"
- }
- case jsoniter.NilValue:
- iter.ReadNil()
- str = "0"
- default:
- iter.ReportError("fuzzyIntegerDecoder", "not number or string")
- }
- newIter := iter.Pool().BorrowIterator([]byte(str))
- defer iter.Pool().ReturnIterator(newIter)
- isFloat := strings.IndexByte(str, '.') != -1
- decoder.fun(isFloat, ptr, newIter)
- if newIter.Error != nil && newIter.Error != io.EOF {
- iter.Error = newIter.Error
- }
- }
- type nullableFuzzyFloat32Decoder struct {
- }
- func (decoder *nullableFuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- valueType := iter.WhatIsNext()
- var str string
- switch valueType {
- case jsoniter.NumberValue:
- *((*float32)(ptr)) = iter.ReadFloat32()
- case jsoniter.StringValue:
- str = iter.ReadString()
- // support empty string
- if str == "" {
- *((*float32)(ptr)) = 0
- return
- }
- newIter := iter.Pool().BorrowIterator([]byte(str))
- defer iter.Pool().ReturnIterator(newIter)
- *((*float32)(ptr)) = newIter.ReadFloat32()
- if newIter.Error != nil && newIter.Error != io.EOF {
- iter.Error = newIter.Error
- }
- case jsoniter.BoolValue:
- // support bool to float32
- if iter.ReadBool() {
- *((*float32)(ptr)) = 1
- } else {
- *((*float32)(ptr)) = 0
- }
- case jsoniter.NilValue:
- iter.ReadNil()
- *((*float32)(ptr)) = 0
- default:
- iter.ReportError("nullableFuzzyFloat32Decoder", "not number or string")
- }
- }
- type nullableFuzzyFloat64Decoder struct {
- }
- func (decoder *nullableFuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
- valueType := iter.WhatIsNext()
- var str string
- switch valueType {
- case jsoniter.NumberValue:
- *((*float64)(ptr)) = iter.ReadFloat64()
- case jsoniter.StringValue:
- str = iter.ReadString()
- // support empty string
- if str == "" {
- *((*float64)(ptr)) = 0
- return
- }
- newIter := iter.Pool().BorrowIterator([]byte(str))
- defer iter.Pool().ReturnIterator(newIter)
- *((*float64)(ptr)) = newIter.ReadFloat64()
- if newIter.Error != nil && newIter.Error != io.EOF {
- iter.Error = newIter.Error
- }
- case jsoniter.BoolValue:
- // support bool to float64
- if iter.ReadBool() {
- *((*float64)(ptr)) = 1
- } else {
- *((*float64)(ptr)) = 0
- }
- case jsoniter.NilValue:
- // support empty string
- iter.ReadNil()
- *((*float64)(ptr)) = 0
- default:
- iter.ReportError("nullableFuzzyFloat64Decoder", "not number or string")
- }
- }
|