diff --git a/README.md b/README.md index 57bef07e1..838dd46fa 100644 --- a/README.md +++ b/README.md @@ -290,12 +290,20 @@ println(string(buf) == string(exp)) // true - modification: `Set()`, `SetByIndex()`, `Add()` ### Ast.Value -Due to `ast.Node`'s **transversely-lazy-load** design, it is not effecient enough for read-once-for-each-path case, and it ** CANNOT be read concurrently**. If your business has such scenario, you can use `ast.Value` -- a more robust encapsulation of raw JSON message. +Due to `ast.Node`'s **transversely-lazy-load** design, it ** CANNOT be read concurrently**. If your business has such scenario, you can use `ast.Value`: +```go +var opts = sonic.SearchOptions{ + Copy: false, // to control returning JSON by Copy instead of Reference + Validate: false // to control if validate returned JSON syntax +} +val, err := opts.GetFromString(json, paths...) // skip and search JSON +any, err := val.Interface() // converts to go primitive type +``` #### APIs Most of its APIs are same with `ast.Node`'s, including both `Get` and `Set`. Besides: -- It provide `GetMany` \ `SetMany` to read or write multiple values once, in order to **reduce the overhead of repeatedly visiting path**. - +- It provide `GetMany` \ `SetMany` \ `UnsetMany` \ `AddMany` \ `PopMany` to read or write multiple values once, in order to **reduce the overhead of repeatedly visiting path**. +- ### Ast.Visitor Sonic provides an advanced API for fully parsing JSON into non-standard types (neither `struct` not `map[string]interface{}`) without using any intermediate representation (`ast.Node` or `interface{}`). For example, you might have the following types which are like `interface{}` but actually not `interface{}`: diff --git a/api.go b/api.go index 66584594b..fb9f47f66 100644 --- a/api.go +++ b/api.go @@ -219,8 +219,8 @@ func DeleteFromString(src string, path ...interface{}) (string, error) { return ast.NewSearcher(src).DeleteByPath(path...) } -// GetOptions -type GetOptions struct { +// SearchOptions +type SearchOptions struct { Copy bool // if copy returned JSON to reduce memory usage Validate bool // if validate returned JSON for safty } @@ -235,12 +235,12 @@ type GetOptions struct { // // Note, the api expects the json is well-formed at least, // otherwise it may return unexpected result. -func GetValue(opts GetOptions, src []byte, path ...interface{}) (ast.Value, error) { - return GetValueFromString(opts, rt.Mem2Str(src), path...) +func (opts SearchOptions) Get(src []byte, path ...interface{}) (ast.Value, error) { + return opts.GetFromString(rt.Mem2Str(src), path...) } -// GetValueFromString is same with GetValue except src is string, -func GetValueFromString(opts GetOptions, src string, path ...interface{}) (ast.Value, error) { +// GetFromString is same with GetValue except src is string, +func (opts SearchOptions) GetFromString(src string, path ...interface{}) (ast.Value, error) { s := ast.NewSearcher(src) s.Validate(opts.Validate) s.Copy(opts.Validate)