Skip to main content

Crate serde_toon

Crate serde_toon 

Source
Expand description

Serde-compatible TOON v3.0 encoder/decoder with optional v1.5 features.

§Examples

Quick encode/decode:

use serde::Serialize;
use serde_toon::toon;

#[derive(Serialize)]
struct User {
    name: String,
    age: u32,
}

let user = User {
    name: "Ada Lovelace".to_string(),
    age: 37,
};
let toon = toon!(encode: user)?;
let toon_from_json = toon!(encode_json: r#"{"name":"Grace Hopper"}"#)?;
let value = toon!("name: Ada Lovelace")?;
assert_eq!(toon, "name: Ada Lovelace\nage: 37");
assert_eq!(toon_from_json, "name: Grace Hopper");
assert_eq!(value, serde_json::json!({"name": "Ada Lovelace"}));

Encode to TOON:

use serde::{Deserialize, Serialize};
use serde_toon::to_string;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct User {
    name: String,
    age: u32,
}

let user = User {
    name: "Ada Lovelace".to_string(),
    age: 37,
};

let toon = to_string(&user)?;
assert_eq!(toon, "name: Ada Lovelace\nage: 37");

Decode back:

use serde::Deserialize;
use serde_toon::from_str;

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

let toon = "name: Ada Lovelace\nage: 37";
let round_trip: User = from_str(toon)?;
assert_eq!(
    round_trip,
    User {
        name: "Ada Lovelace".to_string(),
        age: 37
    }
);

JSON string round-trip:

use serde_toon::{from_str, to_string_from_json_str};

let json = r#"{"name":"Grace Hopper","field":"computer science","year":1952}"#;
let toon = to_string_from_json_str(json)?;
assert_eq!(
    toon,
    "name: Grace Hopper\nfield: computer science\nyear: 1952"
);

let back_to_json = serde_json::to_string(&from_str::<serde_json::Value>(&toon)?)?;
assert_eq!(back_to_json, json);

Untyped values:

use serde_toon::Value;

let value: Value = serde_toon::from_str("name: Margaret Hamilton\nage: 32")?;
assert_eq!(value, serde_json::json!({"name": "Margaret Hamilton", "age": 32}));

Custom options:

use serde_toon::{Delimiter, EncodeOptions, Indent, KeyFolding};

let opts = EncodeOptions::new()
    .with_indent(Indent::spaces(4))
    .with_delimiter(Delimiter::Pipe)
    .with_key_folding(KeyFolding::Safe)
    .with_flatten_depth(Some(2));
let toon = serde_toon::to_string_with_options(&serde_json::json!({"items": ["a", "b"]}), &opts)?;
assert_eq!(toon, "items[2|]: a|b");
use serde_toon::{DecodeOptions, ExpandPaths, Indent};

let opts = DecodeOptions::new()
    .with_indent(Indent::spaces(4))
    .with_strict(false)
    .with_expand_paths(ExpandPaths::Safe);
let value: serde_json::Value = serde_toon::from_str_with_options("a.b: 1", &opts)?;
assert_eq!(value, serde_json::json!({"a": {"b": 1}}));

§Removed APIs

use serde_toon::tabular;

Re-exports§

pub use crate::error::Error;
pub use crate::error::ErrorKind;
pub use crate::error::ErrorStage;
pub use crate::error::Location;
pub use crate::options::DecodeOptions;
pub use crate::options::Delimiter;
pub use crate::options::EncodeOptions;
pub use crate::options::ExpandPaths;
pub use crate::options::Indent;
pub use crate::options::KeyFolding;
pub use canonical::encode_canonical;
pub use canonical::CanonicalProfile;

Modules§

arena
canonical
decode
encode
error
num
options
text

Macros§

toon
Parse a JSON or TOON string into a serde_json::Value, or encode values into TOON.

Enums§

Value
Represents any valid JSON value.

Functions§

decode_to_value
decode_to_value_auto
decode_to_value_auto_with_options
decode_to_value_with_options
from_reader
Decode a value from a reader by buffering the entire input into memory.
from_reader_streaming
from_reader_streaming_with_options
from_reader_with_options
Decode a value from a reader with explicit DecodeOptions.
from_slice
from_slice_with_options
from_str
from_str_with_options
to_string
to_string_from_json_str
to_string_from_json_str_with_options
to_string_into
to_string_into_with_options
to_string_with_options
to_vec
to_vec_with_options
to_writer
to_writer_with_options
validate_str
validate_str_with_options

Type Aliases§

Result