reflect_json_number.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/modern-go/reflect2"
  5. "strconv"
  6. "unsafe"
  7. )
  8. type Number string
  9. // String returns the literal text of the number.
  10. func (n Number) String() string { return string(n) }
  11. // Float64 returns the number as a float64.
  12. func (n Number) Float64() (float64, error) {
  13. return strconv.ParseFloat(string(n), 64)
  14. }
  15. // Int64 returns the number as an int64.
  16. func (n Number) Int64() (int64, error) {
  17. return strconv.ParseInt(string(n), 10, 64)
  18. }
  19. func CastJsonNumber(val interface{}) (string, bool) {
  20. switch typedVal := val.(type) {
  21. case json.Number:
  22. return string(typedVal), true
  23. case Number:
  24. return string(typedVal), true
  25. }
  26. return "", false
  27. }
  28. var jsonNumberType = reflect2.TypeOfPtr((*json.Number)(nil)).Elem()
  29. var jsoniterNumberType = reflect2.TypeOfPtr((*Number)(nil)).Elem()
  30. func createDecoderOfJsonNumber(ctx *ctx, typ reflect2.Type) ValDecoder {
  31. if typ.AssignableTo(jsonNumberType) {
  32. return &jsonNumberCodec{}
  33. }
  34. if typ.AssignableTo(jsoniterNumberType) {
  35. return &jsoniterNumberCodec{}
  36. }
  37. return nil
  38. }
  39. func createEncoderOfJsonNumber(ctx *ctx, typ reflect2.Type) ValEncoder {
  40. if typ.AssignableTo(jsonNumberType) {
  41. return &jsonNumberCodec{}
  42. }
  43. if typ.AssignableTo(jsoniterNumberType) {
  44. return &jsoniterNumberCodec{}
  45. }
  46. return nil
  47. }
  48. type jsonNumberCodec struct {
  49. }
  50. func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  51. switch iter.WhatIsNext() {
  52. case StringValue:
  53. *((*json.Number)(ptr)) = json.Number(iter.ReadString())
  54. case NilValue:
  55. iter.skipFourBytes('n', 'u', 'l', 'l')
  56. *((*json.Number)(ptr)) = ""
  57. default:
  58. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  59. }
  60. }
  61. func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  62. number := *((*json.Number)(ptr))
  63. if len(number) == 0 {
  64. stream.writeByte('0')
  65. } else {
  66. stream.WriteRaw(string(number))
  67. }
  68. }
  69. func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  70. return len(*((*json.Number)(ptr))) == 0
  71. }
  72. type jsoniterNumberCodec struct {
  73. }
  74. func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  75. switch iter.WhatIsNext() {
  76. case StringValue:
  77. *((*Number)(ptr)) = Number(iter.ReadString())
  78. case NilValue:
  79. iter.skipFourBytes('n', 'u', 'l', 'l')
  80. *((*Number)(ptr)) = ""
  81. default:
  82. *((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
  83. }
  84. }
  85. func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  86. number := *((*Number)(ptr))
  87. if len(number) == 0 {
  88. stream.writeByte('0')
  89. } else {
  90. stream.WriteRaw(string(number))
  91. }
  92. }
  93. func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  94. return len(*((*Number)(ptr))) == 0
  95. }