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
impl ToonSerializer
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new serializer with default settings.
§Example
use toon_core::ToonSerializer;
let serializer = ToonSerializer::new();Sourcepub fn with_context(self, context: JsonLdContext) -> Self
pub fn with_context(self, context: JsonLdContext) -> Self
Sourcepub fn with_indent_size(self, size: usize) -> Self
pub fn with_indent_size(self, size: usize) -> Self
Sourcepub fn context(&self) -> &JsonLdContext
pub fn context(&self) -> &JsonLdContext
Get a reference to the current context.
Sourcepub fn indent_size(&self) -> usize
pub fn indent_size(&self) -> usize
Get the current indentation size.
Sourcepub fn with_shape_partitioning(self, enable: bool) -> Self
pub fn with_shape_partitioning(self, enable: bool) -> Self
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);Sourcepub fn serialize(&self, value: &Value) -> Result<String>
pub fn serialize(&self, value: &Value) -> Result<String>
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();Sourcepub fn serialize_json(&self, json: &str) -> Result<String>
pub fn serialize_json(&self, json: &str) -> Result<String>
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();Trait Implementations§
Source§impl Clone for ToonSerializer
impl Clone for ToonSerializer
Source§fn clone(&self) -> ToonSerializer
fn clone(&self) -> ToonSerializer
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ToonSerializer
impl Debug for ToonSerializer
Auto Trait Implementations§
impl Freeze for ToonSerializer
impl RefUnwindSafe for ToonSerializer
impl Send for ToonSerializer
impl Sync for ToonSerializer
impl Unpin for ToonSerializer
impl UnwindSafe for ToonSerializer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more