pub struct ObjectWriter<'w> { /* private fields */ }Expand description
Adds the members of an object being written; see
ValueWriter::object.
Members are written in the order they are added. Nothing stops a key from being added twice; the reader of the output decides what that means (this crate’s parser refuses it by default), so add each key once.
Implementations§
Source§impl ObjectWriter<'_>
impl ObjectWriter<'_>
Sourcepub fn member<T: ToJson + ?Sized>(&mut self, key: &str, value: &T)
pub fn member<T: ToJson + ?Sized>(&mut self, key: &str, value: &T)
Adds the member key: value. An Option that is None is written as
null; to leave the member out instead, use
optional.
§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};
struct Reply<'a> { id: u64, result: Option<&'a str> }
impl ToJson for Reply<'_> {
fn to_json(&self, out: ValueWriter<'_>) -> Written {
out.object(|o| {
o.member("jsonrpc", "2.0");
o.member("id", &self.id);
o.member("result", &self.result); // `null` when None
})
}
}
let reply = Reply { id: 1, result: None };
assert_eq!(jsonx_lib::to_string(&reply), r#"{"jsonrpc":"2.0","id":1,"result":null}"#);Sourcepub fn optional<T: ToJson + ?Sized>(&mut self, key: &str, value: &T)
pub fn optional<T: ToJson + ?Sized>(&mut self, key: &str, value: &T)
Adds the member key: value, unless value is absent
(ToJson::is_absent: an Option that is None). This is
TypeScript’s optional key?: T, as LSP and MCP use it.
§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};
struct Item { label: String, detail: Option<String> }
impl ToJson for Item {
fn to_json(&self, out: ValueWriter<'_>) -> Written {
out.object(|o| {
o.member("label", &self.label);
o.optional("detail", &self.detail);
})
}
}
let bare = Item { label: "x".into(), detail: None };
assert_eq!(jsonx_lib::to_string(&bare), r#"{"label":"x"}"#);
let full = Item { label: "x".into(), detail: Some("fn x()".into()) };
assert_eq!(jsonx_lib::to_string(&full), r#"{"label":"x","detail":"fn x()"}"#);Sourcepub fn member_with(
&mut self,
key: &str,
value: impl FnOnce(ValueWriter<'_>) -> Written,
)
pub fn member_with( &mut self, key: &str, value: impl FnOnce(ValueWriter<'_>) -> Written, )
Adds the member key, its value written by value.
§Examples
use jsonx_lib::{ToJson, ValueWriter, Written};
struct Hover { markdown: String }
impl ToJson for Hover {
fn to_json(&self, out: ValueWriter<'_>) -> Written {
out.object(|o| {
o.member_with("contents", |w| {
w.object(|c| {
c.member("kind", "markdown");
c.member("value", &self.markdown);
})
});
})
}
}
let h = Hover { markdown: "**x**".into() };
assert_eq!(jsonx_lib::to_string(&h), r#"{"contents":{"kind":"markdown","value":"**x**"}}"#);Auto Trait Implementations§
impl<'w> !RefUnwindSafe for ObjectWriter<'w>
impl<'w> !Send for ObjectWriter<'w>
impl<'w> !Sync for ObjectWriter<'w>
impl<'w> !UnwindSafe for ObjectWriter<'w>
impl<'w> Forget for ObjectWriter<'w>
impl<'w> Freeze for ObjectWriter<'w>
impl<'w> Move for ObjectWriter<'w>
impl<'w> Unpin for ObjectWriter<'w>
impl<'w> UnsafeUnpin for ObjectWriter<'w>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more