feature_any_float.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package jsoniter
  2. import (
  3. "io"
  4. "unsafe"
  5. )
  6. type floatLazyAny struct {
  7. baseAny
  8. buf []byte
  9. iter *Iterator
  10. err error
  11. cache float64
  12. }
  13. func (any *floatLazyAny) Parse() *Iterator {
  14. iter := any.iter
  15. if iter == nil {
  16. iter = NewIterator()
  17. }
  18. iter.ResetBytes(any.buf)
  19. return iter
  20. }
  21. func (any *floatLazyAny) ValueType() ValueType {
  22. return Number
  23. }
  24. func (any *floatLazyAny) fillCache() {
  25. if any.err != nil {
  26. return
  27. }
  28. iter := any.Parse()
  29. any.cache = iter.ReadFloat64()
  30. if iter.Error != io.EOF {
  31. iter.reportError("floatLazyAny", "there are bytes left")
  32. }
  33. any.err = iter.Error
  34. }
  35. func (any *floatLazyAny) LastError() error {
  36. return any.err
  37. }
  38. func (any *floatLazyAny) ToBool() bool {
  39. return any.ToFloat64() != 0
  40. }
  41. func (any *floatLazyAny) ToInt() int {
  42. any.fillCache()
  43. return int(any.cache)
  44. }
  45. func (any *floatLazyAny) ToInt32() int32 {
  46. any.fillCache()
  47. return int32(any.cache)
  48. }
  49. func (any *floatLazyAny) ToInt64() int64 {
  50. any.fillCache()
  51. return int64(any.cache)
  52. }
  53. func (any *floatLazyAny) ToFloat32() float32 {
  54. any.fillCache()
  55. return float32(any.cache)
  56. }
  57. func (any *floatLazyAny) ToFloat64() float64 {
  58. any.fillCache()
  59. return any.cache
  60. }
  61. func (any *floatLazyAny) ToString() string {
  62. return *(*string)(unsafe.Pointer(&any.buf))
  63. }
  64. func (any *floatLazyAny) WriteTo(stream *Stream) {
  65. stream.Write(any.buf)
  66. }
  67. func (any *floatLazyAny) GetInterface() interface{} {
  68. any.fillCache()
  69. return any.cache
  70. }