summaryrefslogtreecommitdiff
path: root/vendor/github.com/bytedance/sonic/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/bytedance/sonic/api.go')
-rw-r--r--vendor/github.com/bytedance/sonic/api.go32
1 files changed, 25 insertions, 7 deletions
diff --git a/vendor/github.com/bytedance/sonic/api.go b/vendor/github.com/bytedance/sonic/api.go
index 9525b7afd..54d9a2160 100644
--- a/vendor/github.com/bytedance/sonic/api.go
+++ b/vendor/github.com/bytedance/sonic/api.go
@@ -20,6 +20,7 @@ import (
`io`
`github.com/bytedance/sonic/ast`
+ `github.com/bytedance/sonic/internal/rt`
)
// Config is a combination of sonic/encoder.Options and sonic/decoder.Options
@@ -73,6 +74,9 @@ type Config struct {
// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler bool
+
+ // NoEncoderNewline indicates that the encoder should not add a newline after every message
+ NoEncoderNewline bool
}
var (
@@ -170,27 +174,41 @@ func UnmarshalString(buf string, val interface{}) error {
return ConfigDefault.UnmarshalFromString(buf, val)
}
-// Get searches the given path from json,
-// and returns its representing ast.Node.
+// Get searches and locates the given path from src json,
+// and returns a ast.Node representing the partially json.
//
// Each path arg must be integer or string:
// - Integer is target index(>=0), means searching current node as array.
// - String is target key, means searching current node as object.
//
//
-// Note, the api expects the json is well-formed at least,
-// otherwise it may return unexpected result.
+// Notice: It expects the src json is **Well-formed** and **Immutable** when calling,
+// otherwise it may return unexpected result.
+// Considering memory safty, the returned JSON is **Copied** from the input
func Get(src []byte, path ...interface{}) (ast.Node, error) {
- return GetFromString(string(src), path...)
+ return GetCopyFromString(rt.Mem2Str(src), path...)
}
-// GetFromString is same with Get except src is string,
-// which can reduce unnecessary memory copy.
+// GetFromString is same with Get except src is string.
+//
+// WARNING: The returned JSON is **Referenced** from the input.
+// Caching or long-time holding the returned node may cause OOM.
+// If your src is big, consider use GetFromStringCopy().
func GetFromString(src string, path ...interface{}) (ast.Node, error) {
return ast.NewSearcher(src).GetByPath(path...)
}
+// GetCopyFromString is same with Get except src is string
+func GetCopyFromString(src string, path ...interface{}) (ast.Node, error) {
+ return ast.NewSearcher(src).GetByPathCopy(path...)
+}
+
// Valid reports whether data is a valid JSON encoding.
func Valid(data []byte) bool {
return ConfigDefault.Valid(data)
}
+
+// Valid reports whether data is a valid JSON encoding.
+func ValidString(data string) bool {
+ return ConfigDefault.Valid(rt.Str2Mem(data))
+}