README
ΒΆ
JSON Library Configuration Usage Examples
This example demonstrates comprehensive configuration options for the github.com/cybergodev/json library, showing how to optimize the library for different use cases and requirements.
π― Demonstrated Features
1. Default Configuration Usage
- Global processor usage - Using the library with default settings
- Default processor creation - Creating processors with default configuration
- Configuration inspection - Examining default configuration values
2. Custom Configuration Setup
- Custom cache settings - Tailored cache size and TTL
- Size limits configuration - JSON size, path depth, and batch size limits
- Performance tuning - Concurrency and parallel processing settings
- Processing options - Path creation, null cleanup, and array compaction
3. Performance-Optimized Configuration
- Aggressive caching - Maximum cache size and extended TTL
- High concurrency - Optimized for multi-threaded environments
- Large capacity limits - Handling big JSON files and complex operations
- Metrics collection - Performance monitoring and health checks
4. Security-Focused Configuration
- Conservative limits - Strict size and depth limitations
- Rate limiting - Protection against abuse and DoS attacks
- Input validation - Comprehensive data validation
- Secure defaults - Preventing auto-modification of data
5. Cache Configuration Examples
- High-frequency access cache - Long TTL for stable data
- Memory-conscious cache - Small cache size with short TTL
- No-cache configuration - Disabling cache for dynamic data
- Cache performance comparison - Demonstrating cache effectiveness
6. Processor Options Examples
- Strict mode options - Rigorous parsing and validation
- Flexible mode options - Permissive parsing with auto-corrections
- Performance mode options - Speed-optimized settings
- Per-operation configuration - Different options for different operations
7. Configuration Validation and Monitoring
- Comprehensive metrics - Operation counts, success rates, timing
- Cache metrics - Hit rates, miss counts, efficiency
- Health monitoring - Processor status and lifecycle management
- Performance analysis - Detailed timing and throughput metrics
π Running the Example
cd examples/configuration
go run example.go
Expected Output
The example will demonstrate:
- β Different configuration approaches and their effects
- β Performance comparisons between configuration types
- β Cache behavior with different settings
- β Security features and rate limiting
- β Comprehensive metrics and monitoring data
π― Configuration Use Cases
π’ Production Environment
config := &json.Config{
EnableCache: true,
MaxCacheSize: 20000,
CacheTTL: 30 * time.Minute,
MaxConcurrency: 100,
EnableMetrics: true,
EnableHealthCheck: true,
EnableValidation: false, // Disable for performance
}
π Security-Critical Applications
config := &json.Config{
MaxJSONSize: 10 * 1024 * 1024, // 10MB limit
MaxPathDepth: 20, // Prevent deep nesting
MaxBatchSize: 100, // Limit batch operations
EnableValidation: true,
StrictMode: true,
EnableRateLimit: true,
RateLimitPerSec: 1000,
}
πΎ Memory-Constrained Environment
config := &json.Config{
EnableCache: true,
MaxCacheSize: 500, // Small cache
CacheTTL: 2 * time.Minute, // Short TTL
MaxJSONSize: 5 * 1024 * 1024, // 5MB limit
MaxConcurrency: 5, // Low concurrency
}
β‘ High-Performance Computing
config := &json.Config{
EnableCache: true,
MaxCacheSize: 50000, // Large cache
CacheTTL: 1 * time.Hour, // Long TTL
MaxConcurrency: 200, // High concurrency
ParallelThreshold: 2, // Aggressive parallelization
EnableValidation: false, // Skip validation for speed
}
π Configuration Options Reference
Cache Settings
EnableCache- Enable/disable result cachingMaxCacheSize- Maximum number of cached entriesCacheTTL- Time-to-live for cache entries
Performance Settings
MaxConcurrency- Maximum concurrent operationsParallelThreshold- Minimum items for parallel processingEnableMetrics- Enable performance metrics collectionEnableHealthCheck- Enable health monitoring
Security Settings
MaxJSONSize- Maximum JSON file size (bytes)MaxPathDepth- Maximum path nesting depthMaxBatchSize- Maximum batch operation sizeEnableRateLimit- Enable rate limitingRateLimitPerSec- Operations per second limit
Processing Settings
EnableValidation- Enable input validationStrictMode- Enable strict parsing modeCreatePaths- Auto-create missing paths in Set operationsCleanupNulls- Auto-remove null values after deletionsCompactArrays- Auto-compact arrays after modifications
π‘ Best Practices
π― Configuration Selection
- Start with defaults - Use
json.DefaultConfig()for most applications - Profile your usage - Monitor metrics to identify bottlenecks
- Tune incrementally - Adjust one setting at a time
- Test thoroughly - Validate configuration changes in staging
π Performance Optimization
- Enable caching - For repeated access to same data
- Tune cache size - Balance memory usage vs hit rate
- Adjust concurrency - Match your system's capabilities
- Monitor metrics - Use built-in performance monitoring
π Security Considerations
- Set size limits - Prevent resource exhaustion attacks
- Enable validation - Validate input data integrity
- Use rate limiting - Protect against abuse
- Disable auto-modifications - Prevent unintended data changes
π Monitoring and Maintenance
- Collect metrics - Monitor performance and health
- Set up alerts - Monitor cache hit rates and error rates
- Regular cleanup - Close processors when done
- Resource monitoring - Watch memory and CPU usage
π Related Examples
- basic - Learn basic operations first
- json_get - Advanced path expressions
- json_set - Complex modification operations
- flat_extraction - Advanced extraction techniques
π Related Documentation
- Main Documentation - Complete API documentation
- Chinese Documentation - Detailed Chinese documentation
- Compatibility Guide - Migration guide
Documentation
ΒΆ
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.