encode

Function encode 

Source
pub fn encode<V: IntoJsonValue>(
    value: V,
    options: &EncodeOptions,
) -> ToonResult<String>
Expand description

Encode a JSON value to TOON format with custom options.

This function accepts either JsonValue or serde_json::Value and converts automatically.

ยงExamples

use toon_format::{encode, EncodeOptions, Delimiter};
use serde_json::json;

let data = json!({"tags": ["a", "b", "c"]});
let options = EncodeOptions::new().with_delimiter(Delimiter::Pipe);
let toon = encode(&data, &options)?;
assert!(toon.contains("|"));
Examples found in repository?
examples/parts/length_marker.rs (line 16)
7pub fn length_marker() {
8    let data = json!({
9        "tags": ["reading", "gaming", "coding"],
10        "items": [
11            {"sku": "A1", "qty": 2, "price": 9.99},
12            {"sku": "B2", "qty": 1, "price": 14.5}
13        ]
14    });
15
16    let out = encode(&data, &EncodeOptions::new().with_length_marker('#')).unwrap();
17    println!("{out}");
18}
More examples
Hide additional examples
examples/parts/delimiters.rs (line 17)
8pub fn delimiters() {
9    let data = json!({
10        "items": [
11            {"sku": "A1", "name": "Widget", "qty": 2, "price": 9.99},
12            {"sku": "B2", "name": "Gadget", "qty": 1, "price": 14.5}
13        ]
14    });
15
16    // Tab delimiter (\t)
17    let tab = encode(&data, &EncodeOptions::new().with_delimiter(Delimiter::Tab)).unwrap();
18    println!("{tab}");
19
20    // Pipe delimiter (|)
21    let pipe = encode(&data, &EncodeOptions::new().with_delimiter(Delimiter::Pipe)).unwrap();
22    println!("\n{pipe}");
23}