hedl-json 1.0.0

HEDL to/from JSON conversion
Documentation

HEDL JSON Conversion

Provides bidirectional conversion between HEDL documents and JSON.

Features

  • Bidirectional Conversion: HEDL ↔ JSON with full fidelity
  • JSONPath Queries: Extract data using standard JSONPath expressions
  • 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 DoS attacks

Modules

  • [jsonpath]: JSONPath query engine for extracting specific data
  • [schema_gen]: JSON Schema generation from HEDL documents
  • [streaming]: 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};

# fn example() -> Result<(), Box<dyn std::error::Error>> {
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"));
# Ok(())
# }

JSON Schema Generation

use hedl_json::schema_gen::{generate_schema, SchemaConfig};

# fn example() -> Result<(), Box<dyn std::error::Error>> {
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
# Ok(())
# }

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
    }
}