any_number.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package jsoniter
  2. import (
  3. "io"
  4. "unsafe"
  5. )
  6. type numberLazyAny struct {
  7. baseAny
  8. cfg *frozenConfig
  9. buf []byte
  10. err error
  11. }
  12. func (any *numberLazyAny) ValueType() ValueType {
  13. return NumberValue
  14. }
  15. func (any *numberLazyAny) MustBeValid() Any {
  16. return any
  17. }
  18. func (any *numberLazyAny) LastError() error {
  19. return any.err
  20. }
  21. func (any *numberLazyAny) ToBool() bool {
  22. return any.ToFloat64() != 0
  23. }
  24. func (any *numberLazyAny) ToInt() int {
  25. iter := any.cfg.BorrowIterator(any.buf)
  26. defer any.cfg.ReturnIterator(iter)
  27. val := iter.ReadInt()
  28. if iter.Error != nil && iter.Error != io.EOF {
  29. any.err = iter.Error
  30. }
  31. return val
  32. }
  33. func (any *numberLazyAny) ToInt32() int32 {
  34. iter := any.cfg.BorrowIterator(any.buf)
  35. defer any.cfg.ReturnIterator(iter)
  36. val := iter.ReadInt32()
  37. if iter.Error != nil && iter.Error != io.EOF {
  38. any.err = iter.Error
  39. }
  40. return val
  41. }
  42. func (any *numberLazyAny) ToInt64() int64 {
  43. iter := any.cfg.BorrowIterator(any.buf)
  44. defer any.cfg.ReturnIterator(iter)
  45. val := iter.ReadInt64()
  46. if iter.Error != nil && iter.Error != io.EOF {
  47. any.err = iter.Error
  48. }
  49. return val
  50. }
  51. func (any *numberLazyAny) ToUint() uint {
  52. iter := any.cfg.BorrowIterator(any.buf)
  53. defer any.cfg.ReturnIterator(iter)
  54. val := iter.ReadUint()
  55. if iter.Error != nil && iter.Error != io.EOF {
  56. any.err = iter.Error
  57. }
  58. return val
  59. }
  60. func (any *numberLazyAny) ToUint32() uint32 {
  61. iter := any.cfg.BorrowIterator(any.buf)
  62. defer any.cfg.ReturnIterator(iter)
  63. val := iter.ReadUint32()
  64. if iter.Error != nil && iter.Error != io.EOF {
  65. any.err = iter.Error
  66. }
  67. return val
  68. }
  69. func (any *numberLazyAny) ToUint64() uint64 {
  70. iter := any.cfg.BorrowIterator(any.buf)
  71. defer any.cfg.ReturnIterator(iter)
  72. val := iter.ReadUint64()
  73. if iter.Error != nil && iter.Error != io.EOF {
  74. any.err = iter.Error
  75. }
  76. return val
  77. }
  78. func (any *numberLazyAny) ToFloat32() float32 {
  79. iter := any.cfg.BorrowIterator(any.buf)
  80. defer any.cfg.ReturnIterator(iter)
  81. val := iter.ReadFloat32()
  82. if iter.Error != nil && iter.Error != io.EOF {
  83. any.err = iter.Error
  84. }
  85. return val
  86. }
  87. func (any *numberLazyAny) ToFloat64() float64 {
  88. iter := any.cfg.BorrowIterator(any.buf)
  89. defer any.cfg.ReturnIterator(iter)
  90. val := iter.ReadFloat64()
  91. if iter.Error != nil && iter.Error != io.EOF {
  92. any.err = iter.Error
  93. }
  94. return val
  95. }
  96. func (any *numberLazyAny) ToString() string {
  97. return *(*string)(unsafe.Pointer(&any.buf))
  98. }
  99. func (any *numberLazyAny) WriteTo(stream *Stream) {
  100. stream.Write(any.buf)
  101. }
  102. func (any *numberLazyAny) GetInterface() interface{} {
  103. iter := any.cfg.BorrowIterator(any.buf)
  104. defer any.cfg.ReturnIterator(iter)
  105. return iter.Read()
  106. }