lolly/gjson/config.go
xfy c37364b309 style: format code and modernize loop syntax
- Align struct fields and constants in gjson/config.go
- Add missing newline at EOF in gjson/decode.go
- Remove trailing blank line in gjson/encode.go
- Remove extra blank line in internal/lua/coroutine.go
- Use modern for range syntax in internal/lua/pool.go

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 17:34:59 +08:00

63 lines
2.0 KiB
Go

package gjson
// Default configuration values (matching lua-cjson defaults)
const (
defaultEncodeSparseConvert = false
defaultEncodeSparseRatio = 2
defaultEncodeSparseSafe = 10
defaultEncodeMaxDepth = 1000
defaultDecodeMaxDepth = 1000
defaultEncodeNumberPrecision = 14
defaultEncodeKeepBuffer = true
defaultEncodeSortKeys = false // 默认不排序,保持高性能
)
// sparseArrayConfig controls sparse array handling during encoding.
type sparseArrayConfig struct {
convert bool // true: convert to object, false: raise error
ratio int // sparse ratio threshold
safe int // safe threshold (arrays smaller than this are not checked)
}
// Config holds all configuration options for a GJSON instance.
type Config struct {
encodeSparseArray sparseArrayConfig
encodeMaxDepth int
decodeMaxDepth int
encodeNumberPrecision int
encodeKeepBuffer bool
encodeSortKeys bool // 是否对 object 键排序(稳定输出)
}
// defaultConfig returns a new Config with default values.
func defaultConfig() *Config {
return &Config{
encodeSparseArray: sparseArrayConfig{
convert: defaultEncodeSparseConvert,
ratio: defaultEncodeSparseRatio,
safe: defaultEncodeSparseSafe,
},
encodeMaxDepth: defaultEncodeMaxDepth,
decodeMaxDepth: defaultDecodeMaxDepth,
encodeNumberPrecision: defaultEncodeNumberPrecision,
encodeKeepBuffer: defaultEncodeKeepBuffer,
encodeSortKeys: defaultEncodeSortKeys,
}
}
// Clone returns a deep copy of the config.
func (c *Config) Clone() *Config {
return &Config{
encodeSparseArray: sparseArrayConfig{
convert: c.encodeSparseArray.convert,
ratio: c.encodeSparseArray.ratio,
safe: c.encodeSparseArray.safe,
},
encodeMaxDepth: c.encodeMaxDepth,
decodeMaxDepth: c.decodeMaxDepth,
encodeNumberPrecision: c.encodeNumberPrecision,
encodeKeepBuffer: c.encodeKeepBuffer,
encodeSortKeys: c.encodeSortKeys,
}
}