feature_any_float.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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) fillCache() {
  22. if any.err != nil {
  23. return
  24. }
  25. iter := any.Parse()
  26. any.cache = iter.ReadFloat64()
  27. if iter.Error != io.EOF {
  28. iter.reportError("floatLazyAny", "there are bytes left")
  29. }
  30. any.err = iter.Error
  31. }
  32. func (any *floatLazyAny) LastError() error {
  33. return any.err
  34. }
  35. func (any *floatLazyAny) ToBool() bool {
  36. return any.ToFloat64() != 0
  37. }
  38. func (any *floatLazyAny) ToInt() int {
  39. any.fillCache()
  40. return int(any.cache)
  41. }
  42. func (any *floatLazyAny) ToInt32() int32 {
  43. any.fillCache()
  44. return int32(any.cache)
  45. }
  46. func (any *floatLazyAny) ToInt64() int64 {
  47. any.fillCache()
  48. return int64(any.cache)
  49. }
  50. func (any *floatLazyAny) ToFloat32() float32 {
  51. any.fillCache()
  52. return float32(any.cache)
  53. }
  54. func (any *floatLazyAny) ToFloat64() float64 {
  55. any.fillCache()
  56. return any.cache
  57. }
  58. func (any *floatLazyAny) ToString() string {
  59. return *(*string)(unsafe.Pointer(&any.buf))
  60. }
  61. func (any *floatLazyAny) WriteTo(stream *Stream) {
  62. stream.Write(any.buf)
  63. }
  64. func (any *floatLazyAny) GetInterface() interface{} {
  65. any.fillCache()
  66. return any.cache
  67. }