ToonSerializer

Struct ToonSerializer 

Source
pub struct ToonSerializer { /* private fields */ }
Expand description

TOON-LD Serializer

Converts JSON/JSON-LD values to TOON-LD format. The serializer handles:

  • Tabular arrays (arrays of objects with union-of-keys)
  • Primitive arrays (inline or multi-line)
  • JSON-LD keywords and context-based URI compaction
  • Value nodes with language tags and datatypes (using standard TOON object syntax)

§Example

use toon_core::{ToonSerializer, JsonLdContext};
use serde_json::json;

let serializer = ToonSerializer::new();
let value = json!({
    "name": "Alice",
    "age": 30
});

let toon = serializer.serialize(&value).unwrap();
assert!(toon.contains("name: Alice"));
assert!(toon.contains("age: 30"));

Implementations§

Source§

impl ToonSerializer

Source

pub fn new() -> ToonSerializer

Create a new serializer with default settings.

§Example
use toon_core::ToonSerializer;

let serializer = ToonSerializer::new();
Source

pub fn with_context(self, context: JsonLdContext) -> ToonSerializer

Set the JSON-LD context for URI compaction.

§Arguments
  • context - The JSON-LD context to use
§Example
use toon_core::{ToonSerializer, JsonLdContext};

let mut ctx = JsonLdContext::new();
ctx.add_prefix("foaf", "http://xmlns.com/foaf/0.1/");

let serializer = ToonSerializer::new().with_context(ctx);
Source

pub fn with_indent_size(self, size: usize) -> ToonSerializer

Set the indentation size.

§Arguments
  • size - Number of spaces per indentation level
§Example
use toon_core::ToonSerializer;

let serializer = ToonSerializer::new().with_indent_size(4);
Source

pub fn context(&self) -> &JsonLdContext

Get a reference to the current context.

Source

pub fn indent_size(&self) -> usize

Get the current indentation size.

Source

pub fn with_shape_partitioning(self, enable: bool) -> ToonSerializer

Enable or disable shape-based partitioning for sparse arrays.

When enabled, arrays with high sparsity (> 30% null values) will be automatically partitioned by entity shape to reduce null delimiter overhead.

§Arguments
  • enable - Whether to enable shape-based partitioning
§Example
use toon_core::ToonSerializer;

let serializer = ToonSerializer::new().with_shape_partitioning(true);
Source

pub fn serialize(&self, value: &Value) -> Result<String, ToonError>

Serialize a JSON value to TOON-LD format.

§Arguments
  • value - The JSON value to serialize
§Returns

A Result containing the TOON-LD string or an error.

§Example
use toon_core::ToonSerializer;
use serde_json::json;

let serializer = ToonSerializer::new();
let value = json!({"name": "Alice", "age": 30});
let toon = serializer.serialize(&value).unwrap();
Source

pub fn serialize_json(&self, json: &str) -> Result<String, ToonError>

Serialize a JSON string to TOON-LD string.

§Arguments
  • json - A JSON string to parse and serialize
§Returns

A Result containing the TOON-LD string or an error.

§Example
use toon_core::ToonSerializer;

let serializer = ToonSerializer::new();
let toon = serializer.serialize_json(r#"{"name": "Alice"}"#).unwrap();
Source

pub fn serialize_keyed_array( &self, key: &str, arr: &[Value], depth: usize, output: &mut String, ) -> Result<(), ToonError>

Serialize a keyed array (array with a key prefix).

Trait Implementations§

Source§

impl Clone for ToonSerializer

Source§

fn clone(&self) -> ToonSerializer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ToonSerializer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for ToonSerializer

Source§

fn default() -> ToonSerializer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.