Skip to content

Config ​

Config customizes the behavior of the Processor and of all JSON operations.

The Config Struct ​

go
type Config struct {
    // ===== Cache settings =====
    MaxCacheSize int           `json:"max_cache_size"` // Maximum cache entries
    CacheTTL     time.Duration `json:"cache_ttl"`      // Cache expiration time
    EnableCache  bool          `json:"enable_cache"`   // Whether to enable the cache
    CacheResults bool          `json:"cache_results"`  // Whether to cache operation results
    CacheSharedResults bool `json:"cache_shared_results"` // Share cached results (skips defensive deep copies; callers must not mutate the returned containers)

    // ===== Size limits =====
    MaxJSONSize  int64 `json:"max_json_size"`  // Maximum JSON size (bytes)
    MaxPathDepth int   `json:"max_path_depth"` // Maximum path depth
    MaxBatchSize int   `json:"max_batch_size"` // Maximum batch operation count

    // ===== Security limits =====
    MaxNestingDepthSecurity   int   `json:"max_nesting_depth"`           // Maximum nesting depth
    MaxSecurityValidationSize int64 `json:"max_security_validation_size"` // Maximum size for security validation
    MaxObjectKeys             int   `json:"max_object_keys"`             // Maximum object key count
    MaxArrayElements          int   `json:"max_array_elements"`          // Maximum array element count
    FullSecurityScan          bool  `json:"full_security_scan"`          // Enable full security scanning

    // ===== Concurrency =====
    MaxConcurrency    int `json:"max_concurrency"`    // Maximum concurrency
    ParallelThreshold int `json:"parallel_threshold"` // Parallel processing threshold
    MaxOperationsPerSecond int `json:"max_operations_per_second"` // Rate cap for controlled operations (0 = unlimited)

    // ===== Processing options =====
    EnableValidation bool `json:"enable_validation"` // Enable validation (deprecated, not read)
    StrictMode       bool `json:"strict_mode"`       // Strict mode (deprecated, not read)
    CreatePaths      bool `json:"create_paths"`      // Create paths automatically
    CleanupNulls     bool `json:"cleanup_nulls"`     // Clean up null values
    CompactArrays    bool `json:"compact_arrays"`    // Compact arrays
    ContinueOnError  bool `json:"continue_on_error"` // Continue on batch errors

    // ===== Input/output options =====
    AllowComments    bool `json:"allow_comments"`     // Allow comments (deprecated, not read)
    PreserveNumbers  bool `json:"preserve_numbers"`   // Preserve number precision
    ValidateInput    bool `json:"validate_input"`     // Validate input (deprecated, not read)
    ValidateFilePath bool `json:"validate_file_path"` // Validate file paths
    SkipValidation   bool `json:"skip_validation"`    // Skip validation (trusted input)
    DetectDuplicateKeys bool `json:"detect_duplicate_keys"` // Detect and reject duplicate keys in objects

    // ===== Encoding options =====
    Pretty          bool            `json:"pretty"`           // Pretty output
    Indent          string          `json:"indent"`           // Indentation string
    Prefix          string          `json:"prefix"`           // Prefix
    EscapeHTML      bool            `json:"escape_html"`      // HTML escaping
    SortKeys        bool            `json:"sort_keys"`        // Sort keys
    ValidateUTF8    bool            `json:"validate_utf8"`    // UTF-8 validation
    MaxDepth        int             `json:"max_depth"`        // Maximum encoding depth
    DisallowUnknown bool            `json:"disallow_unknown"` // Disallow unknown fields
    FloatPrecision  int             `json:"float_precision"`  // Float precision (-1 = automatic)
    FloatTruncate   bool            `json:"float_truncate"`   // Truncate floats
    DisableEscaping bool            `json:"disable_escaping"` // Disable escaping
    EscapeUnicode   bool            `json:"escape_unicode"`   // Unicode escaping
    EscapeSlash     bool            `json:"escape_slash"`     // Slash escaping
    EscapeNewlines  bool            `json:"escape_newlines"`  // Newline escaping
    EscapeTabs      bool            `json:"escape_tabs"`      // Tab escaping
    IncludeNulls    bool            `json:"include_nulls"`    // Include null values
    CustomEscapes   map[rune]string `json:"custom_escapes,omitempty"` // Custom escape mapping

    // ===== Observability =====
    EnableMetrics     bool `json:"enable_metrics"`      // Enable metrics collection
    EnableHealthCheck bool `json:"enable_health_check"` // Enable health checks

    // ===== Large file handling =====
    ChunkSize       int64 `json:"chunk_size"`       // Chunk size (deprecated, not read)
    MaxMemory       int64 `json:"max_memory"`       // Maximum memory usage
    BufferSize      int   `json:"buffer_size"`      // Buffer size
    SaveFileMode    os.FileMode `json:"save_file_mode,omitempty"` // Permission bits for newly created files (0 falls back to 0644)
    SamplingEnabled bool  `json:"sampling_enabled"` // Enable sampling (deprecated, not read)
    SampleSize      int   `json:"sample_size"`      // Sample count (deprecated, not read)

    // ===== JSONL configuration =====
    JSONLBufferSize    int   `json:"jsonl_buffer_size"`     // JSONL buffer size
    JSONLMaxLineSize   int   `json:"jsonl_max_line_size"`   // JSONL maximum line size
    JSONLSkipEmpty     bool  `json:"jsonl_skip_empty"`      // Skip empty lines
    JSONLSkipComments  bool  `json:"jsonl_skip_comments"`   // Skip comment lines
    JSONLContinueOnErr bool  `json:"jsonl_continue_on_err"` // Continue on errors
    JSONLWorkers       int   `json:"jsonl_workers"`         // JSONL parallel worker count
    JSONLChunkSize     int   `json:"jsonl_chunk_size"`      // JSONL chunk size
    JSONLMaxMemory     int64 `json:"jsonl_max_memory"`      // JSONL maximum memory

    // ===== Merge options =====
    MergeMode MergeMode `json:"merge_mode"` // Merge strategy

    // ===== File access security =====
    AllowedFileDirs []string `json:"allowed_file_dirs,omitempty"` // Directory whitelist for file operations (empty = built-in blocking only)

    // ===== Extension points (no JSON tags, not serialized) =====
    CustomEncoder               CustomEncoder                // Custom encoder
    CustomTypeEncoders          map[reflect.Type]TypeEncoder // Custom type encoders
    CustomValidators            []Validator                  // Custom validators
    AdditionalDangerousPatterns []DangerousPattern           // Additional dangerous patterns
    DisableDefaultPatterns      bool                         // Disable default warning-level patterns
    Hooks                       []Hook                       // Operation hooks
    CustomPathParser            PathParser                   // Custom path parser
}

The CacheSharedResults contract

When CacheSharedResults is true, cache hits in Get/GetFromParsed return the cached value directly, skipping the defensive deep copy (faster, fewer allocations). In that case callers must not mutate the returned map[string]any/[]any; otherwise the shared cache is corrupted and subsequent reads are affected. Primitive values (bool, float64, string, json.Number, nil) are immutable and always safe. The default false keeps the safe "copy on read" behavior; enable it only when callers treat results as read-only (e.g. read-heavy workloads repeatedly reading the same large subtree).

Extension field wiring status

The three interface fields CustomEncoder, CustomTypeEncoders, and CustomValidators are declared in the current version but not yet wired into the encode/operation pipelines — setting them has no effect; they are reserved for future releases. CustomPathParser is wired: once set, all path-based operations call the custom parser in place of the built-in one (see Interface Definitions). Currently available extension points:

  • Fine-tuning encoding behavior → CustomEscapes (effective, see the encoding options below) or implementing json.Marshaler/encoding.TextMarshaler (see Custom Encoders)
  • Operation interception → Hooks + AddHook (effective, see the Hook System)
  • Custom path syntax → CustomPathParser (effective, see Interface Definitions)
  • Input validation → ValidateSchema (see Schema Validation)

Config Field Reference ​

All exported Config fields are listed below, grouped by purpose. Defaults are taken from DefaultConfig(); valid ranges and auto-correction rules per field are in Clamping ranges at a glance.

Cache ​

FieldTypeDefaultDescription
MaxCacheSizeint128Maximum cache entries (0 disables the cache)
CacheTTLtime.Duration5 minutesCache entry lifetime
EnableCachebooltrueWhether to enable the cache
CacheResultsbooltrueWhether to cache operation results (cached per operation)
CacheSharedResultsboolfalseCache hits return the shared value directly, skipping the defensive deep copy; callers must not mutate the returned containers (contract in the warning above)

Size Limits ​

FieldTypeDefaultDescription
MaxJSONSizeint64100MBMaximum JSON input size (bytes)
MaxPathDepthint50Maximum path depth
MaxBatchSizeint2000Cap on operations per batch (exceeding it is rejected, preventing memory exhaustion)

Security Limits ​

FieldTypeDefaultDescription
MaxNestingDepthSecurityint200Maximum nesting depth
MaxSecurityValidationSizeint6410MBMaximum size for security validation (above it, scanning follows the sampling strategy unless FullSecurityScan is true)
MaxObjectKeysint100000Maximum key count per object
MaxArrayElementsint100000Maximum element count per array
FullSecurityScanboolfalseWhen true, full (non-sampled) security scanning for all input; when false, large input (>4KB) uses rolling-window + sampled scanning, while critical patterns (__proto__, etc.) are still fully scanned

Concurrency ​

FieldTypeDefaultDescription
MaxConcurrencyint50Maximum concurrency
ParallelThresholdint10Parallel processing threshold (falls back to sequential below this element count)
MaxOperationsPerSecondint0 (no rate limit)Rate cap for controlled operations (Get/Set/Delete, etc.): a new operation arriving less than 1/MaxOperationsPerSecond seconds after the previous one is rejected with a rate-limit error. Validate clamps it to [0, 1000000]

Processing Options ​

FieldTypeDefaultDescription
EnableValidationbooltrueDeprecated: not read by any operation (validation always runs by default unless SkipValidation); only takes part in the config cache key
StrictModeboolfalseDeprecated: not read by any parser or operation; setting it changes nothing — it only takes part in the config cache key
CreatePathsbooltrueCreate missing intermediate paths automatically on Set
CleanupNullsboolfalseClean up null values
CompactArraysboolfalseCompact arrays
ContinueOnErrorboolfalseContinue when a single batch item fails

Input/Output Options ​

FieldTypeDefaultDescription
AllowCommentsboolfalseDeprecated: not read by any parser (comment lines in JSONL are controlled separately by JSONLSkipComments); only takes part in the config cache key
PreserveNumbersboolfalseDecoding preserves number literals (1.10 does not become 1.1); encoding writes them back verbatim
ValidateInputbooltrueDeprecated: not read by any operation; setting it changes nothing — it only takes part in the config cache key
ValidateFilePathbooltrueValidate file paths
SkipValidationboolfalseSkip non-essential validation (trusted input only)
DetectDuplicateKeysboolfalseRejects any input object containing duplicate keys, returning an error wrapping ErrDuplicateKey. The default false matches encoding/json (the last duplicate key wins); when explicitly enabled it applies even with SkipValidation. Keys are compared by raw bytes (different escape spellings count as different keys)

Encoding Options ​

FieldTypeDefaultDescription
PrettyboolfalsePretty output
Indentstring" " (two spaces)Indentation string
Prefixstring"" (empty)Prefix for every line
EscapeHTMLbooltrueHTML character escaping (< > &)
SortKeysboolfalseSort object keys in output
ValidateUTF8booltrueUTF-8 validation (reserved field, does not change behavior currently)
MaxDepthint100Maximum encoding depth (0 means unlimited)
DisallowUnknownboolfalseDisallow unknown fields when decoding (equivalent to Decoder.DisallowUnknownFields())
FloatPrecisionint-1Float precision (-1 automatic; 0–15 explicit)
FloatTruncateboolfalseTruncate directly instead of rounding when precision is active
DisableEscapingboolfalseDisable escaping logic (use only for fully controlled output)
EscapeUnicodeboolfalseNon-ASCII characters become \uXXXX
EscapeSlashboolfalse/ is escaped as \/
EscapeNewlinesbooltrueNewline escaping
EscapeTabsbooltrueTab escaping
IncludeNullsbooltrueInclude fields with null values when encoding
CustomEscapesmap[rune]stringnilCustom character escape mapping

Observability ​

FieldTypeDefaultDescription
EnableMetricsboolfalseEnable metrics collection (GetStats / GetHealthStatus need it to have data)
EnableHealthCheckboolfalseEnable health checks (reserved field, does not change behavior currently)

Large File Handling (Sampling and Streaming) ​

FieldTypeDefaultDescription
ChunkSizeint641MBDeprecated: not read by any code path (streaming reads use BufferSize); only takes part in the config cache key
MaxMemoryint64100MBMaximum memory for large-file processing
BufferSizeint64KBLarge-file read buffer size
SaveFileModeos.FileMode0 (falls back to 0644)Permission bits for files newly created by SaveToFile / MarshalToFile; existing files keep their permissions (consistent with os.WriteFile). Prefer 0600 for sensitive data; Validate masks it to permission bits ≤ 0777
SamplingEnabledbooltrueDeprecated: not read (whether security scanning samples is decided by FullSecurityScan and MaxSecurityValidationSize); only takes part in the config cache key
SampleSizeint1000Deprecated: not read by any code path; only takes part in the config cache key

JSONL Configuration ​

FieldTypeDefaultDescription
JSONLBufferSizeint64KBJSONL read buffer size
JSONLMaxLineSizeint1MBMaximum size of a single JSONL line
JSONLSkipEmptybooltrueSkip empty lines
JSONLSkipCommentsboolfalseSkip # / // comment lines
JSONLContinueOnErrboolfalseContinue with subsequent lines on parse errors
JSONLWorkersint4JSONL parallel worker count
JSONLChunkSizeint1000Batch chunk size (lines)
JSONLMaxMemoryint64100MBMaximum memory for JSONL processing

Merge Options ​

FieldTypeDefaultDescription
MergeModeMergeModeMergeUnionMerge strategy of MergeJSON / MergeMany (see Merge Modes)

File Access Security ​

FieldTypeDefaultDescription
AllowedFileDirs[]stringemptyDirectory whitelist for path-based file APIs: when non-empty, every path passed to LoadFromFile, UnmarshalFromFile, SaveToFile, MarshalToFile, the ForeachFile* family, and StreamJSONLFile (after symlink resolution) must be inside one of the listed directories, or ErrSecurityViolation is returned. The whitelist applies in addition to the built-in platform block list. Entries must be absolute paths (Validate resolves relative paths to absolute ones with a warning; empty entries are dropped); matching is by prefix on the cleaned path. A non-empty list in a per-call cfg overrides the processor's own list; the deprecated NDJSONProcessor entry points do not honor the whitelist
go
cfg := json.DefaultConfig()
// Only allow access to files under /var/lib/myapp/data
cfg.AllowedFileDirs = []string{"/var/lib/myapp/data"}

Extension Points ​

The following fields have no JSON tags and are not serialized; wiring status in the warning above.

FieldTypeDefaultDescription
CustomEncoderCustomEncodernilCustom encoder replacing the default encoder (reserved)
CustomTypeEncodersmap[reflect.Type]TypeEncodernilEncoders registered per Go type (reserved)
CustomValidators[]ValidatornilCustom validators run before operations (reserved)
AdditionalDangerousPatterns[]DangerousPatternnilDangerous patterns appended on top of the built-in ones
DisableDefaultPatternsboolfalseDisable built-in warning-level patterns (critical patterns are always enforced and cannot be disabled)
Hooks[]HooknilBefore/after operation hooks (effective)
CustomPathParserPathParsernilCustom path parser (effective; replaces the built-in path parser)

Encoding Options in Detail ​

Encoding options (Pretty/Indent/EscapeHTML/SortKeys/FloatPrecision, etc.) control the output shape of the Marshal/Encode families. Defaults align with the standard library (e.g. EscapeHTML: true, IncludeNulls: true); whenever any "non-default" encoding option is set, the library automatically switches to its custom encoder path internally — no manual intervention needed.

Key Sorting (SortKeys) ​

go
cfg := json.DefaultConfig()
cfg.SortKeys = true
s, _ := json.Encode(map[string]any{"b": 2, "a": 1}, cfg)
// Output: {"a":1,"b":2} — stable key order, convenient for comparison and testing

Float Precision (FloatPrecision / FloatTruncate) ​

go
cfg := json.DefaultConfig()
cfg.FloatPrecision = 2 // -1 (default) = automatic precision
s, _ := json.Encode(3.14159265, cfg)
// Output: 3.14

FloatTruncate controls how rounding happens when FloatPrecision is active: the default (false) applies standard round-half; setting it to true switches to direct truncation — with precision 2, 3.999 outputs 3.99 (rounding would give 4.00). It only takes part in encoding when FloatPrecision >= 0.

Custom Character Escaping (CustomEscapes) ​

go
cfg := json.DefaultConfig()
cfg.CustomEscapes = map[rune]string{
    '<': "&lt;",
}
s, _ := json.Encode("<a>", cfg)
// Output: "&lt;a>"

Common Toggles at a Glance ​

OptionDefaultEffect when toggled/set true
EscapeHTMLtruefalse: < > & are not escaped
EscapeUnicodefalsetrue: non-ASCII characters become \uXXXX
EscapeSlashfalsetrue: / is escaped as \/
EscapeNewlines / EscapeTabstruefalse: control characters pass through raw (use with care when producing JSONL-friendly text)
DisableEscapingfalsetrue: skips escaping logic entirely (use only for fully controlled output)
IncludeNullstruefalse: fields with null values are omitted when encoding
PreserveNumbersfalsetrue: decoding preserves number literals (1.10 does not become 1.1), written back verbatim when encoding

Input and Observability Toggles ​

The following fields control decoding strictness and runtime observability (defaults in DefaultConfig):

FieldDefaultScopeDescription
DisallowUnknownfalseNewDecoder(r, cfg)When true, equivalent to calling DisallowUnknownFields() on the returned Decoder — decoding errors on unknown fields
ValidateUTF8trueReservedDoes not change behavior currently (see note below)
AllowCommentsfalseDeprecatedNot read by any parser (see note below)
EnableMetricsfalseAt New(cfg) constructionWhen true, creates a metrics collector so GetStats operation/error counters and the per-check results of GetHealthStatus have data
EnableHealthCheckfalseDeprecatedNot read (see note below)
SamplingEnabledtrueDeprecatedNot read (see note below)

Reserved/deprecated field notes

The following fields are declared and take part in config comparison and hashing (so they do distinguish processor caches for different cfgs), but are not read by the corresponding pipelines — setting them changes nothing (ValidateUTF8 is a reserved field; the others are all marked Deprecated in the source — all are kept within v1):

  • EnableValidation: validation always runs by default unless SkipValidation is true — this toggle is not read by any operation.
  • StrictMode: not read by any parser or operation.
  • AllowComments: does not make parsing accept // or # comments; comment lines in JSONL files are controlled separately by JSONLSkipComments.
  • ValidateInput: not read by any operation.
  • EnableHealthCheck: health checks are on-demand via GetHealthStatus; whether there is data depends on EnableMetrics (see Lifecycle and Statistics).
  • ChunkSize: not read by any code path; streaming reads actually use BufferSize.
  • SamplingEnabled / SampleSize: whether security scanning of large inputs is sampled is decided by FullSecurityScan and MaxSecurityValidationSize, unrelated to these two fields.
  • ValidateUTF8: on the input side, rejecting invalid UTF-8 is unconditional security behavior; on the output side, standard-library semantics apply — neither is affected by this toggle.

Configuration Presets ​

DefaultConfig ​

Signature: func DefaultConfig() Config

Returns the default configuration, suitable for most scenarios.

go
cfg := json.DefaultConfig()
processor, err := json.New(cfg)
if err != nil {
    panic(err)
}
defer processor.Close()

Defaults

FieldValueDescription
MaxJSONSize100MBJSON size limit
MaxNestingDepthSecurity200Nesting depth
MaxPathDepth50Path depth
MaxSecurityValidationSize10MBSecurity validation size cap
MaxObjectKeys100000Maximum object key count
MaxArrayElements100000Maximum array element count
MaxConcurrency50Concurrency
ParallelThreshold10Falls back to sequential processing below this element count
MaxOperationsPerSecond0Rate cap for controlled operations (0 = no limit)
MaxBatchSize2000Batch operation count
CacheTTL5 minutesCache expiration
MaxCacheSize128Maximum cache entries
EnableCachetrueEnable cache
CacheResultstrueCache operation results
CacheSharedResultsfalseShare cached results (high-performance read-only scenarios)
EnableValidationtrueEnable validation
StrictModefalseNon-strict mode
FullSecurityScanfalseSampled security scanning (not full)
ValidateInputtrueValidate input (deprecated, not read)
ValidateFilePathtrueValidate file paths
DetectDuplicateKeysfalseNo duplicate-key detection (last duplicate key wins)
CreatePathstrueCreate paths automatically
PrettyfalseNo pretty output
EscapeHTMLtrueHTML escaping
ValidateUTF8trueUTF-8 validation
IncludeNullstrueInclude nulls
EscapeNewlinestrueNewline escaping
EscapeTabstrueTab escaping
FloatPrecision-1Automatic precision
MaxDepth100Encoding depth
Indent" "Default indentation
ChunkSize1MBChunk size (deprecated, not read)
MaxMemory100MBMaximum memory
BufferSize64KBBuffer size
SaveFileMode0Permission bits for newly created files (0 falls back to 0644)
SamplingEnabledtrueEnable sampling (deprecated, not read)
SampleSize1000Sample count (deprecated, not read)
JSONLBufferSize64KBJSONL buffer size
JSONLMaxLineSize1MBJSONL maximum line size
JSONLSkipEmptytrueSkip empty lines
JSONLSkipCommentsfalseDo not skip comments
JSONLContinueOnErrfalseStop on errors
JSONLWorkers4Parallel worker count
JSONLChunkSize1000JSONL chunk size
JSONLMaxMemory100MBJSONL maximum memory
MergeModeMergeUnionUnion merge

SecurityConfig ​

Signature: func SecurityConfig() Config

Returns the security configuration, suitable for handling untrusted input.

go
// Recommended for:
// - Public APIs and web services
// - User-submitted data
// - External webhooks
// - Authentication endpoints
// - Financial data processing
cfg := json.SecurityConfig()
processor, err := json.New(cfg)
if err != nil {
    panic(err)
}
defer processor.Close()

Security configuration characteristics

FieldValueDescription
MaxNestingDepthSecurity30Conservative nesting depth
MaxSecurityValidationSize10MBSecurity validation size
MaxObjectKeys5000Conservative key limit
MaxArrayElements5000Conservative element limit
MaxJSONSize10MBConservative size limit
MaxPathDepth30Conservative path depth
FullSecurityScantrueFull security scanning
StrictModetrueStrict mode
EnableValidationtrueEnable validation
EnableCachetrueEnable cache
MaxCacheSize256Cache size
CacheTTL3 minutesShorter TTL

PrettyConfig ​

Signature: func PrettyConfig() Config

Returns the pretty-print configuration.

go
result, err := json.Encode(data, json.PrettyConfig())

Config Methods ​

Clone ​

Signature: func (c *Config) Clone() *Config

Deep-copies the configuration (Config.Clone returns a new *Config; modifying the copy does not affect the original). Value fields are copied one by one; among reference fields, the two maps CustomEscapes and CustomTypeEncoders and the three slices CustomValidators, AdditionalDangerousPatterns, and Hooks are deep-copied and independently modifiable; interface fields (CustomEncoder, CustomPathParser) are shallow-copied (typically stateless or singleton implementations). Calling on a nil Config returns nil.

go
cfg := json.DefaultConfig()
cfgCopy := cfg.Clone()
cfgCopy.EnableValidation = true // Does not affect the original config

Validate ​

Signature: func (c *Config) Validate() error

Validates the configuration and auto-corrects invalid values. This method modifies the Config in place, clamping illegal fields into the valid range: too small (≤0) is raised to the minimum, too large (above the cap) is lowered to the maximum. Calling on a nil Config returns an error. After correction it always returns nil — use ValidateWithWarnings to see what was corrected.

go
cfg := json.DefaultConfig()
cfg.MaxJSONSize = -1 // invalid value
if err := cfg.Validate(); err != nil {
    panic(err)
}
// MaxJSONSize is corrected in place to the minimum

ValidateWithWarnings ​

Signature: func (c *Config) ValidateWithWarnings() []ConfigWarning

Validates the configuration and returns the list of correction warnings.

go
cfg := json.DefaultConfig()
cfg.MaxJSONSize = -1
warnings := cfg.ValidateWithWarnings()
for _, w := range warnings {
    fmt.Printf("%s: %s\n", w.Field, w.Reason)
}

Clamping ranges at a glance (auto-correction rules of Validate/ValidateWithWarnings; ≤0 counts as invalid and gets the lower bound, above the cap gets the upper bound):

FieldValid range (lower–upper)Notes
MaxJSONSize1MB – 100MBint64
MaxPathDepth10 – 200
MaxNestingDepthSecurity10 – 200
MaxConcurrency1 – 200≤0 resets to the default 50 (not the lower bound 1)
ParallelThreshold1 – 50
MaxObjectKeys100 – 100000
MaxArrayElements100 – 100000
MaxSecurityValidationSize1MB – 100MBint64
MaxBatchSize10 – 10000
MaxCacheSize0 – 2000 (only negatives are invalid)When negative, set to 0 and EnableCache is also turned off (0 disables the cache, which is legal)
CacheTTL≤0 invalid, reset to DefaultCacheTTL (5 minutes)
MaxDepth[0, 1000], out-of-range reset to 1000 means unlimited depth; negative is invalid
FloatPrecision[-1, 15], out-of-range reset to -1-1 is the automatic-precision sentinel
ChunkSize64KB – 100MB
MaxMemory10MB – 1GB
BufferSize4KB – 1MB
SampleSize100 – 10000
JSONLBufferSize4KB – 1MB
JSONLMaxLineSize1KB – 100MB
JSONLWorkers1 – 64
JSONLChunkSize100 – 10000
JSONLMaxMemory10MB – 1GBint64
MaxOperationsPerSecond[0, 1000000], negatives reset to 0 (disables rate limiting)Rate cap
SaveFileModeMasked to permission bits (≤ 0777)Extra bits such as os.ModeDir are masked with a warning
AllowedFileDirsEntries normalizedRelative paths resolved to absolute (with a warning); empty or unresolvable entries dropped (with a warning)

The ConfigWarning Type ​

ConfigWarning describes an auto-correction made during configuration validation.

go
type ConfigWarning struct {
    Field    string // Name of the corrected field
    OldValue any    // Original value (may be nil for invalid values)
    NewValue any    // Corrected value
    Reason   string // Why it was corrected
}
FieldTypeDescription
FieldstringName of the corrected field (e.g. MaxJSONSize)
OldValueanyValue before correction (may be nil for invalid values)
NewValueanyValue after correction
ReasonstringReason for the correction (e.g. below the lower bound, above the upper bound)

Security limits have no separate aggregate type — they are provided directly as the following Config fields:

FieldTypeDefaultDescription
MaxJSONSizeint64100MBCap on input JSON size (ErrSizeLimit)
MaxPathDepthint50Cap on path segment count (ErrInvalidPath)
MaxBatchSizeint2000Cap on operations per batch (ErrSizeLimit)
MaxNestingDepthSecurityint200 (DefaultMaxNestingDepth, Validate clamps to 10–200)Cap on input nesting depth (ErrDepthLimit); semantically distinct from MaxDepth, the encoding/decoding nesting depth (default 100) — do not confuse them
MaxSecurityValidationSizeint6410MBSize threshold for security validation (threshold type)
MaxObjectKeysint100000Maximum object key count (ErrSizeLimit)
MaxArrayElementsint100000Maximum array element count (ErrSizeLimit)
FullSecurityScanboolfalse (tiered scanning)Whether to fully security-scan all input (set to true for full scanning)

AddHook ​

Signature: func (c *Config) AddHook(hook Hook)

Adds an operation hook.

go
cfg := json.DefaultConfig()
cfg.AddHook(json.LoggingHook(slog.Default()))

AddValidator ​

Deprecated

Signature: func (c *Config) AddValidator(validator Validator)

Adds a custom validator (appended to CustomValidators).

Deprecated

Validators added via AddValidator are never executed by any public operation (CustomValidators is not wired into the operation pipeline). For custom pre-operation checks, use AddHook with a Before hook instead (e.g. ValidationHook). This method is kept within v1 and will not be removed.

go
cfg := json.DefaultConfig()
cfg.AddValidator(&MyValidator{}) // Deprecated: the validator is never executed

AddDangerousPattern ​

Signature: func (c *Config) AddDangerousPattern(pattern DangerousPattern)

Adds an extra security pattern.

go
cfg := json.DefaultConfig()
cfg.AddDangerousPattern(json.DangerousPattern{
    Pattern: "eval(",
    Name:    "eval-call",
    Level:   json.PatternLevelCritical,
})

Usage Examples ​

Basic Usage ​

go
cfg := json.DefaultConfig()
processor, err := json.New(cfg)
if err != nil {
    panic(err)
}
defer processor.Close()

Security Configuration ​

go
// Handling untrusted input
cfg := json.SecurityConfig()
processor, err := json.New(cfg)
if err != nil {
    panic(err)
}
defer processor.Close()

Pretty Output ​

go
// Pretty-print JSON
result, err := json.Encode(data, json.PrettyConfig())

Custom Configuration ​

go
cfg := json.DefaultConfig()

// Security settings
cfg.MaxJSONSize = 10 * 1024 * 1024 // 10MB
cfg.MaxNestingDepthSecurity = 50
cfg.EnableValidation = true

// Hooks
cfg.Hooks = []json.Hook{json.LoggingHook(slog.Default())}

// Validators
cfg.CustomValidators = []json.Validator{&MyValidator{}}

processor, err := json.New(cfg)
if err != nil {
    panic(err)
}
defer processor.Close()

Clone and Modify ​

go
// Create variants from the default configuration
base := json.DefaultConfig()

// Variant 1: development configuration
devCfg := base.Clone()
devCfg.EnableMetrics = true

// Variant 2: production configuration
prodCfg := base.Clone()
prodCfg.EnableValidation = true

Configuration Constants ​

go
const (
    // Size limits
    DefaultMaxJSONSize       = 100 * 1024 * 1024  // 100MB
    DefaultMaxNestingDepth   = 200
    DefaultMaxPathDepth      = 50
    DefaultMaxDepth          = 100                 // Default encode/decode nesting depth (Config.MaxDepth)
    DefaultMaxConcurrency    = 50
    DefaultMaxBatchSize      = 2000
    DefaultMaxSecuritySize   = 10 * 1024 * 1024   // 10MB
    DefaultMaxObjectKeys     = 100000
    DefaultMaxArrayElements  = 100000
    DefaultParallelThreshold = 10

    // Cache
    DefaultCacheTTL = 5 * time.Minute
)

Internal constants

Path validation length limits (maxPathLength) and similar constants have moved to internal implementation and are no longer exported as public API. Their defaults are reflected in the field defaults of the Config struct.


Merge Modes ​

MergeMode controls the merge strategy of the MergeJSON and MergeMany functions.

MergeMode implements fmt.Stringer: func (m MergeMode) String() string returns "union" / "intersection" / "difference" (unknown values return "unknown(N)"), convenient for logging and debug output.

ConstantValueString() returns
MergeUnion0"union"
MergeIntersection1"intersection"
MergeDifference2"difference"

MergeUnion (default) ​

Merges all keys/elements; on conflict the overriding value wins.

go
cfg := json.DefaultConfig()
cfg.MergeMode = json.MergeUnion
result, err := json.MergeJSON(
    `{"a": 1, "b": 2}`,
    `{"b": 3, "c": 4}`,
    cfg,
)
// Result: {"a": 1, "b": 3, "c": 4}

MergeIntersection ​

Keeps only keys present in both objects.

go
cfg := json.DefaultConfig()
cfg.MergeMode = json.MergeIntersection
result, err := json.MergeJSON(
    `{"a": 1, "b": 2}`,
    `{"b": 3, "c": 4}`,
    cfg,
)
// Result: {"b": 3}

MergeDifference ​

Keeps only keys present in the base object but absent from the overriding object.

go
cfg := json.DefaultConfig()
cfg.MergeMode = json.MergeDifference
result, err := json.MergeJSON(
    `{"a": 1, "b": 2}`,
    `{"b": 3, "c": 4}`,
    cfg,
)
// Result: {"a": 1}

Security Recommendations ​

SettingRecommended valueNotes
MaxJSONSize10-100MBTune to your server memory
MaxNestingDepthSecurity30-50Prevents deep-nesting attacks
MaxPathDepth30-50Limits path complexity
SkipValidationfalseKeep input validation on (it is on by default — do not turn it off; EnableValidation is deprecated and not read)
FullSecurityScantrue (untrusted input)Full security scanning

See Also ​