json_parser.go 10 KB

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