fuzzy_decoder.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package extra
  2. import (
  3. "encoding/json"
  4. "io"
  5. "math"
  6. "reflect"
  7. "strings"
  8. "unsafe"
  9. "github.com/json-iterator/go"
  10. )
  11. const maxUint = ^uint(0)
  12. const maxInt = int(maxUint >> 1)
  13. const minInt = -maxInt - 1
  14. // RegisterFuzzyDecoders decode input from PHP with tolerance.
  15. // It will handle string/number auto conversation, and treat empty [] as empty struct.
  16. func RegisterFuzzyDecoders() {
  17. jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
  18. jsoniter.RegisterTypeDecoder("string", &fuzzyStringDecoder{})
  19. jsoniter.RegisterTypeDecoder("float32", &fuzzyFloat32Decoder{})
  20. jsoniter.RegisterTypeDecoder("float64", &fuzzyFloat64Decoder{})
  21. jsoniter.RegisterTypeDecoder("int", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  22. if isFloat {
  23. val := iter.ReadFloat64()
  24. if val > float64(maxInt) || val < float64(minInt) {
  25. iter.ReportError("fuzzy decode int", "exceed range")
  26. return
  27. }
  28. *((*int)(ptr)) = int(val)
  29. } else {
  30. *((*int)(ptr)) = iter.ReadInt()
  31. }
  32. }})
  33. jsoniter.RegisterTypeDecoder("uint", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  34. if isFloat {
  35. val := iter.ReadFloat64()
  36. if val > float64(maxUint) || val < 0 {
  37. iter.ReportError("fuzzy decode uint", "exceed range")
  38. return
  39. }
  40. *((*uint)(ptr)) = uint(val)
  41. } else {
  42. *((*uint)(ptr)) = iter.ReadUint()
  43. }
  44. }})
  45. jsoniter.RegisterTypeDecoder("int8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  46. if isFloat {
  47. val := iter.ReadFloat64()
  48. if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
  49. iter.ReportError("fuzzy decode int8", "exceed range")
  50. return
  51. }
  52. *((*int8)(ptr)) = int8(val)
  53. } else {
  54. *((*int8)(ptr)) = iter.ReadInt8()
  55. }
  56. }})
  57. jsoniter.RegisterTypeDecoder("uint8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  58. if isFloat {
  59. val := iter.ReadFloat64()
  60. if val > float64(math.MaxUint8) || val < 0 {
  61. iter.ReportError("fuzzy decode uint8", "exceed range")
  62. return
  63. }
  64. *((*uint8)(ptr)) = uint8(val)
  65. } else {
  66. *((*uint8)(ptr)) = iter.ReadUint8()
  67. }
  68. }})
  69. jsoniter.RegisterTypeDecoder("int16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  70. if isFloat {
  71. val := iter.ReadFloat64()
  72. if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
  73. iter.ReportError("fuzzy decode int16", "exceed range")
  74. return
  75. }
  76. *((*int16)(ptr)) = int16(val)
  77. } else {
  78. *((*int16)(ptr)) = iter.ReadInt16()
  79. }
  80. }})
  81. jsoniter.RegisterTypeDecoder("uint16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  82. if isFloat {
  83. val := iter.ReadFloat64()
  84. if val > float64(math.MaxUint16) || val < 0 {
  85. iter.ReportError("fuzzy decode uint16", "exceed range")
  86. return
  87. }
  88. *((*uint16)(ptr)) = uint16(val)
  89. } else {
  90. *((*uint16)(ptr)) = iter.ReadUint16()
  91. }
  92. }})
  93. jsoniter.RegisterTypeDecoder("int32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  94. if isFloat {
  95. val := iter.ReadFloat64()
  96. if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
  97. iter.ReportError("fuzzy decode int32", "exceed range")
  98. return
  99. }
  100. *((*int32)(ptr)) = int32(val)
  101. } else {
  102. *((*int32)(ptr)) = iter.ReadInt32()
  103. }
  104. }})
  105. jsoniter.RegisterTypeDecoder("uint32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  106. if isFloat {
  107. val := iter.ReadFloat64()
  108. if val > float64(math.MaxUint32) || val < 0 {
  109. iter.ReportError("fuzzy decode uint32", "exceed range")
  110. return
  111. }
  112. *((*uint32)(ptr)) = uint32(val)
  113. } else {
  114. *((*uint32)(ptr)) = iter.ReadUint32()
  115. }
  116. }})
  117. jsoniter.RegisterTypeDecoder("int64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  118. if isFloat {
  119. val := iter.ReadFloat64()
  120. if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
  121. iter.ReportError("fuzzy decode int64", "exceed range")
  122. return
  123. }
  124. *((*int64)(ptr)) = int64(val)
  125. } else {
  126. *((*int64)(ptr)) = iter.ReadInt64()
  127. }
  128. }})
  129. jsoniter.RegisterTypeDecoder("uint64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  130. if isFloat {
  131. val := iter.ReadFloat64()
  132. if val > float64(math.MaxUint64) || val < 0 {
  133. iter.ReportError("fuzzy decode uint64", "exceed range")
  134. return
  135. }
  136. *((*uint64)(ptr)) = uint64(val)
  137. } else {
  138. *((*uint64)(ptr)) = iter.ReadUint64()
  139. }
  140. }})
  141. }
  142. type tolerateEmptyArrayExtension struct {
  143. jsoniter.DummyExtension
  144. }
  145. func (extension *tolerateEmptyArrayExtension) DecorateDecoder(typ reflect.Type, decoder jsoniter.ValDecoder) jsoniter.ValDecoder {
  146. if typ.Kind() == reflect.Struct || typ.Kind() == reflect.Map {
  147. return &tolerateEmptyArrayDecoder{decoder}
  148. }
  149. return decoder
  150. }
  151. type tolerateEmptyArrayDecoder struct {
  152. valDecoder jsoniter.ValDecoder
  153. }
  154. func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  155. if iter.WhatIsNext() == jsoniter.ArrayValue {
  156. iter.Skip()
  157. newIter := iter.Pool().BorrowIterator([]byte("{}"))
  158. defer iter.Pool().ReturnIterator(newIter)
  159. decoder.valDecoder.Decode(ptr, newIter)
  160. } else {
  161. decoder.valDecoder.Decode(ptr, iter)
  162. }
  163. }
  164. type fuzzyStringDecoder struct {
  165. }
  166. func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  167. valueType := iter.WhatIsNext()
  168. switch valueType {
  169. case jsoniter.NumberValue:
  170. var number json.Number
  171. iter.ReadVal(&number)
  172. *((*string)(ptr)) = string(number)
  173. case jsoniter.StringValue:
  174. *((*string)(ptr)) = iter.ReadString()
  175. default:
  176. iter.ReportError("fuzzyStringDecoder", "not number or string")
  177. }
  178. }
  179. type fuzzyIntegerDecoder struct {
  180. fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
  181. }
  182. func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  183. valueType := iter.WhatIsNext()
  184. var str string
  185. switch valueType {
  186. case jsoniter.NumberValue:
  187. var number json.Number
  188. iter.ReadVal(&number)
  189. str = string(number)
  190. case jsoniter.StringValue:
  191. str = iter.ReadString()
  192. case jsoniter.BoolValue:
  193. if iter.ReadBool() {
  194. str = "1"
  195. } else {
  196. str = "0"
  197. }
  198. default:
  199. iter.ReportError("fuzzyIntegerDecoder", "not number or string")
  200. }
  201. newIter := iter.Pool().BorrowIterator([]byte(str))
  202. defer iter.Pool().ReturnIterator(newIter)
  203. isFloat := strings.IndexByte(str, '.') != -1
  204. decoder.fun(isFloat, ptr, newIter)
  205. if newIter.Error != nil && newIter.Error != io.EOF {
  206. iter.Error = newIter.Error
  207. }
  208. }
  209. type fuzzyFloat32Decoder struct {
  210. }
  211. func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  212. valueType := iter.WhatIsNext()
  213. var str string
  214. switch valueType {
  215. case jsoniter.NumberValue:
  216. *((*float32)(ptr)) = iter.ReadFloat32()
  217. case jsoniter.StringValue:
  218. str = iter.ReadString()
  219. newIter := iter.Pool().BorrowIterator([]byte(str))
  220. defer iter.Pool().ReturnIterator(newIter)
  221. *((*float32)(ptr)) = newIter.ReadFloat32()
  222. if newIter.Error != nil && newIter.Error != io.EOF {
  223. iter.Error = newIter.Error
  224. }
  225. case jsoniter.BoolValue:
  226. // support bool to float32
  227. if iter.ReadBool() {
  228. *((*float32)(ptr)) = 1
  229. } else {
  230. *((*float32)(ptr)) = 0
  231. }
  232. default:
  233. iter.ReportError("fuzzyFloat32Decoder", "not number or string")
  234. }
  235. }
  236. type fuzzyFloat64Decoder struct {
  237. }
  238. func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  239. valueType := iter.WhatIsNext()
  240. var str string
  241. switch valueType {
  242. case jsoniter.NumberValue:
  243. *((*float64)(ptr)) = iter.ReadFloat64()
  244. case jsoniter.StringValue:
  245. str = iter.ReadString()
  246. newIter := iter.Pool().BorrowIterator([]byte(str))
  247. defer iter.Pool().ReturnIterator(newIter)
  248. *((*float64)(ptr)) = newIter.ReadFloat64()
  249. if newIter.Error != nil && newIter.Error != io.EOF {
  250. iter.Error = newIter.Error
  251. }
  252. case jsoniter.BoolValue:
  253. // support bool to float64
  254. if iter.ReadBool() {
  255. *((*float64)(ptr)) = 1
  256. } else {
  257. *((*float64)(ptr)) = 0
  258. }
  259. default:
  260. iter.ReportError("fuzzyFloat32Decoder", "not number or string")
  261. }
  262. }