GitHub | crates.io | Documentation | Issues
The serde-json-fmt
crate lets you create custom
serde_json
formatters with the
indentation, separators, and ASCII requirements of your choice.
serde_json
itself only directly provides the ability to produce JSON in
either "compact" form or "pretty" form, with the only customizable aspect being
the string used for pretty indentation. serde-json-fmt
complements
serde_json
to let you also customize the whitespace around commas & colons
and whether to escape non-ASCII characters.
Installation
serde-json-fmt
requires version 1.60 of Rust or higher. To use the
serde-json-fmt
library in your Cargo project, add the following to your
Cargo.toml
:
[]
= "0.1.0"
Examples
Say you want to serialize a value in one-line "compact" form, but you want a
space after each colon & comma, something that serde_json
's compact form
doesn't do. serde-json-fmt
lets you do that:
use json;
use JsonFormat;
let value = json!;
let s = new
.comma
.unwrap
.colon
.unwrap
.format_to_string
.unwrap;
assert_eq!;
Say you want to format a value in multiline "pretty" form, but using four-space
indents and with all non-ASCII characters encoded as \uXXXX
escape sequences.
serde-json-fmt
lets you do that:
use json;
use JsonFormat;
let value = json!;
let s = pretty
.indent_width
.ascii
.format_to_string
.unwrap;
assert_eq!;