Skip to main content

JsonFormat

Struct 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

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 duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Eq for JsonFormat

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 for JsonFormat

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for JsonFormat

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.