feature_any.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package jsoniter
  2. import "fmt"
  3. type Any interface {
  4. LastError() error
  5. ToBool() bool
  6. ToInt() int
  7. ToInt32() int32
  8. ToInt64() int64
  9. ToFloat32() float32
  10. ToFloat64() float64
  11. ToString() string
  12. }
  13. func (iter *Iterator) ReadAny() Any {
  14. valueType := iter.WhatIsNext()
  15. switch valueType {
  16. case Nil:
  17. iter.skipFixedBytes(4)
  18. return &nilAny{}
  19. case Number:
  20. dotFound, lazyBuf := iter.skipNumber()
  21. if dotFound {
  22. return &floatLazyAny{lazyBuf, nil, nil}
  23. } else {
  24. return &intLazyAny{lazyBuf, nil, nil, 0}
  25. }
  26. }
  27. iter.reportError("ReadAny", fmt.Sprintf("unexpected value type: %v", valueType))
  28. return nil
  29. }
  30. func (iter *Iterator) skipNumber() (bool, []byte) {
  31. dotFound := false
  32. var lazyBuf []byte
  33. for {
  34. for i := iter.head; i < iter.tail; i++ {
  35. c := iter.buf[i]
  36. if c == '.' {
  37. dotFound = true
  38. continue
  39. }
  40. switch c {
  41. case ' ', '\n', '\r', '\t', ',', '}', ']':
  42. lazyBuf = append(lazyBuf, iter.buf[iter.head:i]...)
  43. iter.head = i
  44. return dotFound, lazyBuf
  45. }
  46. }
  47. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  48. if !iter.loadMore() {
  49. iter.head = iter.tail;
  50. return dotFound, lazyBuf
  51. }
  52. }
  53. }