search.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package ast
  17. import (
  18. `github.com/bytedance/sonic/internal/rt`
  19. `github.com/bytedance/sonic/internal/native/types`
  20. )
  21. // SearchOptions controls Searcher's behavior
  22. type SearchOptions struct {
  23. // ValidateJSON indicates the searcher to validate the entire JSON
  24. ValidateJSON bool
  25. // CopyReturn indicates the searcher to copy the result JSON instead of refer from the input
  26. // This can help to reduce memory usage if you cache the results
  27. CopyReturn bool
  28. // ConcurrentRead indicates the searcher to return a concurrently-READ-safe node,
  29. // including: GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON
  30. ConcurrentRead bool
  31. }
  32. type Searcher struct {
  33. parser Parser
  34. SearchOptions
  35. }
  36. func NewSearcher(str string) *Searcher {
  37. return &Searcher{
  38. parser: Parser{
  39. s: str,
  40. noLazy: false,
  41. },
  42. SearchOptions: SearchOptions{
  43. ValidateJSON: true,
  44. },
  45. }
  46. }
  47. // GetByPathCopy search in depth from top json and returns a **Copied** json node at the path location
  48. func (self *Searcher) GetByPathCopy(path ...interface{}) (Node, error) {
  49. self.CopyReturn = true
  50. return self.getByPath(path...)
  51. }
  52. // GetByPathNoCopy search in depth from top json and returns a **Referenced** json node at the path location
  53. //
  54. // WARN: this search directly refer partial json from top json, which has faster speed,
  55. // may consumes more memory.
  56. func (self *Searcher) GetByPath(path ...interface{}) (Node, error) {
  57. return self.getByPath(path...)
  58. }
  59. func (self *Searcher) getByPath(path ...interface{}) (Node, error) {
  60. var err types.ParsingError
  61. var start int
  62. self.parser.p = 0
  63. start, err = self.parser.getByPath(self.ValidateJSON, path...)
  64. if err != 0 {
  65. // for compatibility with old version
  66. if err == types.ERR_NOT_FOUND {
  67. return Node{}, ErrNotExist
  68. }
  69. if err == types.ERR_UNSUPPORT_TYPE {
  70. panic("path must be either int(>=0) or string")
  71. }
  72. return Node{}, self.parser.syntaxError(err)
  73. }
  74. t := switchRawType(self.parser.s[start])
  75. if t == _V_NONE {
  76. return Node{}, self.parser.ExportError(err)
  77. }
  78. // copy string to reducing memory usage
  79. var raw string
  80. if self.CopyReturn {
  81. raw = rt.Mem2Str([]byte(self.parser.s[start:self.parser.p]))
  82. } else {
  83. raw = self.parser.s[start:self.parser.p]
  84. }
  85. return newRawNode(raw, t, self.ConcurrentRead), nil
  86. }
  87. // GetByPath searches a path and returns relaction and types of target
  88. func _GetByPath(src string, path ...interface{}) (start int, end int, typ int, err error) {
  89. p := NewParserObj(src)
  90. s, e := p.getByPath(false, path...)
  91. if e != 0 {
  92. // for compatibility with old version
  93. if e == types.ERR_NOT_FOUND {
  94. return -1, -1, 0, ErrNotExist
  95. }
  96. if e == types.ERR_UNSUPPORT_TYPE {
  97. panic("path must be either int(>=0) or string")
  98. }
  99. return -1, -1, 0, p.syntaxError(e)
  100. }
  101. t := switchRawType(p.s[s])
  102. if t == _V_NONE {
  103. return -1, -1, 0, ErrNotExist
  104. }
  105. if t == _V_NUMBER {
  106. p.p = 1 + backward(p.s, p.p-1)
  107. }
  108. return s, p.p, int(t), nil
  109. }
  110. // ValidSyntax check if a json has a valid JSON syntax,
  111. // while not validate UTF-8 charset
  112. func _ValidSyntax(json string) bool {
  113. p := NewParserObj(json)
  114. _, e := p.skip()
  115. if e != 0 {
  116. return false
  117. }
  118. if skipBlank(p.s, p.p) != -int(types.ERR_EOF) {
  119. return false
  120. }
  121. return true
  122. }
  123. // SkipFast skip a json value in fast-skip algs,
  124. // while not strictly validate JSON syntax and UTF-8 charset.
  125. func _SkipFast(src string, i int) (int, int, error) {
  126. p := NewParserObj(src)
  127. p.p = i
  128. s, e := p.skipFast()
  129. if e != 0 {
  130. return -1, -1, p.ExportError(e)
  131. }
  132. t := switchRawType(p.s[s])
  133. if t == _V_NONE {
  134. return -1, -1, ErrNotExist
  135. }
  136. if t == _V_NUMBER {
  137. p.p = 1 + backward(p.s, p.p-1)
  138. }
  139. return s, p.p, nil
  140. }