iter_skip.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package jsoniter
  2. import "fmt"
  3. // ReadNil reads a json object as nil and
  4. // returns whether it's a nil or not
  5. func (iter *Iterator) ReadNil() (ret bool) {
  6. c := iter.nextToken()
  7. if c == 'n' {
  8. iter.skipThreeBytes('u', 'l', 'l') // null
  9. return true
  10. }
  11. iter.unreadByte()
  12. return false
  13. }
  14. // ReadBool reads a json object as BoolValue
  15. func (iter *Iterator) ReadBool() (ret bool) {
  16. c := iter.nextToken()
  17. if c == 't' {
  18. iter.skipThreeBytes('r', 'u', 'e')
  19. return true
  20. }
  21. if c == 'f' {
  22. iter.skipFourBytes('a', 'l', 's', 'e')
  23. return false
  24. }
  25. iter.ReportError("ReadBool", "expect t or f, but found "+string([]byte{c}))
  26. return
  27. }
  28. // SkipAndReturnBytes skip next JSON element, and return its content as []byte.
  29. // The []byte can be kept, it is a copy of data.
  30. func (iter *Iterator) SkipAndReturnBytes() []byte {
  31. iter.startCapture(iter.head)
  32. iter.Skip()
  33. return iter.stopCapture()
  34. }
  35. // SkipAndAppendBytes skips next JSON element and appends its content to
  36. // buffer, returning the result.
  37. func (iter *Iterator) SkipAndAppendBytes(buf []byte) []byte {
  38. iter.startCaptureTo(buf, iter.head)
  39. iter.Skip()
  40. return iter.stopCapture()
  41. }
  42. func (iter *Iterator) startCaptureTo(buf []byte, captureStartedAt int) {
  43. if iter.captured != nil {
  44. panic("already in capture mode")
  45. }
  46. iter.captureStartedAt = captureStartedAt
  47. iter.captured = buf
  48. }
  49. func (iter *Iterator) startCapture(captureStartedAt int) {
  50. iter.startCaptureTo(make([]byte, 0, 32), captureStartedAt)
  51. }
  52. func (iter *Iterator) stopCapture() []byte {
  53. if iter.captured == nil {
  54. panic("not in capture mode")
  55. }
  56. captured := iter.captured
  57. remaining := iter.buf[iter.captureStartedAt:iter.head]
  58. iter.captureStartedAt = -1
  59. iter.captured = nil
  60. return append(captured, remaining...)
  61. }
  62. // Skip skips a json object and positions to relatively the next json object
  63. func (iter *Iterator) Skip() {
  64. c := iter.nextToken()
  65. switch c {
  66. case '"':
  67. iter.skipString()
  68. case 'n':
  69. iter.skipThreeBytes('u', 'l', 'l') // null
  70. case 't':
  71. iter.skipThreeBytes('r', 'u', 'e') // true
  72. case 'f':
  73. iter.skipFourBytes('a', 'l', 's', 'e') // false
  74. case '0':
  75. iter.unreadByte()
  76. iter.ReadFloat32()
  77. case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  78. iter.skipNumber()
  79. case '[':
  80. iter.skipArray()
  81. case '{':
  82. iter.skipObject()
  83. default:
  84. iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
  85. return
  86. }
  87. }
  88. func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
  89. if iter.readByte() != b1 {
  90. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  91. return
  92. }
  93. if iter.readByte() != b2 {
  94. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  95. return
  96. }
  97. if iter.readByte() != b3 {
  98. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  99. return
  100. }
  101. if iter.readByte() != b4 {
  102. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  103. return
  104. }
  105. }
  106. func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
  107. if iter.readByte() != b1 {
  108. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  109. return
  110. }
  111. if iter.readByte() != b2 {
  112. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  113. return
  114. }
  115. if iter.readByte() != b3 {
  116. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  117. return
  118. }
  119. }