README
¶
JSON File Marshal/Unmarshal Example
This example demonstrates the new MarshalToFile and UnmarshalFromFile functions that provide convenient file I/O operations for JSON data.
Features Demonstrated
- MarshalToFile - Convert Go data structures to JSON and save to files
- UnmarshalFromFile - Read JSON files and convert to Go data structures
- Pretty formatting - Save JSON with indentation for readability
- Compact formatting - Save JSON without extra whitespace
- Processor methods - Use advanced processor for file operations
- Error handling - Proper error handling for common failure scenarios
Key Functions
Package-level Functions
// Marshal data to file (compact by default)
err := json.MarshalToFile("data.json", data)
// Marshal data to file with pretty formatting
err := json.MarshalToFile("data.json", data, true)
// Unmarshal data from file
var result MyStruct
err := json.UnmarshalFromFile("data.json", &result)
Processor Methods
processor := json.New()
defer processor.Close()
// Marshal with processor-specific configuration
err := processor.MarshalToFile("data.json", data, true)
// Unmarshal with processor options
err := processor.UnmarshalFromFile("data.json", &result, opts...)
Running the Example
# Run the example
go run -tags examples examples/marshal_file/example.go
# Or from the project root
go run -tags examples ./examples/marshal_file/
Expected Output
The example will:
- Create an
output/directory - Generate several JSON files with different formatting
- Demonstrate loading data back from files
- Show error handling for common failure cases
- Display the contents and operations performed
Generated Files
user_compact.json- User data in compact formatuser_pretty.json- User data with pretty formattingconfig.json- Configuration data as a mapadvanced.json- Data processed with advanced processor
Security Features
The file operations include built-in security features:
- Path validation - Prevents path traversal attacks
- Size limits - Enforces maximum file size limits
- Directory creation - Automatically creates parent directories
- Safe file permissions - Uses secure file permissions (0644)
Error Handling
The example demonstrates proper error handling for:
- Non-existent files
- Invalid file paths
- Nil unmarshal targets
- JSON parsing errors
- File system errors
Use Cases
These functions are ideal for:
- Configuration files - Loading and saving application settings
- Data persistence - Storing application state
- API responses - Caching JSON responses to files
- Data exchange - Sharing structured data between applications
- Backup and restore - Creating JSON backups of data structures
Performance Notes
- Files are read/written in a single operation for efficiency
- Directory creation is automatic and safe
- Memory usage is optimized for large files
- Thread-safe operations when using processor methods
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.