Serde TOON
Serde-compatible TOON Format Spec v3.0 encoder/decoder (275 tests).
[]
= "0.1"
Auto-detect struct, JSON or TOON
use Serialize;
use toon;
let user = User ;
let toon = toon!?;
let json_value = toon!?;
let toon_value = toon!?;
assert_eq!;
assert_eq!;
assert_eq!;
# Ok::
Example
Encode to TOON:
use ;
use to_string;
let user = User ;
let toon = to_string?;
assert_eq!;
# Ok::
TOON format string:
name: Ada Lovelace
age: 37
Decode back:
use from_str;
let toon = "name: Ada Lovelace\nage: 37";
let round_trip: User = from_str?;
assert_eq!;
# Ok::
JSON string round-trip
use ;
let json = r#"{"name":"Grace Hopper","field":"computer science","year":1952}"#;
let toon = to_string_from_json_str?;
assert_eq!;
let back_to_json =
to_string
.map_err?;
assert_eq!;
# Ok::
Untyped values
use Value;
let value: Value = from_str?;
assert_eq!;
# Ok::
Custom options
use ;
let opts = new
.with_indent
.with_delimiter
.with_key_folding
.with_flatten_depth;
let toon = to_string_with_options?;
assert_eq!;
# Ok::
use ;
let opts = new
.with_indent
.with_strict
.with_expand_paths;
let value: Value = from_str_with_options?;
assert_eq!;
# Ok::
Performance tips
- For large outputs, prefer
to_vecorto_writerto avoid extra UTF-8 checks.
Benchmarks
This TOON encoder/decoder is well optimized. You can compare it to other implementations with serde_json as a baseline.
GitHub_Repo example baseline (median):
- github_repos/encode_toon: 241.64 µs
- github_repos/decode_toon: 92.94 µs
- github_repos/encode_json: 28.824 µs
- github_repos/decode_json: 46.657 µs
Peanuts example baseline (median):
-
peanuts_characters/encode_toon: 21.057 µs
-
peanuts_characters/decode_toon: 10.432 µs
-
peanuts_characters/encode_json: 3.2412 µs
-
peanuts_characters/decode_json: 4.1360 µs
-
peanuts_specials/encode_toon: 14.334 µs
-
peanuts_specials/decode_toon: 24.949 µs
-
peanuts_specials/encode_json: 2.4854 µs
-
peanuts_specials/decode_json: 4.9380 µs
-
peanuts_universe/encode_toon: 29.048 µs
-
peanuts_universe/decode_toon: 18.683 µs
-
peanuts_universe/encode_json: 2.9901 µs
-
peanuts_universe/decode_json: 5.1913 µs
-
peanuts_jsonld/encode_toon: 33.826 µs
-
peanuts_jsonld/decode_toon: 64.089 µs
-
peanuts_jsonld/encode_json: 6.8633 µs
-
peanuts_jsonld/decode_json: 12.488 µs
License
MIT. See LICENSE.