Skip to main content

Crate serde_toon2

Crate serde_toon2 

Source
Expand description

Serde-compatible serializer and deserializer for TOON (Token-Oriented Object Notation).

TOON is a line-oriented, indentation-based format that encodes the JSON data model. It emphasizes human readability with minimal punctuation while maintaining compatibility with the JSON type system.

§Features

  • Indentation-based structure: Objects use indentation instead of braces
  • Minimal quoting: Strings quoted only when necessary
  • Array headers: Declare length and optional field lists
  • Multiple delimiters: Arrays can use comma, tab, or pipe delimiters
  • Serde integration: Full compatibility with Rust’s serde ecosystem

§Usage

§Serialization

use serde::Serialize;
use serde_toon2::to_string;

#[derive(Serialize)]
struct User {
    id: u64,
    name: String,
}

let user = User {
    id: 42,
    name: "Ada".to_string()
};

let toon = to_string(&user).unwrap();
assert_eq!(toon, "id: 42\nname: Ada");

§Deserialization

use serde::Deserialize;
use serde_toon2::from_str;

#[derive(Deserialize, Debug, PartialEq)]
struct User {
    id: u64,
    name: String,
}

let toon = "id: 42\nname: Ada";
let user: User = from_str(toon).unwrap();

assert_eq!(user, User { id: 42, name: "Ada".to_string() });

§Format Examples

§Simple Object

name: Ada
age: 42
active: true

§Nested Objects

user:
  name: Ada
  profile:
    bio: Programmer
    location: London

§Arrays

Inline with commas:

tags[3]: rust,serde,parser

Vertical:

tags[3]:
  rust
  serde
  parser

§Arrays with Field Lists

users[2 name,age]:
  Ada,42
  Bob,35

§Configuration Options

Both serialization and deserialization can be customized using options:

use serde_toon2::{to_string_with_options, EncoderOptions, Delimiter};

let opts = EncoderOptions {
    indent: 4,
    delimiter: Delimiter::Pipe,
    ..Default::default()
};

let data = vec!["a", "b", "c"];
let toon = to_string_with_options(&data, opts).unwrap();

§Error Handling

Errors include location information when available:

use serde_toon2::from_str;
use serde_json::Value;

let invalid = "key: unclosed quote\"";
let result: Result<Value, _> = from_str(invalid);

// Error includes line and column information
assert!(result.is_err());

Re-exports§

pub use de::from_reader;
pub use de::from_reader_with_options;
pub use de::from_slice;
pub use de::from_slice_with_options;
pub use de::from_str;
pub use de::from_str_with_options;
pub use error::Error;
pub use error::Result;
pub use options::DecoderOptions;
pub use options::Delimiter;
pub use options::EncoderOptions;
pub use options::KeyFolding;
pub use options::PathExpansion;
pub use ser::to_string;
pub use ser::to_string_with_options;
pub use ser::to_vec;
pub use ser::to_vec_with_options;
pub use ser::to_writer;
pub use ser::to_writer_with_options;
pub use value::Map;
pub use value::Number;
pub use value::Value;

Modules§

de
error
Error types for TOON serialization and deserialization.
options
Configuration options for TOON serialization and deserialization.
ser
value
Generic TOON value type for dynamic content.