JSON Library
github.com/cybergodev/json is a Go JSON library for parsing, path queries, modification, validation, formatting, and streaming of large files and JSONL. Function signatures match encoding/json, so existing code compiles after swapping the import; input is validated by default, which introduces a few behavioral differences from the standard library (see Comparison with the Standard Library).
Core Features
- Signature-compatible with encoding/json —
Marshal/Unmarshal/Valid/Compact/Indentand theEncoder/Decodermethods share names and signatures with the standard library; swapping the import is enough to compile. Default input validation introduces a few behavioral differences. - Thread-safe — one
Processorcan be shared across goroutines; every public method is protected internally. - Path queries — dot notation, array indices, slices, wildcards, and JSON Pointer each express a lookup in one path string.
- Type-safe access —
GetTyped[T]for generic decoding;SafeGetreturns anAccessResultyou can chain conversions on. - Streaming —
StreamIterator/StreamObjectIteratordecode element by element; the JSONL family processes line by line. - Security — input size, nesting depth, invalid UTF-8, and dangerous patterns are validated by default.
- Caching — parse and result caches are on by default;
WarmupCachepre-fills hot paths. - Extensible —
HookinterceptsGet/Set/Delete; custom encoding goes throughMarshalJSON,MarshalText, orCustomEscapes.
Installation
bash
go get github.com/cybergodev/jsonRequires Go 1.25+ (the source go.mod declares go 1.25.0; examples use generics and the slices standard library).
30-Second Quick Tour
go
package main
import (
"fmt"
"github.com/cybergodev/json"
)
func main() {
data := `{"name": "CyberGo", "version": 1, "tags": ["json", "go"]}`
// 1. Get by path
name := json.GetString(data, "name")
fmt.Println("Name:", name)
// 2. Modify a value
updated, _ := json.Set(data, "version", 2)
fmt.Println("Updated:", updated)
// 3. Validate
if json.Valid([]byte(data)) {
fmt.Println("Valid JSON")
}
// 4. Get with a default value
desc := json.GetString(data, "description", "Default description")
fmt.Println("Description:", desc)
// 5. Decode into a struct
type Config struct {
Name string `json:"name"`
Version int `json:"version"`
Tags []string `json:"tags"`
}
var config Config
json.Unmarshal([]byte(data), &config)
fmt.Printf("Config: %+v\n", config)
}Feature Overview
Path Operations
| Feature | Functions | Description |
|---|---|---|
| Get values | Get, GetString, GetInt... | Nested paths and array indices supported |
| Batch get | GetMultiple | Retrieve values at multiple paths with a single parse |
| Get with default | GetString, GetInt, etc. | Pass a defaultValue argument |
| Set values | Set | Automatically creates missing paths by default (Config.CreatePaths) |
| Delete values | Delete | Deletes the specified path |
Encoding & Decoding
| Feature | Functions | Description |
|---|---|---|
| Encode | Marshal, MarshalIndent | Output bytes match the standard library (typical values) |
| Decode | Unmarshal, Parse, ParseAny | Generics and type safety supported |
| Format | Prettify, Compact | JSON pretty-print / compaction |
Advanced Features
| Feature | Functions/Types | Description |
|---|---|---|
| Generic API | GetTyped[T] | Type-safe generic get |
| Pre-parsing | Processor.PreParse, Processor.GetFromParsed | Parse once, query many times |
| Path pre-compilation | Processor.CompilePath, Processor.GetCompiled | Zero repeated parsing for high-frequency identical paths |
| Parallel iteration | NewParallelIterator | Parallel Map/Filter/ForEach with a built-in worker pool |
| Safe access | SafeGet → AccessResult | Chained type conversion |
| Streaming | StreamJSONL family | Line-by-line streaming with bounded memory |
| JSONL processing | StreamLinesInto[T] | Log/data pipelines |
| Schema validation | ValidateSchema | JSON Schema validation |
Module Navigation
| Module | Description |
|---|---|
| Quick Start | Installation, basic usage, core concepts |
| Path Expression Syntax | Path queries, slices, wildcards, field extraction |
| Processor Guide | When to use a Processor, pre-parse optimization, lifecycle |
| API Reference | Complete API reference |
| Large File Handling | Item-by-item file iteration, streaming iterators, read caps |
| Extension Interfaces | Hooks intercept the full Get/Set/Delete flow; custom encoding via MarshalJSON, MarshalText, or CustomEscapes |
| Security | Input validation on by default (size/depth/UTF-8/dangerous patterns); SecurityConfig() preset for untrusted input, with a production checklist |
| Examples | Runnable examples for paths, codecs, iteration, and JSONL |
| Advanced Examples | Batch encoding, field selection, pre-parsing, compiled paths, cache warm-up |
Performance Characteristics
- Zero-allocation fast paths — cache keys are comparable structs, so no string is concatenated per operation; with the cache off, single-key property access reads the root object directly and bypasses the recursive processor
- Caching — parse results and query results are cached by default (
EnableCache/CacheResults);WarmupCachepre-fills hot paths - Object pools —
IterableValue, encoder buffers, and sorted-key slices are reused to reduce GC pressure - Parallel streaming — JSONL streaming supports multiple parallel workers (
StreamJSONLParallel) - Pre-parsing —
PreParseparses one JSON document once and shares the parse tree across paths
Comparison with the Standard Library
| Feature | encoding/json | cybergodev/json |
|---|---|---|
| Basic encoding/decoding | ✅ | ✅ same signatures, plus default input validation |
| Path queries | ❌ | ✅ dot/bracket syntax |
| Type-safe access | ❌ | ✅ generic API |
| Streaming | basic streaming decoder | ✅ element/line iterators |
| JSONL support | ❌ | ✅ native support |
| Security validation | ❌ | ✅ on by default |
| Hook system | ❌ | ✅ extensible |
| Cache optimization | ❌ | ✅ parse and result caches |
Quick Decision Guide
| Scenario | Recommended Approach |
|---|---|
| Simple query | GetString(data, "path") |
| With a default value | GetString(data, "path", "default") |
| Type safety | GetTyped[User](data, "user") |
| Query many paths on one JSON | Processor + PreParse |
| Query one path across many JSONs | Processor + CompilePath |
| Large files | StreamIterator (>100MB) / Processor.ForeachFile (10-100MB, fully loaded) |
| Process large arrays in parallel | NewParallelIterator |
| JSONL pipelines | StreamLinesInto[T] |
| Untrusted input | SecurityConfig() |
Next Steps
- Quick Start — installation and a minimal example
- Processor Guide — when to use a processor
- Path Expression Syntax — the complete path syntax
- Examples — more runnable examples
- Security — built-in input validation and untrusted-input hardening