json_parser.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package dara
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. jsoniter "github.com/json-iterator/go"
  6. "github.com/modern-go/reflect2"
  7. "io"
  8. "math"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "unsafe"
  13. )
  14. const maxUint = ^uint(0)
  15. const maxInt = int(maxUint >> 1)
  16. const minInt = -maxInt - 1
  17. var jsonParser jsoniter.API
  18. func init() {
  19. jsonParser = jsoniter.Config{
  20. EscapeHTML: true,
  21. SortMapKeys: true,
  22. ValidateJsonRawMessage: true,
  23. CaseSensitive: true,
  24. }.Froze()
  25. jsonParser.RegisterExtension(newBetterFuzzyExtension())
  26. }
  27. func newBetterFuzzyExtension() jsoniter.DecoderExtension {
  28. return jsoniter.DecoderExtension{
  29. reflect2.DefaultTypeOfKind(reflect.String): &nullableFuzzyStringDecoder{},
  30. reflect2.DefaultTypeOfKind(reflect.Bool): &fuzzyBoolDecoder{},
  31. reflect2.DefaultTypeOfKind(reflect.Float32): &nullableFuzzyFloat32Decoder{},
  32. reflect2.DefaultTypeOfKind(reflect.Float64): &nullableFuzzyFloat64Decoder{},
  33. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. reflect2.DefaultTypeOfKind(reflect.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. }
  155. type nullableFuzzyStringDecoder struct {
  156. }
  157. func (decoder *nullableFuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  158. valueType := iter.WhatIsNext()
  159. switch valueType {
  160. case jsoniter.NumberValue:
  161. var number json.Number
  162. iter.ReadVal(&number)
  163. *((*string)(ptr)) = string(number)
  164. case jsoniter.StringValue:
  165. *((*string)(ptr)) = iter.ReadString()
  166. case jsoniter.BoolValue:
  167. *((*string)(ptr)) = strconv.FormatBool(iter.ReadBool())
  168. case jsoniter.NilValue:
  169. iter.ReadNil()
  170. *((*string)(ptr)) = ""
  171. default:
  172. iter.ReportError("fuzzyStringDecoder", "not number or string or bool")
  173. }
  174. }
  175. type fuzzyBoolDecoder struct {
  176. }
  177. func (decoder *fuzzyBoolDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  178. valueType := iter.WhatIsNext()
  179. switch valueType {
  180. case jsoniter.BoolValue:
  181. *((*bool)(ptr)) = iter.ReadBool()
  182. case jsoniter.NumberValue:
  183. var number json.Number
  184. iter.ReadVal(&number)
  185. num, err := number.Int64()
  186. if err != nil {
  187. iter.ReportError("fuzzyBoolDecoder", "get value from json.number failed")
  188. }
  189. if num == 0 {
  190. *((*bool)(ptr)) = false
  191. } else {
  192. *((*bool)(ptr)) = true
  193. }
  194. case jsoniter.StringValue:
  195. strValue := strings.ToLower(iter.ReadString())
  196. if strValue == "true" {
  197. *((*bool)(ptr)) = true
  198. } else if strValue == "false" || strValue == "" {
  199. *((*bool)(ptr)) = false
  200. } else {
  201. iter.ReportError("fuzzyBoolDecoder", "unsupported bool value: "+strValue)
  202. }
  203. case jsoniter.NilValue:
  204. iter.ReadNil()
  205. *((*bool)(ptr)) = false
  206. default:
  207. iter.ReportError("fuzzyBoolDecoder", "not number or string or nil")
  208. }
  209. }
  210. type nullableFuzzyIntegerDecoder struct {
  211. fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
  212. }
  213. func (decoder *nullableFuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  214. valueType := iter.WhatIsNext()
  215. var str string
  216. switch valueType {
  217. case jsoniter.NumberValue:
  218. var number json.Number
  219. iter.ReadVal(&number)
  220. str = string(number)
  221. case jsoniter.StringValue:
  222. str = iter.ReadString()
  223. // support empty string
  224. if str == "" {
  225. str = "0"
  226. }
  227. case jsoniter.BoolValue:
  228. if iter.ReadBool() {
  229. str = "1"
  230. } else {
  231. str = "0"
  232. }
  233. case jsoniter.NilValue:
  234. iter.ReadNil()
  235. str = "0"
  236. default:
  237. iter.ReportError("fuzzyIntegerDecoder", "not number or string")
  238. }
  239. newIter := iter.Pool().BorrowIterator([]byte(str))
  240. defer iter.Pool().ReturnIterator(newIter)
  241. isFloat := strings.IndexByte(str, '.') != -1
  242. decoder.fun(isFloat, ptr, newIter)
  243. if newIter.Error != nil && newIter.Error != io.EOF {
  244. iter.Error = newIter.Error
  245. }
  246. }
  247. type nullableFuzzyFloat32Decoder struct {
  248. }
  249. func (decoder *nullableFuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  250. valueType := iter.WhatIsNext()
  251. var str string
  252. switch valueType {
  253. case jsoniter.NumberValue:
  254. *((*float32)(ptr)) = iter.ReadFloat32()
  255. case jsoniter.StringValue:
  256. str = iter.ReadString()
  257. // support empty string
  258. if str == "" {
  259. *((*float32)(ptr)) = 0
  260. return
  261. }
  262. newIter := iter.Pool().BorrowIterator([]byte(str))
  263. defer iter.Pool().ReturnIterator(newIter)
  264. *((*float32)(ptr)) = newIter.ReadFloat32()
  265. if newIter.Error != nil && newIter.Error != io.EOF {
  266. iter.Error = newIter.Error
  267. }
  268. case jsoniter.BoolValue:
  269. // support bool to float32
  270. if iter.ReadBool() {
  271. *((*float32)(ptr)) = 1
  272. } else {
  273. *((*float32)(ptr)) = 0
  274. }
  275. case jsoniter.NilValue:
  276. iter.ReadNil()
  277. *((*float32)(ptr)) = 0
  278. default:
  279. iter.ReportError("nullableFuzzyFloat32Decoder", "not number or string")
  280. }
  281. }
  282. type nullableFuzzyFloat64Decoder struct {
  283. }
  284. func (decoder *nullableFuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  285. valueType := iter.WhatIsNext()
  286. var str string
  287. switch valueType {
  288. case jsoniter.NumberValue:
  289. *((*float64)(ptr)) = iter.ReadFloat64()
  290. case jsoniter.StringValue:
  291. str = iter.ReadString()
  292. // support empty string
  293. if str == "" {
  294. *((*float64)(ptr)) = 0
  295. return
  296. }
  297. newIter := iter.Pool().BorrowIterator([]byte(str))
  298. defer iter.Pool().ReturnIterator(newIter)
  299. *((*float64)(ptr)) = newIter.ReadFloat64()
  300. if newIter.Error != nil && newIter.Error != io.EOF {
  301. iter.Error = newIter.Error
  302. }
  303. case jsoniter.BoolValue:
  304. // support bool to float64
  305. if iter.ReadBool() {
  306. *((*float64)(ptr)) = 1
  307. } else {
  308. *((*float64)(ptr)) = 0
  309. }
  310. case jsoniter.NilValue:
  311. // support empty string
  312. iter.ReadNil()
  313. *((*float64)(ptr)) = 0
  314. default:
  315. iter.ReportError("nullableFuzzyFloat64Decoder", "not number or string")
  316. }
  317. }
  318. func Stringify(m interface{}) string {
  319. byt, _ := json.Marshal(m)
  320. return string(byt)
  321. }
  322. func ParseJSON(a string) interface{} {
  323. mapTmp := make(map[string]interface{})
  324. d := json.NewDecoder(bytes.NewReader([]byte(a)))
  325. d.UseNumber()
  326. err := d.Decode(&mapTmp)
  327. if err == nil {
  328. return mapTmp
  329. }
  330. sliceTmp := make([]interface{}, 0)
  331. d = json.NewDecoder(bytes.NewReader([]byte(a)))
  332. d.UseNumber()
  333. err = d.Decode(&sliceTmp)
  334. if err == nil {
  335. return sliceTmp
  336. }
  337. if num, err := strconv.Atoi(a); err == nil {
  338. return num
  339. }
  340. if ok, err := strconv.ParseBool(a); err == nil {
  341. return ok
  342. }
  343. if floa64tVal, err := strconv.ParseFloat(a, 64); err == nil {
  344. return floa64tVal
  345. }
  346. return nil
  347. }