Skip to main content

Crate hedl_json

Crate hedl_json 

Source
Expand description

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

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. JSONPath query 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§

ErrorLocation
Location information for an error
FromJsonConfig
Configuration for JSON import
FromJsonConfigBuilder
Builder for FromJsonConfig
ParseError
Captured error during partial parsing
PartialConfig
Configuration for partial parsing
PartialConfigBuilder
Builder for PartialConfig
PartialResult
Result of partial parsing
ToJsonConfig
Configuration for JSON output

Enums§

ErrorTolerance
Error tolerance strategy for partial parsing
JsonConversionError
Errors that can occur during JSON to HEDL conversion
SurrogatePolicy
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::Value to HEDL Document
from_json_value_owned
Convert owned serde_json::Value to 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::Value with partial error recovery
to_json
Convert Document to JSON string
to_json_value
Convert Document to serde_json::Value