Skip to content

Commit

Permalink
chore: api comment
Browse files Browse the repository at this point in the history
chore
  • Loading branch information
AsterDY committed Jul 28, 2024
1 parent 1aaf54a commit 2155f34
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ sub := root.Get("key3").Index(2).Int64() // == 3

**Tip**: since `Index()` uses offset to locate data, which is much faster than scanning like `Get()`, we suggest you use it as much as possible. And sonic also provides another API `IndexOrGet()` to underlying use offset as well as ensure the key is matched.

#### SearchOption
`Searcher` provides some options for user to meet different needs:
```go
opts := ast.SearchOption{ CopyReturn: true ... }
val, err := sonic.GetWithOptions(JSON, opts, "key")
```
- CopyReturn
Indicate the searcher to copy the result JSON string instead of refer from the input. This can help to reduce memory usage if you cache the results
- ConcurentRead
Since `ast.Node` use `Lazy-Load` design, it doesn't support Concurrently-Read by default. If you want to read it concurrently, please specify it.
- ValidateJSON
Indicate the searcher to validate the entire JSON. This option is enabled by default, which slow down the search speed a little.
#### Set/Unset
Modify the json content by Set()/Unset()
Expand All @@ -300,20 +314,6 @@ println(alias1 == alias2) // true
exist, err := root.UnsetByIndex(1) // exist == true
println(root.Get("key4").Check()) // "value not exist"
```

#### SearchOption
```go
opts := ast.SearchOption{ CopyReturn: true ... }
val, err := ast.GetWithOption(JSON, opts, "key"...)
```
`Searcher` provides some options for use to meet different needs:
- CopyReturn
Indicate the searcher to copy the result JSON string instead of refer from the input. This can help to reduce memory usage if you cache the results
- ConcurentRead
Since `ast.Node` use `Lazy-Load` design, it doesn't support Concurrently-Read by default. If you want to read it concurrently, please specify it.
- ValidateJSON
Indicate the searcher to validate the entire JSON. This option is enabled by default.
#### Serialize
To encode `ast.Node` as json, use `MarshalJson()` or `json.Marshal()` (MUST pass the node's pointer)
Expand Down
15 changes: 14 additions & 1 deletion README_ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fmt.Printf("%+v", data) // {A:0 B:1}

### `Ast.Node`

Sonic/ast.Node 是完全独立的 JSON 抽象语法树库。它实现了序列化和反序列化,并提供了获取和修改通用数据的鲁棒的 API
Sonic/ast.Node 是完全独立的 JSON 抽象语法树库。它实现了序列化和反序列化,并提供了获取和修改JSON数据的鲁棒的 API

#### 查找/索引

Expand All @@ -282,6 +282,19 @@ sub := root.Get("key3").Index(2).Int64() // == 3

**注意**:由于 `Index()` 使用偏移量来定位数据,比使用扫描的 `Get()` 要快的多,建议尽可能的使用 `Index`Sonic 也提供了另一个 API`IndexOrGet()` ,以偏移量为基础并且也确保键的匹配。

#### 查找选项
`ast.Searcher`提供了一些选项,以满足用户的不同需求:
```
opts:= ast.SearchOption{CopyReturn: true…}
Val, err:= sonic。gettwithoptions (JSON, opts, "key")
```
- CopyReturn
指示搜索器复制结果JSON字符串,而不是从输入引用。如果用户缓存结果,这有助于减少内存使用
- ConcurentRead
因为`ast.Node`使用`Lazy-Load`设计,默认不支持并发读取。如果您想同时读取,请指定它。
- ValidateJSON
指示搜索器来验证整个JSON。默认情况下启用该选项, 但是对于查找速度有一定影响。
#### 修改
使用 `Set()` / `Unset()` 修改 json 的内容
Expand Down
2 changes: 2 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func Get(src []byte, path ...interface{}) (ast.Node, error) {
return GetCopyFromString(rt.Mem2Str(src), path...)
}

//GetWithOptions searches and locates the given path from src json,
// with specific options of ast.Searcher
func GetWithOptions(src []byte, opts ast.SearchOptions, path ...interface{}) (ast.Node, error) {
s := ast.NewSearcher(rt.Mem2Str(src))
s.SearchOptions = opts
Expand Down
8 changes: 4 additions & 4 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (self Node) Type() int {
return int(self.t & _MASK_LAZY & _MASK_RAW)
}

// Type returns json type represented by the node
// Type concurrently-safe returns json type represented by the node
// It will be one of belows:
// V_NONE = 0 (empty node, key not exists)
// V_ERROR = 1 (error node)
Expand Down Expand Up @@ -139,6 +139,7 @@ func (self *Node) Check() error {
}

// isRaw returns true if node's underlying value is raw json
//
// Deprecated: not concurent safe
func (self Node) IsRaw() bool {
return self.t & _V_RAW != 0
Expand All @@ -154,7 +155,7 @@ func (self *Node) isLazy() bool {
}

func (self *Node) isAny() bool {
return self != nil && self.t == _V_ANY
return self != nil && self.loadt() == _V_ANY
}

/** Simple Value Methods **/
Expand Down Expand Up @@ -1446,11 +1447,10 @@ func (self *Node) loadAllIndex(loadOnce bool) error {
parser, stack := self.getParserAndArrayStack()
if !loadOnce {
parser.noLazy = true
*self, err = parser.decodeArray(&stack.v)
} else {
parser.loadOnce = true
*self, err = parser.decodeArray(&stack.v)
}
*self, err = parser.decodeArray(&stack.v)
if err != 0 {
return parser.ExportError(err)
}
Expand Down
2 changes: 0 additions & 2 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ type Parser struct {
dbuf *byte
}

// var noLazy = option.AstSafeConcurrentRead

/** Parser Private Methods **/

func (self *Parser) delim() types.ParsingError {
Expand Down
1 change: 1 addition & 0 deletions ast/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestNodeRace(t *testing.T) {
start.RLock()
n := node.GetByPath(c.path...)
_ = n.TypeSafe()
_ = n.isAny()
v, err := n.Raw()
iv, _ := n.Int64()
lv, _ := n.Len()
Expand Down

0 comments on commit 2155f34

Please sign in to comment.