Skip to content

JSON Library

github.com/cybergodev/json is a high-performance, thread-safe Go JSON processing library. It provides rich JSON operations including parsing, querying, modifying, validating, and formatting, while maintaining 100% compatibility with the standard library encoding/json.

Core Features

  • 100% encoding/json Compatible — Seamless replacement for the standard library with no code changes required
  • Thread-Safe — All operations are concurrency-safe, supporting high-concurrency scenarios
  • Path Queries — JSONPath-style path expressions, including wildcards and slicing
  • Type-Safe Getters — Generic API (GetTyped[T]) and type assertion methods (SafeGet)
  • Stream Processing — Large file and JSONL/NDJSON streaming support
  • Security Protection — Built-in input validation, depth limits, and dangerous pattern detection
  • High-Performance Caching — Smart caching, pre-parse optimization, and object pool reuse
  • Extensible — Hook system, custom encoders, and validators

Installation

bash
go get github.com/cybergodev/json

30-Second Quick Start

go
package main

import (
    "fmt"
    "github.com/cybergodev/json"
)

func main() {
    data := `{"name": "CyberGo", "version": 1, "tags": ["json", "go"]}`

    // 1. Path query
    name := json.GetString(data, "name")
    fmt.Println("Name:", name)

    // 2. Modify 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 default value
    desc := json.GetString(data, "description", "Default description")
    fmt.Println("Description:", desc)

    // 5. Decode into 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

FeatureFunctionsDescription
Get valueGet, GetString, GetInt...Supports nested paths, array indices
Get with defaultGetString, GetInt, etc.Pass a defaultValue parameter
Set valueSetAutomatically creates non-existent paths by default (Config.CreatePaths)
Delete valueDeleteRemove value at specified path

Encoding & Decoding

FeatureFunctionsDescription
EncodeMarshal, MarshalIndent100% compatible with encoding/json
DecodeUnmarshal, Parse, ParseAnySupports generics and type safety
FormatPrettify, CompactJSON pretty-print / compact

Advanced Features

FeatureFunctions/TypesDescription
Generic APIGetTyped[T]Type-safe generic getter
Pre-parseProcessor.PreParse, Processor.GetFromParsedParse once, query many times
Safe accessSafeGetAccessResultChained type conversions
Stream processingProcessor.ForeachFileMemory-friendly for large files
JSONL processingStreamLinesInto[T]Log/data pipeline support
Schema validationValidateSchemaJSON Schema validation

Module Navigation

ModuleDescription
Getting StartedInstallation, basic usage, core concepts
Path Expression SyntaxPath queries, slicing, wildcards, field extraction
API ReferenceComplete API reference
Large File ProcessingStream processing, chunked read/write, memory optimization
Usage ExamplesPractical code examples
Advanced ExamplesBatch encoding, pre-parsing, hook system

Performance Features

  • Zero-Copy Parsing — Reduced memory allocations
  • Smart Caching — Automatic caching of hot paths with cache warmup support
  • Object Pool — Reuse of intermediate objects to reduce GC pressure
  • Parallel Processing — Automatic parallelization of batch operations
  • Pre-Parse Optimization — Parse large JSON once, query many times

Comparison with Standard Library

Featureencoding/jsoncybergodev/json
Basic encoding/decoding✅ 100% compatible
Path queries✅ Dot/bracket syntax
Type-safe getters✅ Generic API
Stream processingBasic✅ Enhanced
JSONL support✅ Native support
Security validation✅ Built-in protection
Hook system✅ Extensible
Cache optimization✅ Smart caching

Quick Decision Guide

ScenarioRecommended Approach
Simple queryGetString(data, "path")
With default valueGetString(data, "path", "default")
Type safetyGetTyped[User](data, "user")
High-frequency queriesProcessor + PreParse
Large filesProcessor.ForeachFile
Untrusted inputSecurityConfig()

Next Steps