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
impl JsonWriter
Sourcepub fn nested(&self, path: Path) -> Self
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.
Sourcepub fn set(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
pub fn set(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
Set a value at the specified path.
Sourcepub fn append(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
pub fn append(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
Append a value to an array at the specified path.
Sourcepub fn merge_object(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
pub fn merge_object(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
Merge an object into the object at the specified path.
Sourcepub fn increment(&mut self, path: Path, amount: impl Into<Number>) -> &mut Self
pub fn increment(&mut self, path: Path, amount: impl Into<Number>) -> &mut Self
Increment a numeric value at the specified path.
Sourcepub fn decrement(&mut self, path: Path, amount: impl Into<Number>) -> &mut Self
pub fn decrement(&mut self, path: Path, amount: impl Into<Number>) -> &mut Self
Decrement a numeric value at the specified path.
Sourcepub fn insert(
&mut self,
path: Path,
index: usize,
value: impl Into<Value>,
) -> &mut Self
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.
Sourcepub fn remove(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
pub fn remove(&mut self, path: Path, value: impl Into<Value>) -> &mut Self
Remove the first occurrence of a value from an array.
Trait Implementations§
Source§impl Clone for JsonWriter
impl Clone for JsonWriter
Source§fn clone(&self) -> JsonWriter
fn clone(&self) -> JsonWriter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for JsonWriter
impl Debug for JsonWriter
Auto Trait Implementations§
impl Freeze for JsonWriter
impl RefUnwindSafe for JsonWriter
impl Send for JsonWriter
impl Sync for JsonWriter
impl Unpin for JsonWriter
impl UnwindSafe for JsonWriter
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