json - JSON Processing
Overview
The json module provides enhanced JSON handling with better error messages and platform-specific optimizations. It wraps the standard library JSON encoder/decoder with improved functionality.
Functions
Marshal()
Encode value to JSON.
Parameters:
v- Value to encode
Returns:
- JSON-encoded bytes
- Error if encoding fails
Example:
Unmarshal()
Decode JSON data into value.
Parameters:
data- JSON-encoded bytesv- Destination value (must be a pointer)
Returns:
- Error if decoding fails
Example:
MarshalString()
Encode value to JSON string.
Parameters:
v- Value to encode
Returns:
- JSON-encoded string
- Error if encoding fails
Example:
UnmarshalString()
Decode JSON string into value.
Parameters:
data- JSON-encoded stringv- Destination value (must be a pointer)
Returns:
- Error if decoding fails
Example:
NewEncoder()
Create a new JSON encoder.
Parameters:
w- Writer to encode to
Returns:
- JSON encoder
Example:
NewDecoder()
Create a new JSON decoder.
Parameters:
r- Reader to decode from
Returns:
- JSON decoder
Example:
Usage Patterns
HTTP API
File I/O
Configuration
Pretty Printing
Advanced Features
Custom Marshal/Unmarshal
Streaming Processing
Error Handling
Best Practices
Error Handling
Memory Efficiency
Performance
Platform-Specific Optimizations
The json module automatically selects the best implementation based on the platform:
Linux AMD64
Uses sonic JSON library for maximum performance:
- 3.5x faster than standard library
- Zero-allocation optimizations
- SIMD-accelerated parsing
Other Platforms
Uses standard library JSON with enhanced error messages:
- Compatible with all platforms
- Better error messages for debugging
- Standard library performance
Related Documentation
- candy - Type conversion
- stringx - String utilities
- anyx - Interface{} helpers
- API Documentation
- Module Overview