Thanks to visit codestin.com
Credit goes to lib.rs

4 releases

0.2.2 Dec 24, 2025
0.2.1 Dec 21, 2025
0.2.0 Dec 20, 2025
0.1.0 Dec 19, 2025

#537 in Encoding


Used in 2 crates

MIT license

130KB
2.5K SLoC

toon-core

DEPRECATED: Please use toon-ld instead.

This crate is no longer maintained. All functionality has been moved to the toon-ld crate, which provides a better user-facing API and naming scheme.

Core serialization and parsing logic for the TOON-LD (Token-Oriented Object Notation for Linked Data) format.

This crate provides the fundamental conversion algorithms between JSON-LD and TOON-LD formats, achieving 40-60% token reduction while maintaining full semantic compatibility with JSON-LD.

Features

  • JSON-LD to TOON-LD conversion with automatic tabular array optimization
  • TOON-LD to JSON-LD parsing with full round-trip compatibility
  • Context handling for URI compaction and expansion
  • Value node support for language tags and datatypes
  • Optimized parsing with efficient data structures
  • Comprehensive error handling with detailed error messages

Usage

Add this to your Cargo.toml:

[dependencies]
toon-core = "0.2"

Basic Example

use toon_core::{encode, decode};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Convert JSON-LD to TOON-LD
    let json_ld = r#"{
        "@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
        "foaf:name": "Alice"
    }"#;
    
    let toon = encode(json_ld)?;
    println!("TOON-LD:\n{}", toon);
    
    // Convert back to JSON-LD
    let back_to_json = decode(&toon)?;
    println!("JSON-LD:\n{}", back_to_json);
    
    Ok(())
}

Tabular Arrays

TOON-LD's key feature is efficient serialization of arrays of objects:

use toon_core::encode;

let json_ld = r#"{
    "@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
    "@graph": [
        {"@id": "ex:1", "foaf:name": "Alice", "foaf:age": 30},
        {"@id": "ex:2", "foaf:name": "Bob", "foaf:age": 25}
    ]
}"#;

let toon = encode(json_ld)?;
// Output uses tabular format:
// @graph[2]{@id,foaf:age,foaf:name}:
//   ex:1, 30, Alice
//   ex:2, 25, Bob

API Reference

Main Functions

  • encode(json: &str) -> Result<String, ToonError> - Convert (encode) JSON-LD to TOON-LD
  • decode(toon: &str) -> Result<String, ToonError> - Convert (decode) TOON-LD to JSON-LD
  • ToonParser::parse(toon: &str) -> Result<Value, ToonError> - Parse TOON-LD to serde_json::Value
  • ToonSerializer::serialize(value: &Value) -> Result<String, ToonError> - Serialize Value to TOON-LD

Error Handling

All functions return Result<T, ToonError> where ToonError provides detailed error messages including line numbers and context.

Performance

TOON-LD achieves:

  • 40-60% token reduction compared to JSON-LD
  • Efficient parsing with structured error handling
  • Optimized serialization with automatic tabular array detection

Specification

For the complete TOON-LD specification, see the main repository.

  • toon-cli - Command-line tool for TOON-LD conversion and benchmarking

License

MIT License - See LICENSE for details.

Dependencies

~2.6–4.5MB
~85K SLoC