Struct serde_json_fmt::JsonFormat
source · pub struct JsonFormat { /* private fields */ }
Expand description
A Formatter
builder for configuring JSON
serialization options.
This type is the “entry point” to serde-json-fmt
’s functionality. To
perform custom-formatted JSON serialization, start by creating a
JsonFormat
instance by calling either JsonFormat::new()
or
JsonFormat::pretty()
, then call the various configuration methods as
desired, then either pass your serde::Serialize
value to one of the
format_to_string()
,
format_to_vec()
, and
format_to_writer()
convenience methods or
else (for lower-level usage) call build()
or
as_formatter()
to acquire a
serde_json::ser::Formatter
instance.
Implementations§
source§impl JsonFormat
impl JsonFormat
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new JsonFormat
instance that starts out configured to use
serde_json
’s “compact” format. Specifically, the instance is
configured as follows:
indent(None)
comma(",")
colon(":")
ascii(false)
sourcepub fn pretty() -> Self
pub fn pretty() -> Self
Create a new JsonFormat
instance that starts out configured to use
serde_json
’s “pretty” format. Specifically, the instance is
configured as follows:
indent(Some(" "))
(two spaces)comma(",")
colon(": ")
ascii(false)
sourcepub fn ascii(self, flag: bool) -> Self
pub fn ascii(self, flag: bool) -> Self
Set whether non-ASCII characters in strings should be serialized as
ASCII using \uXXXX
escape sequences. If flag
is true
, then all
non-ASCII characters will be escaped; if flag
is false
, then
non-ASCII characters will be serialized as themselves.
sourcepub fn comma<S: AsRef<str>>(self, s: S) -> Result<Self, JsonSyntaxError>
pub fn comma<S: AsRef<str>>(self, s: S) -> Result<Self, JsonSyntaxError>
Set the string to use as the item separator in lists & objects.
s
must contain exactly one comma (,
) character; all other
characters must be space characters, tabs, line feeds, and/or carriage
returns.
Errors
Returns Err
if s
does not meet the above requirements.
sourcepub fn colon<S: AsRef<str>>(self, s: S) -> Result<Self, JsonSyntaxError>
pub fn colon<S: AsRef<str>>(self, s: S) -> Result<Self, JsonSyntaxError>
Set the string to use as the key-value separator in objects.
s
must contain exactly one colon (:
) character; all other
characters must be space characters, tabs, line feeds, and/or carriage
returns.
Errors
Returns Err
if s
does not meet the above requirements.
sourcepub fn indent<S: AsRef<str>>(
self,
s: Option<S>
) -> Result<Self, JsonSyntaxError>
pub fn indent<S: AsRef<str>>( self, s: Option<S> ) -> Result<Self, JsonSyntaxError>
Set the string used for indentation.
If s
is None
, then no indentation or newlines will be inserted when
serializing. If s
is Some("")
(an empty string), then newlines
will be inserted, but nothing will be indented. If s
contains any
other string, the string must consist entirely of space characters,
tabs, line feeds, and/or carriage returns.
Errors
Returns Err
if s
contains a string that contains any character
other than those listed above.
sourcepub fn indent_width(self, n: Option<usize>) -> Self
pub fn indent_width(self, n: Option<usize>) -> Self
Set the string used for indentation to the given number of spaces.
This method is a convenience wrapper around
indent()
that calls it with a string consisting
of the given number of space characters, or with None
if n
is
None
.
sourcepub fn format_to_string<T: ?Sized + Serialize>(
&self,
value: &T
) -> Result<String, Error>
pub fn format_to_string<T: ?Sized + Serialize>( &self, value: &T ) -> Result<String, Error>
Format a serde::Serialize
value to a String
as JSON using the
configured formatting options.
Errors
Has the same error conditions as serde_json::to_string()
.
sourcepub fn format_to_vec<T: ?Sized + Serialize>(
&self,
value: &T
) -> Result<Vec<u8>, Error>
pub fn format_to_vec<T: ?Sized + Serialize>( &self, value: &T ) -> Result<Vec<u8>, Error>
Format a serde::Serialize
value to a Vec<u8>
as JSON using the
configured formatting options.
Errors
Has the same error conditions as serde_json::to_vec()
.
sourcepub fn format_to_writer<T: ?Sized + Serialize, W: Write>(
&self,
writer: W,
value: &T
) -> Result<(), Error>
pub fn format_to_writer<T: ?Sized + Serialize, W: Write>( &self, writer: W, value: &T ) -> Result<(), Error>
Write a serde::Serialize
value to a std::io::Write
instance as
JSON using the configured formatting options.
Errors
Has the same error conditions as serde_json::to_writer()
.
sourcepub fn build(self) -> JsonFormatter
pub fn build(self) -> JsonFormatter
Consume the JsonFormat
instance and return a
serde_json::ser::Formatter
instance.
This is a low-level operation. For most use cases, using one of the
format_to_string()
,
format_to_vec()
, and
format_to_writer()
convenience
methods is recommended.
sourcepub fn as_formatter(&self) -> JsonFrmtr<'_>
pub fn as_formatter(&self) -> JsonFrmtr<'_>
Return a serde_json::ser::Formatter
instance that borrows data from
the JsonFormat
instance.
This is a low-level operation. For most use cases, using one of the
format_to_string()
,
format_to_vec()
, and
format_to_writer()
convenience
methods is recommended.
Unlike build()
, this method makes it possible to
create multiple Formatter
s from a single JsonFormat
instance.
Trait Implementations§
source§impl Clone for JsonFormat
impl Clone for JsonFormat
source§fn clone(&self) -> JsonFormat
fn clone(&self) -> JsonFormat
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for JsonFormat
impl Debug for JsonFormat
source§impl Default for JsonFormat
impl Default for JsonFormat
source§fn default() -> Self
fn default() -> Self
Equivalent to JsonFormat::new()
source§impl Hash for JsonFormat
impl Hash for JsonFormat
source§impl PartialEq<JsonFormat> for JsonFormat
impl PartialEq<JsonFormat> for JsonFormat
source§fn eq(&self, other: &JsonFormat) -> bool
fn eq(&self, other: &JsonFormat) -> bool
self
and other
values to be equal, and is used
by ==
.