pub struct TypedDoc { /* private fields */ }Expand description
Owned JSON buffer with typed path access and exclusive in-place mutation.
Prefer this as the single object you hold in pipelines instead of
serde_json::Value. Reads decode only the spans you ask for; writes splice
the same Vec<u8> and leave unmentioned keys intact (open document).
Read methods come from JsonDoc; mutation via TypedDoc::mutate.
Implementations§
Source§impl TypedDoc
impl TypedDoc
Sourcepub fn from_slice(slice: &[u8]) -> Self
pub fn from_slice(slice: &[u8]) -> Self
Copy slice into a new owned document.
pub fn is_empty(&self) -> bool
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consume into the underlying buffer.
Sourcepub fn as_mut_vec(&mut self) -> &mut Vec<u8> ⓘ
pub fn as_mut_vec(&mut self) -> &mut Vec<u8> ⓘ
Mutable access to the buffer (invalidates external indexes; prefer
mutate for path ops).
Sourcepub fn as_ref_doc(&self) -> TypedDocRef<'_>
pub fn as_ref_doc(&self) -> TypedDocRef<'_>
Borrowed view of the same bytes.
Sourcepub fn mutate(&mut self) -> TypedMutator<'_>
pub fn mutate(&mut self) -> TypedMutator<'_>
Begin exclusive mutation of this document.
Holds &mut self for the lifetime of the mutator, so any zero-copy
borrows from JsonDoc reads cannot coexist with active mutation.
Sourcepub fn write_view<V: JsonView>(&mut self, view: &V) -> Result<(), Error>
pub fn write_view<V: JsonView>(&mut self, view: &V) -> Result<(), Error>
Open-document write of a JsonView (only named paths are upserted).
Sourcepub fn apply_ops(&mut self, ops: &[MutateOp]) -> Result<(), Error>
pub fn apply_ops(&mut self, ops: &[MutateOp]) -> Result<(), Error>
Apply a batch of crate::MutateOps (see crate::apply_ops).
Sourcepub fn merge_shallow(&mut self, path: &str, patch: &[u8]) -> Result<(), Error>
pub fn merge_shallow(&mut self, path: &str, patch: &[u8]) -> Result<(), Error>
Shallow-merge a patch object into the object at path ("" = root).
Sourcepub fn from_view<V: JsonView>(view: &V) -> Result<Self, Error>
pub fn from_view<V: JsonView>(view: &V) -> Result<Self, Error>
Build a new document by open-upserting view onto {}.
Result contains only the paths the view writes (plus any parents created
by upsert). Prefer crate::ObjectBuilder when you need full control of
key order without a view type.
use jshift::{JsonView, TypedDoc, JsonDoc};
#[derive(JsonView)]
struct Card {
#[json(path = "id")]
id: u64,
#[json(path = "title")]
title: String,
}
let doc = TypedDoc::from_view(&Card { id: 1, title: "Hat".into() }).unwrap();
assert_eq!(doc.get::<u64>("id").unwrap(), 1);Source§impl TypedDoc
impl TypedDoc
Sourcepub fn view_list<T: JsonView>(
&self,
path: &str,
) -> Result<ViewList<'_, T>, Error>
pub fn view_list<T: JsonView>( &self, path: &str, ) -> Result<ViewList<'_, T>, Error>
Open a ViewList of T: JsonView at path.
Sourcepub fn value_list<T: FromJsonSlice>(
&self,
path: &str,
) -> Result<ValueList<'_, T>, Error>
pub fn value_list<T: FromJsonSlice>( &self, path: &str, ) -> Result<ValueList<'_, T>, Error>
Open a ValueList of T: FromJsonSlice at path.
Sourcepub fn root_view_list<T: JsonView>(&self) -> Result<ViewList<'_, T>, Error>
pub fn root_view_list<T: JsonView>(&self) -> Result<ViewList<'_, T>, Error>
Root document is a JSON array of views ([{...}, ...]).
Sourcepub fn root_value_list<T: FromJsonSlice>(
&self,
) -> Result<ValueList<'_, T>, Error>
pub fn root_value_list<T: FromJsonSlice>( &self, ) -> Result<ValueList<'_, T>, Error>
Root document is a JSON array of values ([1,2,3]).
Trait Implementations§
impl Eq for TypedDoc
Source§impl<'a> From<&'a TypedDoc> for TypedDocRef<'a>
impl<'a> From<&'a TypedDoc> for TypedDocRef<'a>
Source§impl JsonDoc for TypedDoc
impl JsonDoc for TypedDoc
Source§fn as_json_bytes(&self) -> &[u8] ⓘ
fn as_json_bytes(&self) -> &[u8] ⓘ
Source§fn get_raw(&self, path: &str) -> Result<&[u8], Error>
fn get_raw(&self, path: &str) -> Result<&[u8], Error>
Source§fn get_raw_path(&self, path: &[PathSegment<'_>]) -> Result<&[u8], Error>
fn get_raw_path(&self, path: &[PathSegment<'_>]) -> Result<&[u8], Error>
Path).Source§fn get<T: FromJsonSlice>(&self, path: &str) -> Result<T, Error>
fn get<T: FromJsonSlice>(&self, path: &str) -> Result<T, Error>
FromJsonSlice.Source§fn get_path<T: FromJsonSlice>(
&self,
path: &[PathSegment<'_>],
) -> Result<T, Error>
fn get_path<T: FromJsonSlice>( &self, path: &[PathSegment<'_>], ) -> Result<T, Error>
Source§fn get_at<T: FromJsonSlice>(&self, path: &Path) -> Result<T, Error>
fn get_at<T: FromJsonSlice>(&self, path: &Path) -> Result<T, Error>
Path.Source§fn contains(&self, path: &str) -> Result<bool, Error>
fn contains(&self, path: &str) -> Result<bool, Error>
true if the path exists (value span found), without decoding.Source§fn get_str(&self, path: &str) -> Result<&str, Error>
fn get_str(&self, path: &str) -> Result<&str, Error>
Source§fn get_str_path(&self, path: &[PathSegment<'_>]) -> Result<&str, Error>
fn get_str_path(&self, path: &[PathSegment<'_>]) -> Result<&str, Error>
Source§fn get_str_at(&self, path: &Path) -> Result<&str, Error>
fn get_str_at(&self, path: &Path) -> Result<&str, Error>
Path.Source§fn get_pointer<T: FromJsonSlice>(&self, pointer: &str) -> Result<T, Error>
fn get_pointer<T: FromJsonSlice>(&self, pointer: &str) -> Result<T, Error>
"/a~1b/0").Source§fn as_view<V: JsonView>(&self) -> Result<V, Error>
fn as_view<V: JsonView>(&self) -> Result<V, Error>
JsonView projection of the whole buffer.Source§fn view_at(&self, path: &str) -> Result<TypedDocRef<'_>, Error>
fn view_at(&self, path: &str) -> Result<TypedDocRef<'_>, Error>
path as a nested TypedDocRef (subtree as root).Source§fn view_at_path(
&self,
path: &[PathSegment<'_>],
) -> Result<TypedDocRef<'_>, Error>
fn view_at_path( &self, path: &[PathSegment<'_>], ) -> Result<TypedDocRef<'_>, Error>
Source§fn array_len(&self, path: &str) -> Result<usize, Error>
fn array_len(&self, path: &str) -> Result<usize, Error>
path.Source§fn elems(&self, path: &str) -> Result<ArrayElems<'_>, Error>
fn elems(&self, path: &str) -> Result<ArrayElems<'_>, Error>
path. Read moreSource§fn each_with<F>(&self, path: &str, f: F) -> Result<(), Error>
fn each_with<F>(&self, path: &str, f: F) -> Result<(), Error>
f.Source§fn each_with_decoded<T, F>(&self, path: &str, f: F) -> Result<(), Error>
fn each_with_decoded<T, F>(&self, path: &str, f: F) -> Result<(), Error>
T: FromJsonSlice (whole element).Source§fn each_get<T, F>(
&self,
array_path: &str,
field_path: &str,
f: F,
) -> Result<(), Error>
fn each_get<T, F>( &self, array_path: &str, field_path: &str, f: F, ) -> Result<(), Error>
Source§fn collect_each<T: FromJsonSlice>(&self, path: &str) -> Result<Vec<T>, Error>
fn collect_each<T: FromJsonSlice>(&self, path: &str) -> Result<Vec<T>, Error>
Vec<T> (explicit materialize).Source§fn collect_each_get<T: FromJsonSlice>(
&self,
array_path: &str,
field_path: &str,
) -> Result<Vec<T>, Error>
fn collect_each_get<T: FromJsonSlice>( &self, array_path: &str, field_path: &str, ) -> Result<Vec<T>, Error>
Source§fn collect_each_view<V: JsonView>(&self, path: &str) -> Result<Vec<V>, Error>
fn collect_each_view<V: JsonView>(&self, path: &str) -> Result<Vec<V>, Error>
Vec<V>.Source§fn root_kind(&self) -> Result<RootKind, Error>
fn root_kind(&self) -> Result<RootKind, Error>
Source§fn root_elems(&self) -> Result<ArrayElems<'_>, Error>
fn root_elems(&self) -> Result<ArrayElems<'_>, Error>
[...]).Source§fn presence(&self, path: &str) -> Result<Presence, Error>
fn presence(&self, path: &str) -> Result<Presence, Error>
null, or a real value.Source§fn is_null(&self, path: &str) -> Result<bool, Error>
fn is_null(&self, path: &str) -> Result<bool, Error>
true if the path exists and is JSON null.Source§fn get_nullable<T: FromJsonSlice>(&self, path: &str) -> Result<Option<T>, Error>
fn get_nullable<T: FromJsonSlice>(&self, path: &str) -> Result<Option<T>, Error>
Ok(None). Read moreSource§fn object_entries(&self) -> Result<ObjectEntries<'_>, Error>
fn object_entries(&self) -> Result<ObjectEntries<'_>, Error>
{"k":...}). Read moreSource§fn object_entries_at(&self, path: &str) -> Result<ObjectEntries<'_>, Error>
fn object_entries_at(&self, path: &str) -> Result<ObjectEntries<'_>, Error>
path.