nojson
A flexible Rust JSON library with no dependencies and no macros.
nojson is a flexible and ergonomic JSON library for Rust that offers a balance between the type-safety of Rust and the dynamic nature of JSON.
Unlike serde, which typically requires one-to-one mapping between Rust types and JSON structures (or other serialization formats),
nojson provides a toolbox approach that allows you to leverage both type-level programming and imperative code flexibility.
Features
- No strict one-to-one type mapping required - Mix type-level programming with imperative flexibility as needed
- Clean parsing error messages with position information for better debugging
- Customizable validation - Add application-specific validation rules with proper error context
- Flexible formatting options including pretty-printing with customizable indentation
- Low-level access to the JSON structure when needed
- High-level conveniences for common JSON operations
Core Design Principles
- A toolbox rather than a monolithic framework
- Gain the benefits of both type-level programming and imperative code
- Easy to add custom validations with rich error context
- Error messages that precisely indicate the problematic position in the JSON text
Getting Started
Parsing JSON with Strong Typing
The Json<T> wrapper allows parsing JSON text into Rust types that implement the FromRawJsonValue trait:
use Json;
Generating JSON
The DisplayJson trait allows converting Rust types to JSON:
use Json;
// Generate a JSON array from a Rust array
let value = ;
assert_eq!;
In-place JSON Generation with Formatting
The json() function provides a convenient way to generate JSON with custom formatting:
use json;
// Compact JSON
let compact = json;
assert_eq!;
// Pretty-printed JSON with custom indentation
let pretty = json;
assert_eq!;
Custom Types
Implementing DisplayJson and FromRawJsonValue for your own types:
use ;
Advanced Features
Custom Validations
You can add custom validations using JsonParseError::invalid_value():
use ;
Error Handling with Context
Rich error information helps with debugging:
use ;
let text = r#"{"invalid": 123e++}"#;
let result = parse;
if let Err = result