Skip to main content

ValueWriter

Struct ValueWriter 

Source
pub struct ValueWriter<'w> { /* private fields */ }
Expand description

Writes one JSON value; see ToJson.

Each method writes a value and consumes the writer. Strings are escaped as write_string describes; numbers are written in their shortest exact form; containers are built in a closure that gets an ObjectWriter or ArrayWriter. The layout (compact or pretty) is the caller’s choice and is applied for you.

§Examples

use jsonx_lib::{ToJson, ValueWriter, Written};

struct Celsius(f64);

impl ToJson for Celsius {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.f64(self.0)
    }
}

assert_eq!(jsonx_lib::to_string(&Celsius(21.5)), "21.5");
assert_eq!(jsonx_lib::to_string(&Celsius(f64::NAN)), "null");

Implementations§

Source§

impl<'w> ValueWriter<'w>

Source

pub fn null(self) -> Written

Writes null.

§Examples
struct Nothing;
impl ToJson for Nothing {
    fn to_json(&self, out: ValueWriter<'_>) -> Written { out.null() }
}
assert_eq!(jsonx_lib::to_string(&Nothing), "null");
Source

pub fn bool(self, value: bool) -> Written

Writes true or false.

§Examples
assert_eq!(jsonx_lib::to_string(&true), "true");
Source

pub fn i64(self, value: i64) -> Written

Writes an integer in decimal.

§Examples
assert_eq!(jsonx_lib::to_string(&-42i64), "-42");
Source

pub fn u64(self, value: u64) -> Written

Writes an integer in decimal.

§Examples
assert_eq!(jsonx_lib::to_string(&u64::MAX), "18446744073709551615");
Source

pub fn i128(self, value: i128) -> Written

Writes a 128-bit integer in decimal, exactly (JSON numbers have no size limit; a reader that stores them as f64 will round it).

§Examples
assert_eq!(jsonx_lib::to_string(&i128::MIN), "-170141183460469231731687303715884105728");
Source

pub fn u128(self, value: u128) -> Written

Writes a 128-bit unsigned integer in decimal, exactly.

§Examples
assert_eq!(jsonx_lib::to_string(&u128::MAX), "340282366920938463463374607431768211455");
Source

pub fn f64(self, value: f64) -> Written

Writes the shortest text that reads back as the same f64, laid out as Number::from_f64 describes (1.0, 0.1, 1e100). NaN and the infinities, which JSON cannot represent, are written as null, as Value::from makes them.

§Examples
assert_eq!(jsonx_lib::to_string(&1.0f64), "1.0");
assert_eq!(jsonx_lib::to_string(&f64::INFINITY), "null");
Source

pub fn f32(self, value: f32) -> Written

Writes the shortest text that reads back as the same f32 (so 0.1f32 is 0.1); NaN and the infinities as null.

§Examples
assert_eq!(jsonx_lib::to_string(&0.1f32), "0.1");
Source

pub fn number(self, value: &Number<'_>) -> Written

Writes a Number’s exact text.

§Examples
use jsonx_lib::Number;

let price = Number::from_text("19.990").unwrap();
assert_eq!(jsonx_lib::to_string(&price), "19.990");
Source

pub fn str(self, value: &str) -> Written

Writes a string, escaped as write_string describes.

§Examples
assert_eq!(jsonx_lib::to_string("say \"hi\""), r#""say \"hi\"""#);
Source

pub fn raw(self, value: &RawValue<'_>) -> Written

Writes a RawValue’s text: already valid JSON, so it is not checked again. In the plain formats it is copied unchanged (in the pretty layout it keeps its own spacing), U+2028 and U+2029 inside its strings included: valid JSON, though write_string escapes them in the strings it writes, for JavaScript before ES2019. For text that will be pasted into JavaScript source, use Format::html_safe, which escapes them in raw text too. Under Format::html_safe or Format::ascii, the characters that format escapes are escaped inside the raw text’s strings too (the only place valid JSON can hold them), so </script> in a forwarded value is written \u003c/script\u003e and the format’s promise holds; the value it stands for is unchanged.

§Examples
use jsonx_lib::{RawValue, ToJson, ValueWriter, Written};

struct Forward<'a> { id: u64, result: RawValue<'a> }

impl ToJson for Forward<'_> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.object(|o| {
            o.member("id", &self.id);
            o.member_with("result", |w| w.raw(&self.result));
        })
    }
}

let result = RawValue::new(r#"{"items": [1, 2]}"#)?;
let reply = Forward { id: 3, result };
assert_eq!(jsonx_lib::to_string(&reply), r#"{"id":3,"result":{"items": [1, 2]}}"#);
Source

pub fn value(self, value: &Value<'_>) -> Written

Writes a Value (iterative at any depth), in the writer’s layout.

§Examples
use jsonx_lib::{json, ToJson, Value, ValueWriter, Written};

struct Extra<'a>(Value<'a>);
impl ToJson for Extra<'_> {
    fn to_json(&self, out: ValueWriter<'_>) -> Written { out.value(&self.0) }
}
assert_eq!(jsonx_lib::to_string(&Extra(json!([1, {}]))), "[1,{}]");
Source

pub fn json<T: ToJson + ?Sized>(self, value: &T) -> Written

Writes value through its own ToJson: out.json(&x) is x.to_json(out).

§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};

struct Id(u32);
impl ToJson for Id {
    fn to_json(&self, out: ValueWriter<'_>) -> Written { out.json(&self.0) }
}
assert_eq!(jsonx_lib::to_string(&Id(7)), "7");
Source

pub fn object(self, members: impl FnOnce(&mut ObjectWriter<'_>)) -> Written

Writes an object whose members members adds, in the order it adds them.

§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};

struct Position { line: u32, character: u32 }

impl ToJson for Position {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.object(|o| {
            o.member("line", &self.line);
            o.member("character", &self.character);
        })
    }
}

let p = Position { line: 3, character: 14 };
assert_eq!(jsonx_lib::to_string(&p), r#"{"line":3,"character":14}"#);
assert_eq!(jsonx_lib::to_string_pretty(&p), "{\n  \"line\": 3,\n  \"character\": 14\n}");
Source

pub fn array(self, items: impl FnOnce(&mut ArrayWriter<'_>)) -> Written

Writes an array whose items items adds.

§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};

struct Range { start: u32, end: u32 }

impl ToJson for Range {
    fn to_json(&self, out: ValueWriter<'_>) -> Written {
        out.array(|a| {
            a.item(&self.start);
            a.item(&self.end);
        })
    }
}

assert_eq!(jsonx_lib::to_string(&Range { start: 1, end: 4 }), "[1,4]");

Auto Trait Implementations§

§

impl<'w> !RefUnwindSafe for ValueWriter<'w>

§

impl<'w> !Send for ValueWriter<'w>

§

impl<'w> !Sync for ValueWriter<'w>

§

impl<'w> !UnwindSafe for ValueWriter<'w>

§

impl<'w> Forget for ValueWriter<'w>

§

impl<'w> Freeze for ValueWriter<'w>

§

impl<'w> Move for ValueWriter<'w>

§

impl<'w> Unpin for ValueWriter<'w>

§

impl<'w> UnsafeUnpin for ValueWriter<'w>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.