Struct Formatter

Source
pub struct Formatter {
    pub indent: String,
    pub line_separator: String,
    pub record_separator: String,
    pub after_colon: String,
    pub trailing_output: String,
    pub eager_record_separators: bool,
    /* private fields */
}
Expand description

Formatter allows customizable pretty-printing, minimizing, and other formatting tasks on JSON-encoded UTF-8 data in string or stream format.

Example:

let mut fmt = jsonxf::Formatter::pretty_printer();
fmt.line_separator = String::from("\r\n");
assert_eq!(
    fmt.format("{\"a\":1}").unwrap(),
    "{\r\n  \"a\": 1\r\n}"
);

Fields§

§indent: String

Used for beginning-of-line indentation in arrays and objects.

§line_separator: String

Used inside arrays and objects.

§record_separator: String

Used between root-level arrays and objects.

§after_colon: String

Used after a colon inside objects.

§trailing_output: String

Used at very end of output.

§eager_record_separators: bool

Add a record_separator as soon as a record ends, before seeing a subsequent record. Useful when there’s a long time between records.

Implementations§

Source§

impl Formatter

Source

pub fn pretty_printer() -> Formatter

Returns a Formatter set up for pretty-printing. Defaults to using two spaces of indentation, Unix newlines, and no whitespace at EOF.

§Example:
assert_eq!(
    jsonxf::Formatter::pretty_printer().format("{\"a\":1}").unwrap(),
    "{\n  \"a\": 1\n}"
);
Source

pub fn minimizer() -> Formatter

Returns a Formatter set up for minimizing. Defaults to using Unix newlines between records, and no whitespace at EOF.

§Example:
assert_eq!(
    jsonxf::Formatter::minimizer().format("{  \"a\" : 1  }\n").unwrap(),
    "{\"a\":1}"
);
Source

pub fn format(&mut self, json_string: &str) -> Result<String, String>

Formats a string of JSON-encoded data.

Input must be valid JSON data in UTF-8 encoding.

§Example:
let mut fmt = jsonxf::Formatter::pretty_printer();
fmt.indent = String::from("\t");
fmt.trailing_output = String::from("\n");
assert_eq!(
    fmt.format("{\"a\":1}").unwrap(),
    "{\n\t\"a\": 1\n}\n"
);
Source

pub fn format_stream( &mut self, input: &mut dyn Read, output: &mut dyn Write, ) -> Result<(), Error>

Formats a stream of JSON-encoded data.

Input must be valid JSON data in UTF-8 encoding.

§Example:
let mut fmt = jsonxf::Formatter::pretty_printer();
fmt.indent = String::from("\t");
fmt.trailing_output = String::from("\n");
match fmt.format_stream(&mut std::io::stdin(), &mut std::io::stdout()) {
    Ok(_) => { /* YAY */ },
    Err(e) => { panic!(e.to_string()); }
}
Source

pub fn format_stream_unbuffered( &mut self, input: &mut impl Read, output: &mut impl Write, ) -> Result<(), Error>

Formats a stream of JSON-encoded data without buffering.

This will perform many small writes, so it’s advisable to use an output that does its own buffering. In simple cases, use Formatter::format_stream instead.

§Example:
let mut fmt = jsonxf::Formatter::pretty_printer();
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
fmt.format_stream_unbuffered(&mut stdin, &mut std::io::LineWriter::new(stdout))
    .unwrap();
Source

pub fn format_buf( &mut self, buf: &[u8], writer: &mut impl Write, ) -> Result<(), Error>

Format directly from a buffer into a writer.

This may be called on chunks of a JSON document to format it bit by bit.

As such, it does not add the trailing_output at the end.

§Example:
let text = "[1, 2, 3]";
let mut fmt = jsonxf::Formatter::pretty_printer();
let mut stdout = std::io::stdout();
fmt.format_buf(text.as_bytes(), &mut stdout).unwrap();

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> 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, 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.