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§
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