Files
CLIProxyAPI/internal/util/gjson.go
sususu ea8882bf26 Add a no-copy GJSON parse helper
gjson.ParseBytes copies the entire document before returning a result, which
is prohibitive for multi-megabyte requests. ParseGJSONBytesNoCopy mirrors the
existing GetGJSONBytesNoCopy contract: the returned result references the
caller's bytes, so callers must neither retain it nor mutate the buffer while
it is in use.
2026-08-09 00:40:57 +08:00

28 lines
837 B
Go

package util
import (
"unsafe"
"github.com/tidwall/gjson"
)
// GetGJSONBytesNoCopy returns a GJSON result that may reference data directly.
// Callers must not retain the result or mutate data while using it.
func GetGJSONBytesNoCopy(data []byte, path string) gjson.Result {
if len(data) == 0 {
return gjson.Result{}
}
return gjson.Get(unsafe.String(unsafe.SliceData(data), len(data)), path)
}
// ParseGJSONBytesNoCopy parses data into a GJSON result that references data
// directly. gjson.ParseBytes copies the whole document, which is prohibitive
// for multi-megabyte payloads. Callers must not retain the result or mutate
// data while using it.
func ParseGJSONBytesNoCopy(data []byte) gjson.Result {
if len(data) == 0 {
return gjson.Result{}
}
return gjson.Parse(unsafe.String(unsafe.SliceData(data), len(data)))
}