JsonWriter

Struct JsonWriter 

Source
pub struct JsonWriter { /* private fields */ }
Expand description

A generic writer for building patches on arbitrary JSON structures.

JsonWriter is an escape hatch for cases where you need to work with dynamic or unknown JSON structures. For typed access, prefer using derived State types.

§Examples

use tirea_state::{JsonWriter, path};
use serde_json::json;

let mut w = JsonWriter::new();
w.set(path!("name"), json!("Alice"));
w.set(path!("age"), json!(30));
w.append(path!("tags"), json!("admin"));

let patch = w.build();
assert_eq!(patch.len(), 3);

Implementations§

Source§

impl JsonWriter

Source

pub fn new() -> Self

Create a new writer at the document root.

Source

pub fn at(base: Path) -> Self

Create a new writer at the specified base path.

Source

pub fn base(&self) -> &Path

Get the base path of this writer.

Source

pub fn nested(&self, path: Path) -> Self

Create a nested writer at a relative path.

The nested writer will have its own operations vector. Use merge to combine its operations back into the parent.

Source

pub fn set(&mut self, path: Path, value: impl Into<Value>) -> &mut Self

Set a value at the specified path.

Source

pub fn set_root(&mut self, value: impl Into<Value>) -> &mut Self

Set a value at the base path.

Source

pub fn delete(&mut self, path: Path) -> &mut Self

Delete the value at the specified path.

Source

pub fn append(&mut self, path: Path, value: impl Into<Value>) -> &mut Self

Append a value to an array at the specified path.

Source

pub fn merge_object(&mut self, path: Path, value: impl Into<Value>) -> &mut Self

Merge an object into the object at the specified path.

Source

pub fn increment(&mut self, path: Path, amount: impl Into<Number>) -> &mut Self

Increment a numeric value at the specified path.

Source

pub fn decrement(&mut self, path: Path, amount: impl Into<Number>) -> &mut Self

Decrement a numeric value at the specified path.

Source

pub fn insert( &mut self, path: Path, index: usize, value: impl Into<Value>, ) -> &mut Self

Insert a value at a specific index in an array.

Source

pub fn remove(&mut self, path: Path, value: impl Into<Value>) -> &mut Self

Remove the first occurrence of a value from an array.

Source

pub fn merge<W: WriterOps>(&mut self, other: W) -> &mut Self

Merge operations from another writer into this one.

Source

pub fn build(self) -> Patch

Consume this writer and build a patch.

Source

pub fn is_empty(&self) -> bool

Check if this writer has any operations.

Source

pub fn len(&self) -> usize

Get the number of operations.

Source

pub fn clear(&mut self)

Clear all operations.

Trait Implementations§

Source§

impl Clone for JsonWriter

Source§

fn clone(&self) -> JsonWriter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JsonWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for JsonWriter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.