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>
impl<'w> ValueWriter<'w>
Sourcepub fn null(self) -> Written
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");Sourcepub fn u64(self, value: u64) -> Written
pub fn u64(self, value: u64) -> Written
Writes an integer in decimal.
§Examples
assert_eq!(jsonx_lib::to_string(&u64::MAX), "18446744073709551615");Sourcepub fn i128(self, value: i128) -> Written
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");Sourcepub fn u128(self, value: u128) -> Written
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");Sourcepub fn f64(self, value: f64) -> Written
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");Sourcepub fn f32(self, value: f32) -> Written
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");Sourcepub fn str(self, value: &str) -> Written
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\"""#);Sourcepub fn raw(self, value: &RawValue<'_>) -> Written
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]}}"#);Sourcepub fn value(self, value: &Value<'_>) -> Written
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,{}]");Sourcepub fn object(self, members: impl FnOnce(&mut ObjectWriter<'_>)) -> Written
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}");Sourcepub fn array(self, items: impl FnOnce(&mut ArrayWriter<'_>)) -> Written
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]");