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

source

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)
source

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)
source

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.

source

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.

source

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.

source

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.

source

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.

source

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().

source

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().

source

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().

source

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.

source

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 Formatters from a single JsonFormat instance.

Trait Implementations§

source§

impl Clone for JsonFormat

source§

fn clone(&self) -> JsonFormat

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for JsonFormat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for JsonFormat

source§

fn default() -> Self

Equivalent to JsonFormat::new()

source§

impl Hash for JsonFormat

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<JsonFormat> for JsonFormat

source§

fn eq(&self, other: &JsonFormat) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for JsonFormat

source§

impl StructuralEq for JsonFormat

source§

impl StructuralPartialEq for JsonFormat

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.