json_parser.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package responses
  2. import (
  3. "encoding/json"
  4. "io"
  5. "math"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "unsafe"
  10. jsoniter "github.com/json-iterator/go"
  11. "github.com/modern-go/reflect2"
  12. )
  13. const maxUint = ^uint(0)
  14. const maxInt = int(maxUint >> 1)
  15. const minInt = -maxInt - 1
  16. var jsonParser jsoniter.API
  17. func init() {
  18. jsonParser = jsoniter.Config{
  19. EscapeHTML: true,
  20. SortMapKeys: true,
  21. ValidateJsonRawMessage: true,
  22. CaseSensitive: true,
  23. }.Froze()
  24. jsonParser.RegisterExtension(newBetterFuzzyExtension())
  25. }
  26. func newBetterFuzzyExtension() jsoniter.DecoderExtension {
  27. return jsoniter.DecoderExtension{
  28. reflect2.DefaultTypeOfKind(reflect.String): &nullableFuzzyStringDecoder{},
  29. reflect2.DefaultTypeOfKind(reflect.Bool): &fuzzyBoolDecoder{},
  30. reflect2.DefaultTypeOfKind(reflect.Float32): &nullableFuzzyFloat32Decoder{},
  31. reflect2.DefaultTypeOfKind(reflect.Float64): &nullableFuzzyFloat64Decoder{},
  32. reflect2.DefaultTypeOfKind(reflect.Int): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  33. if isFloat {
  34. val := iter.ReadFloat64()
  35. if val > float64(maxInt) || val < float64(minInt) {
  36. iter.ReportError("fuzzy decode int", "exceed range")
  37. return
  38. }
  39. *((*int)(ptr)) = int(val)
  40. } else {
  41. *((*int)(ptr)) = iter.ReadInt()
  42. }
  43. }},
  44. reflect2.DefaultTypeOfKind(reflect.Uint): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  45. if isFloat {
  46. val := iter.ReadFloat64()
  47. if val > float64(maxUint) || val < 0 {
  48. iter.ReportError("fuzzy decode uint", "exceed range")
  49. return
  50. }
  51. *((*uint)(ptr)) = uint(val)
  52. } else {
  53. *((*uint)(ptr)) = iter.ReadUint()
  54. }
  55. }},
  56. reflect2.DefaultTypeOfKind(reflect.Int8): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  57. if isFloat {
  58. val := iter.ReadFloat64()
  59. if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
  60. iter.ReportError("fuzzy decode int8", "exceed range")
  61. return
  62. }
  63. *((*int8)(ptr)) = int8(val)
  64. } else {
  65. *((*int8)(ptr)) = iter.ReadInt8()
  66. }
  67. }},
  68. reflect2.DefaultTypeOfKind(reflect.Uint8): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  69. if isFloat {
  70. val := iter.ReadFloat64()
  71. if val > float64(math.MaxUint8) || val < 0 {
  72. iter.ReportError("fuzzy decode uint8", "exceed range")
  73. return
  74. }
  75. *((*uint8)(ptr)) = uint8(val)
  76. } else {
  77. *((*uint8)(ptr)) = iter.ReadUint8()
  78. }
  79. }},
  80. reflect2.DefaultTypeOfKind(reflect.Int16): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  81. if isFloat {
  82. val := iter.ReadFloat64()
  83. if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
  84. iter.ReportError("fuzzy decode int16", "exceed range")
  85. return
  86. }
  87. *((*int16)(ptr)) = int16(val)
  88. } else {
  89. *((*int16)(ptr)) = iter.ReadInt16()
  90. }
  91. }},
  92. reflect2.DefaultTypeOfKind(reflect.Uint16): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  93. if isFloat {
  94. val := iter.ReadFloat64()
  95. if val > float64(math.MaxUint16) || val < 0 {
  96. iter.ReportError("fuzzy decode uint16", "exceed range")
  97. return
  98. }
  99. *((*uint16)(ptr)) = uint16(val)
  100. } else {
  101. *((*uint16)(ptr)) = iter.ReadUint16()
  102. }
  103. }},
  104. reflect2.DefaultTypeOfKind(reflect.Int32): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  105. if isFloat {
  106. val := iter.ReadFloat64()
  107. if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
  108. iter.ReportError("fuzzy decode int32", "exceed range")
  109. return
  110. }
  111. *((*int32)(ptr)) = int32(val)
  112. } else {
  113. *((*int32)(ptr)) = iter.ReadInt32()
  114. }
  115. }},
  116. reflect2.DefaultTypeOfKind(reflect.Uint32): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  117. if isFloat {
  118. val := iter.ReadFloat64()
  119. if val > float64(math.MaxUint32) || val < 0 {
  120. iter.ReportError("fuzzy decode uint32", "exceed range")
  121. return
  122. }
  123. *((*uint32)(ptr)) = uint32(val)
  124. } else {
  125. *((*uint32)(ptr)) = iter.ReadUint32()
  126. }
  127. }},
  128. reflect2.DefaultTypeOfKind(reflect.Int64): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  129. if isFloat {
  130. val := iter.ReadFloat64()
  131. if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
  132. iter.ReportError("fuzzy decode int64", "exceed range")
  133. return
  134. }
  135. *((*int64)(ptr)) = int64(val)
  136. } else {
  137. *((*int64)(ptr)) = iter.ReadInt64()
  138. }
  139. }},
  140. reflect2.DefaultTypeOfKind(reflect.Uint64): &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  141. if isFloat {
  142. val := iter.ReadFloat64()
  143. if val > float64(math.MaxUint64) || val < 0 {
  144. iter.ReportError("fuzzy decode uint64", "exceed range")
  145. return
  146. }
  147. *((*uint64)(ptr)) = uint64(val)
  148. } else {
  149. *((*uint64)(ptr)) = iter.ReadUint64()
  150. }
  151. }},
  152. }
  153. }
  154. type nullableFuzzyStringDecoder struct {
  155. }
  156. func (decoder *nullableFuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  157. valueType := iter.WhatIsNext()
  158. switch valueType {
  159. case jsoniter.NumberValue:
  160. var number json.Number
  161. iter.ReadVal(&number)
  162. *((*string)(ptr)) = string(number)
  163. case jsoniter.StringValue:
  164. *((*string)(ptr)) = iter.ReadString()
  165. case jsoniter.BoolValue:
  166. *((*string)(ptr)) = strconv.FormatBool(iter.ReadBool())
  167. case jsoniter.NilValue:
  168. iter.ReadNil()
  169. *((*string)(ptr)) = ""
  170. default:
  171. iter.ReportError("fuzzyStringDecoder", "not number or string or bool")
  172. }
  173. }
  174. type fuzzyBoolDecoder struct {
  175. }
  176. func (decoder *fuzzyBoolDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  177. valueType := iter.WhatIsNext()
  178. switch valueType {
  179. case jsoniter.BoolValue:
  180. *((*bool)(ptr)) = iter.ReadBool()
  181. case jsoniter.NumberValue:
  182. var number json.Number
  183. iter.ReadVal(&number)
  184. num, err := number.Int64()
  185. if err != nil {
  186. iter.ReportError("fuzzyBoolDecoder", "get value from json.number failed")
  187. }
  188. if num == 0 {
  189. *((*bool)(ptr)) = false
  190. } else {
  191. *((*bool)(ptr)) = true
  192. }
  193. case jsoniter.StringValue:
  194. strValue := strings.ToLower(iter.ReadString())
  195. if strValue == "true" {
  196. *((*bool)(ptr)) = true
  197. } else if strValue == "false" || strValue == "" {
  198. *((*bool)(ptr)) = false
  199. } else {
  200. iter.ReportError("fuzzyBoolDecoder", "unsupported bool value: "+strValue)
  201. }
  202. case jsoniter.NilValue:
  203. iter.ReadNil()
  204. *((*bool)(ptr)) = false
  205. default:
  206. iter.ReportError("fuzzyBoolDecoder", "not number or string or nil")
  207. }
  208. }
  209. type nullableFuzzyIntegerDecoder struct {
  210. fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
  211. }
  212. func (decoder *nullableFuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  213. valueType := iter.WhatIsNext()
  214. var str string
  215. switch valueType {
  216. case jsoniter.NumberValue:
  217. var number json.Number
  218. iter.ReadVal(&number)
  219. str = string(number)
  220. case jsoniter.StringValue:
  221. str = iter.ReadString()
  222. // support empty string
  223. if str == "" {
  224. str = "0"
  225. }
  226. case jsoniter.BoolValue:
  227. if iter.ReadBool() {
  228. str = "1"
  229. } else {
  230. str = "0"
  231. }
  232. case jsoniter.NilValue:
  233. iter.ReadNil()
  234. str = "0"
  235. default:
  236. iter.ReportError("fuzzyIntegerDecoder", "not number or string")
  237. }
  238. newIter := iter.Pool().BorrowIterator([]byte(str))
  239. defer iter.Pool().ReturnIterator(newIter)
  240. isFloat := strings.IndexByte(str, '.') != -1
  241. decoder.fun(isFloat, ptr, newIter)
  242. if newIter.Error != nil && newIter.Error != io.EOF {
  243. iter.Error = newIter.Error
  244. }
  245. }
  246. type nullableFuzzyFloat32Decoder struct {
  247. }
  248. func (decoder *nullableFuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  249. valueType := iter.WhatIsNext()
  250. var str string
  251. switch valueType {
  252. case jsoniter.NumberValue:
  253. *((*float32)(ptr)) = iter.ReadFloat32()
  254. case jsoniter.StringValue:
  255. str = iter.ReadString()
  256. // support empty string
  257. if str == "" {
  258. *((*float32)(ptr)) = 0
  259. return
  260. }
  261. newIter := iter.Pool().BorrowIterator([]byte(str))
  262. defer iter.Pool().ReturnIterator(newIter)
  263. *((*float32)(ptr)) = newIter.ReadFloat32()
  264. if newIter.Error != nil && newIter.Error != io.EOF {
  265. iter.Error = newIter.Error
  266. }
  267. case jsoniter.BoolValue:
  268. // support bool to float32
  269. if iter.ReadBool() {
  270. *((*float32)(ptr)) = 1
  271. } else {
  272. *((*float32)(ptr)) = 0
  273. }
  274. case jsoniter.NilValue:
  275. iter.ReadNil()
  276. *((*float32)(ptr)) = 0
  277. default:
  278. iter.ReportError("nullableFuzzyFloat32Decoder", "not number or string")
  279. }
  280. }
  281. type nullableFuzzyFloat64Decoder struct {
  282. }
  283. func (decoder *nullableFuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  284. valueType := iter.WhatIsNext()
  285. var str string
  286. switch valueType {
  287. case jsoniter.NumberValue:
  288. *((*float64)(ptr)) = iter.ReadFloat64()
  289. case jsoniter.StringValue:
  290. str = iter.ReadString()
  291. // support empty string
  292. if str == "" {
  293. *((*float64)(ptr)) = 0
  294. return
  295. }
  296. newIter := iter.Pool().BorrowIterator([]byte(str))
  297. defer iter.Pool().ReturnIterator(newIter)
  298. *((*float64)(ptr)) = newIter.ReadFloat64()
  299. if newIter.Error != nil && newIter.Error != io.EOF {
  300. iter.Error = newIter.Error
  301. }
  302. case jsoniter.BoolValue:
  303. // support bool to float64
  304. if iter.ReadBool() {
  305. *((*float64)(ptr)) = 1
  306. } else {
  307. *((*float64)(ptr)) = 0
  308. }
  309. case jsoniter.NilValue:
  310. // support empty string
  311. iter.ReadNil()
  312. *((*float64)(ptr)) = 0
  313. default:
  314. iter.ReportError("nullableFuzzyFloat64Decoder", "not number or string")
  315. }
  316. }