JsonGenerator

Struct JsonGenerator 

Source
pub struct JsonGenerator<'indent, W: Write> { /* private fields */ }
Expand description

JSON serializer for JsonValue.

Basically you don’t need to use this struct directly since JsonValue::stringify or JsonValue::format methods are using this internally.

use tinyjson::{JsonGenerator, JsonValue};

let v = JsonValue::from("hello, world".to_string());
let mut buf = vec![];
let mut gen = JsonGenerator::new(&mut buf);
gen.generate(&v).unwrap();

assert_eq!(String::from_utf8(buf).unwrap(), "\"hello, world\"");

Implementations§

Source§

impl<'indent, W: Write> JsonGenerator<'indent, W>

Source

pub fn new(out: W) -> Self

Create a new JsonGenerator object. The serialized byte sequence will be written to the given io::Write object.

Source

pub fn indent(self, indent: &'indent str) -> Self

Set indent string. This will be used by JsonGenerator::generate.

use tinyjson::{JsonGenerator, JsonValue};

let v = JsonValue::from(vec![1.0.into(), 2.0.into(), 3.0.into()]);
let mut buf = vec![];
let mut gen = JsonGenerator::new(&mut buf).indent("        ");
gen.generate(&v).unwrap();

assert_eq!(String::from_utf8(buf).unwrap(),
"[
        1,
        2,
        3
]");
Source

pub fn generate(&mut self, value: &JsonValue) -> Result<()>

Serialize the JsonValue into UTF-8 byte sequence. The result will be written to the io::Write object passed at JsonGenerator::new. This method serializes the value without indentation by default. But after setting an indent string via JsonGenerator::indent, this method will use the indent for elements of array and object.

use tinyjson::{JsonGenerator, JsonValue};

let v = JsonValue::from(vec![1.0.into(), 2.0.into(), 3.0.into()]);

let mut buf = vec![];
let mut gen = JsonGenerator::new(&mut buf);
gen.generate(&v).unwrap();
assert_eq!(String::from_utf8(buf).unwrap(), "[1,2,3]");

let mut buf = vec![];
let mut gen = JsonGenerator::new(&mut buf).indent("  "); // with 2-spaces indent
gen.generate(&v).unwrap();

assert_eq!(String::from_utf8(buf).unwrap(),
"[
  1,
  2,
  3
]");

Auto Trait Implementations§

§

impl<'indent, W> Freeze for JsonGenerator<'indent, W>
where W: Freeze,

§

impl<'indent, W> RefUnwindSafe for JsonGenerator<'indent, W>
where W: RefUnwindSafe,

§

impl<'indent, W> Send for JsonGenerator<'indent, W>
where W: Send,

§

impl<'indent, W> Sync for JsonGenerator<'indent, W>
where W: Sync,

§

impl<'indent, W> Unpin for JsonGenerator<'indent, W>
where W: Unpin,

§

impl<'indent, W> UnwindSafe for JsonGenerator<'indent, W>
where W: UnwindSafe,

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.