Skip to main content

ObjectWriter

Struct ObjectWriter 

Source
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<'_>

Source

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}"#);
Source

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()"}"#);
Source

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