json_parser.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package responses
  2. import (
  3. "encoding/json"
  4. "io"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "unsafe"
  10. jsoniter "github.com/json-iterator/go"
  11. )
  12. const maxUint = ^uint(0)
  13. const maxInt = int(maxUint >> 1)
  14. const minInt = -maxInt - 1
  15. var jsonParser jsoniter.API
  16. var initJson = &sync.Once{}
  17. func initJsonParserOnce() {
  18. initJson.Do(func() {
  19. registerBetterFuzzyDecoder()
  20. jsonParser = jsoniter.Config{
  21. EscapeHTML: true,
  22. SortMapKeys: true,
  23. ValidateJsonRawMessage: true,
  24. CaseSensitive: true,
  25. }.Froze()
  26. })
  27. }
  28. func registerBetterFuzzyDecoder() {
  29. jsoniter.RegisterTypeDecoder("string", &nullableFuzzyStringDecoder{})
  30. jsoniter.RegisterTypeDecoder("bool", &fuzzyBoolDecoder{})
  31. jsoniter.RegisterTypeDecoder("float32", &nullableFuzzyFloat32Decoder{})
  32. jsoniter.RegisterTypeDecoder("float64", &nullableFuzzyFloat64Decoder{})
  33. jsoniter.RegisterTypeDecoder("int", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  34. if isFloat {
  35. val := iter.ReadFloat64()
  36. if val > float64(maxInt) || val < float64(minInt) {
  37. iter.ReportError("fuzzy decode int", "exceed range")
  38. return
  39. }
  40. *((*int)(ptr)) = int(val)
  41. } else {
  42. *((*int)(ptr)) = iter.ReadInt()
  43. }
  44. }})
  45. jsoniter.RegisterTypeDecoder("uint", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  46. if isFloat {
  47. val := iter.ReadFloat64()
  48. if val > float64(maxUint) || val < 0 {
  49. iter.ReportError("fuzzy decode uint", "exceed range")
  50. return
  51. }
  52. *((*uint)(ptr)) = uint(val)
  53. } else {
  54. *((*uint)(ptr)) = iter.ReadUint()
  55. }
  56. }})
  57. jsoniter.RegisterTypeDecoder("int8", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  58. if isFloat {
  59. val := iter.ReadFloat64()
  60. if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
  61. iter.ReportError("fuzzy decode int8", "exceed range")
  62. return
  63. }
  64. *((*int8)(ptr)) = int8(val)
  65. } else {
  66. *((*int8)(ptr)) = iter.ReadInt8()
  67. }
  68. }})
  69. jsoniter.RegisterTypeDecoder("uint8", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  70. if isFloat {
  71. val := iter.ReadFloat64()
  72. if val > float64(math.MaxUint8) || val < 0 {
  73. iter.ReportError("fuzzy decode uint8", "exceed range")
  74. return
  75. }
  76. *((*uint8)(ptr)) = uint8(val)
  77. } else {
  78. *((*uint8)(ptr)) = iter.ReadUint8()
  79. }
  80. }})
  81. jsoniter.RegisterTypeDecoder("int16", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  82. if isFloat {
  83. val := iter.ReadFloat64()
  84. if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
  85. iter.ReportError("fuzzy decode int16", "exceed range")
  86. return
  87. }
  88. *((*int16)(ptr)) = int16(val)
  89. } else {
  90. *((*int16)(ptr)) = iter.ReadInt16()
  91. }
  92. }})
  93. jsoniter.RegisterTypeDecoder("uint16", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  94. if isFloat {
  95. val := iter.ReadFloat64()
  96. if val > float64(math.MaxUint16) || val < 0 {
  97. iter.ReportError("fuzzy decode uint16", "exceed range")
  98. return
  99. }
  100. *((*uint16)(ptr)) = uint16(val)
  101. } else {
  102. *((*uint16)(ptr)) = iter.ReadUint16()
  103. }
  104. }})
  105. jsoniter.RegisterTypeDecoder("int32", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  106. if isFloat {
  107. val := iter.ReadFloat64()
  108. if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
  109. iter.ReportError("fuzzy decode int32", "exceed range")
  110. return
  111. }
  112. *((*int32)(ptr)) = int32(val)
  113. } else {
  114. *((*int32)(ptr)) = iter.ReadInt32()
  115. }
  116. }})
  117. jsoniter.RegisterTypeDecoder("uint32", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  118. if isFloat {
  119. val := iter.ReadFloat64()
  120. if val > float64(math.MaxUint32) || val < 0 {
  121. iter.ReportError("fuzzy decode uint32", "exceed range")
  122. return
  123. }
  124. *((*uint32)(ptr)) = uint32(val)
  125. } else {
  126. *((*uint32)(ptr)) = iter.ReadUint32()
  127. }
  128. }})
  129. jsoniter.RegisterTypeDecoder("int64", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  130. if isFloat {
  131. val := iter.ReadFloat64()
  132. if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
  133. iter.ReportError("fuzzy decode int64", "exceed range")
  134. return
  135. }
  136. *((*int64)(ptr)) = int64(val)
  137. } else {
  138. *((*int64)(ptr)) = iter.ReadInt64()
  139. }
  140. }})
  141. jsoniter.RegisterTypeDecoder("uint64", &nullableFuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  142. if isFloat {
  143. val := iter.ReadFloat64()
  144. if val > float64(math.MaxUint64) || val < 0 {
  145. iter.ReportError("fuzzy decode uint64", "exceed range")
  146. return
  147. }
  148. *((*uint64)(ptr)) = uint64(val)
  149. } else {
  150. *((*uint64)(ptr)) = iter.ReadUint64()
  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. }