Expand description
HEDL JSON Conversion
Provides bidirectional conversion between HEDL documents and JSON.
§Features
- Bidirectional Conversion: HEDL ↔ JSON with full fidelity
JSONPathQueries: Extract data using standardJSONPathexpressions- JSON Schema Generation: Generate JSON Schema Draft 7 from HEDL documents
- Partial Parsing: Continue parsing despite errors and collect all errors
- Streaming Support: Memory-efficient processing of large files
- JSONL Support: Newline-delimited JSON for logs and streaming
- Zero-Copy Optimization: Reduced allocations for better performance
- Security Limits: Configurable limits to prevent
DoSattacks
§Modules
jsonpath:JSONPathquery engine for extracting specific dataschema_gen: JSON Schema generation from HEDL documentsstreaming: Streaming parsers for large files and JSONL format
§Examples
§Basic Conversion
use hedl_json::{json_to_hedl, hedl_to_json};
let json = r#"{"name": "Alice", "age": 30}"#;
let doc = json_to_hedl(json).unwrap();
let json_out = hedl_to_json(&doc).unwrap();§JSONPath Queries
use hedl_json::jsonpath::{query, QueryConfig};
let doc = hedl_core::parse(b"name: \"Alice\"\nage: 30")?;
let config = QueryConfig::default();
// Extract specific fields
let results = query(&doc, "$.name", &config)?;
assert_eq!(results[0].as_str(), Some("Alice"));§JSON Schema Generation
use hedl_json::schema_gen::{generate_schema, SchemaConfig};
let doc = hedl_core::parse(b"name: \"Alice\"\nage: 30")?;
let config = SchemaConfig::builder()
.title("User Schema")
.strict(true)
.build();
let schema = generate_schema(&doc, &config)?;
// schema is a valid JSON Schema Draft 7 document§Streaming Large Files
use hedl_json::streaming::{JsonLinesStreamer, StreamConfig};
use std::io::Cursor;
let jsonl = "{\"id\": \"1\"}\n{\"id\": \"2\"}";
let reader = Cursor::new(jsonl.as_bytes());
let config = StreamConfig::default();
for result in JsonLinesStreamer::new(reader, config) {
let doc = result.unwrap();
// Process each document incrementally
}§Partial Parsing with Error Recovery
use hedl_json::{partial_parse_json, PartialConfig, ErrorTolerance};
let json = r#"{
"valid": "data",
"users": [
{"id": "1", "name": "Alice"},
{"id": "2", "name": "Bob"}
]
}"#;
let config = PartialConfig::builder()
.tolerance(ErrorTolerance::CollectAll)
.build();
let result = partial_parse_json(json, &config);
// Check if parsing completed successfully
if result.is_complete() {
let doc = result.document.unwrap();
// Use the document
} else {
// Handle errors
for error in &result.errors {
eprintln!("Error at {}: {}", error.location.path, error.error);
}
// Use partial results if available
if let Some(doc) = result.document {
// Process what was successfully parsed
}
}Modules§
- jsonpath
- JSONPath query engine for extracting specific data.
JSONPathquery support for HEDL documents - schema_
cache - Schema caching. Schema caching for JSON to HEDL conversion
- schema_
gen - JSON Schema generation from HEDL documents. JSON Schema (Draft 7) generation from HEDL documents.
- streaming
- Streaming JSON conversion. Streaming JSON parsing for HEDL
- string_
cache - String interning and caching. String interning cache for reducing allocations
- validation
- JSON schema validation. JSON Schema validation for hedl-json
Structs§
- Error
Location - Location information for an error
- From
Json Config - Configuration for JSON import
- From
Json Config Builder - Builder for
FromJsonConfig - Parse
Error - Captured error during partial parsing
- Partial
Config - Configuration for partial parsing
- Partial
Config Builder - Builder for
PartialConfig - Partial
Result - Result of partial parsing
- ToJson
Config - Configuration for JSON output
Enums§
- Error
Tolerance - Error tolerance strategy for partial parsing
- Json
Conversion Error - Errors that can occur during JSON to HEDL conversion
- Surrogate
Policy - Policy for handling unpaired UTF-16 surrogates in JSON input
Constants§
- DEFAULT_
MAX_ ARRAY_ SIZE - Default maximum array size for JSON parsing
- DEFAULT_
MAX_ DEPTH - Default maximum recursion depth for JSON parsing
- DEFAULT_
MAX_ OBJECT_ SIZE - Default maximum object size (number of keys)
- DEFAULT_
MAX_ STRING_ LENGTH - Default maximum string length for JSON parsing
Functions§
- from_
json - Convert JSON string to HEDL Document
- from_
json_ value - Convert
serde_json::Valueto HEDL Document - from_
json_ value_ owned - Convert owned
serde_json::Valueto HEDL Document with zero-copy optimization - hedl_
to_ json - Convert HEDL document to JSON string
- json_
to_ hedl - Convert JSON string to HEDL document
- partial_
parse_ json - Parse JSON string with partial error recovery
- partial_
parse_ json_ value - Parse
serde_json::Valuewith partial error recovery - to_json
- Convert Document to JSON string
- to_
json_ value - Convert Document to
serde_json::Value