Skip to main content

TypedDoc

Struct TypedDoc 

Source
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

Source

pub fn new() -> Self

Empty buffer (not valid JSON until you write something).

Source

pub fn from_vec(bytes: Vec<u8>) -> Self

Take ownership of a buffer without copying.

Source

pub fn from_slice(slice: &[u8]) -> Self

Copy slice into a new owned document.

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Borrow the raw bytes.

Source

pub fn len(&self) -> usize

Length in bytes.

Source

pub fn is_empty(&self) -> bool

Source

pub fn into_bytes(self) -> Vec<u8> ⓘ

Consume into the underlying buffer.

Source

pub fn as_mut_vec(&mut self) -> &mut Vec<u8> ⓘ

Mutable access to the buffer (invalidates external indexes; prefer mutate for path ops).

Source

pub fn as_ref_doc(&self) -> TypedDocRef<'_>

Borrowed view of the same bytes.

Source

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.

Source

pub fn write_view<V: JsonView>(&mut self, view: &V) -> Result<(), Error>

Open-document write of a JsonView (only named paths are upserted).

Source

pub fn apply_ops(&mut self, ops: &[MutateOp]) -> Result<(), Error>

Apply a batch of crate::MutateOps (see crate::apply_ops).

Source

pub fn merge_shallow(&mut self, path: &str, patch: &[u8]) -> Result<(), Error>

Shallow-merge a patch object into the object at path ("" = root).

Source

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

Source

pub fn view_list<T: JsonView>( &self, path: &str, ) -> Result<ViewList<'_, T>, Error>

Open a ViewList of T: JsonView at path.

Source

pub fn value_list<T: FromJsonSlice>( &self, path: &str, ) -> Result<ValueList<'_, T>, Error>

Open a ValueList of T: FromJsonSlice at path.

Source

pub fn root_view_list<T: JsonView>(&self) -> Result<ViewList<'_, T>, Error>

Root document is a JSON array of views ([{...}, ...]).

Source

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§

Source§

impl AsRef<[u8]> for TypedDoc

Source§

fn as_ref(&self) -> &[u8] ⓘ

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for TypedDoc

Source§

fn clone(&self) -> TypedDoc

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TypedDoc

Source§

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

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

impl Default for TypedDoc

Source§

fn default() -> TypedDoc

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

impl Eq for TypedDoc

Source§

impl<'a> From<&'a TypedDoc> for TypedDocRef<'a>

Source§

fn from(doc: &'a TypedDoc) -> Self

Converts to this type from the input type.
Source§

impl From<&[u8]> for TypedDoc

Source§

fn from(slice: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<TypedDoc> for Vec<u8>

Source§

fn from(doc: TypedDoc) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for TypedDoc

Source§

fn from(bytes: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl Hash for TypedDoc

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl JsonDoc for TypedDoc

Source§

fn as_json_bytes(&self) -> &[u8] ⓘ

Raw JSON bytes for this document (or subdocument).
Source§

fn get_raw(&self, path: &str) -> Result<&[u8], Error>

Locate a path and return the raw value span (zero-copy).
Source§

fn get_raw_path(&self, path: &[PathSegment<'_>]) -> Result<&[u8], Error>

Locate using pre-parsed path segments (hot loops / reused Path).
Source§

fn get_raw_at(&self, path: &Path) -> Result<&[u8], Error>

Locate using an owned Path.
Source§

fn get<T: FromJsonSlice>(&self, path: &str) -> Result<T, Error>

Decode a path into an owned Rust value via FromJsonSlice.
Source§

fn get_path<T: FromJsonSlice>( &self, path: &[PathSegment<'_>], ) -> Result<T, Error>

Decode using pre-parsed segments.
Source§

fn get_at<T: FromJsonSlice>(&self, path: &Path) -> Result<T, Error>

Decode using an owned Path.
Source§

fn get_opt<T: FromJsonSlice>(&self, path: &str) -> Result<Option<T>, Error>

Like get, but Error::PathNotFound → Ok(None). Read more
Source§

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>

Zero-copy string content for a JSON string without escapes. Read more
Source§

fn get_str_path(&self, path: &[PathSegment<'_>]) -> Result<&str, Error>

Zero-copy string using pre-parsed segments.
Source§

fn get_str_at(&self, path: &Path) -> Result<&str, Error>

Zero-copy string using an owned Path.
Source§

fn get_pointer<T: FromJsonSlice>(&self, pointer: &str) -> Result<T, Error>

RFC 6901 JSON Pointer decode ("/a~1b/0").
Source§

fn as_view<V: JsonView>(&self) -> Result<V, Error>

Open-document JsonView projection of the whole buffer.
Source§

fn view_at(&self, path: &str) -> Result<TypedDocRef<'_>, Error>

Borrow the value at path as a nested TypedDocRef (subtree as root).
Source§

fn view_at_path( &self, path: &[PathSegment<'_>], ) -> Result<TypedDocRef<'_>, Error>

Nested view using pre-parsed segments.
Source§

fn array_len(&self, path: &str) -> Result<usize, Error>

Number of elements in the array at path.
Source§

fn elems(&self, path: &str) -> Result<ArrayElems<'_>, Error>

Iterator over raw element spans of the array at path. Read more
Source§

fn each_with<F>(&self, path: &str, f: F) -> Result<(), Error>
where F: FnMut(&[u8]) -> Result<(), Error>,

Stream each array element as a raw span into f.
Source§

fn each_with_decoded<T, F>(&self, path: &str, f: F) -> Result<(), Error>
where T: FromJsonSlice, F: FnMut(T) -> Result<(), Error>,

Stream each array element decoded as T: FromJsonSlice (whole element).
Source§

fn each_view_with<V, F>(&self, path: &str, f: F) -> Result<(), Error>
where V: JsonView, F: FnMut(V) -> Result<(), Error>,

Stream each array element as a JsonView. Read more
Source§

fn each_get<T, F>( &self, array_path: &str, field_path: &str, f: F, ) -> Result<(), Error>
where T: FromJsonSlice, F: FnMut(T) -> Result<(), Error>,

Stream a relative field from each array element (path parsed once). Read more
Source§

fn collect_each<T: FromJsonSlice>(&self, path: &str) -> Result<Vec<T>, Error>

Collect array elements into Vec<T> (explicit materialize).
Source§

fn collect_each_get<T: FromJsonSlice>( &self, array_path: &str, field_path: &str, ) -> Result<Vec<T>, Error>

Collect a relative field from each array element.
Source§

fn collect_each_view<V: JsonView>(&self, path: &str) -> Result<Vec<V>, Error>

Collect array elements as views into Vec<V>.
Source§

fn root_kind(&self) -> Result<RootKind, Error>

Classify the root JSON value (object, array, primitive, …).
Source§

fn is_object(&self) -> Result<bool, Error>

true when the root value is a JSON object.
Source§

fn is_array(&self) -> Result<bool, Error>

true when the root value is a JSON array.
Source§

fn root_elems(&self) -> Result<ArrayElems<'_>, Error>

Stream root array elements (document is [...]).
Source§

fn presence(&self, path: &str) -> Result<Presence, Error>

Presence of a path: missing, JSON null, or a real value.
Source§

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>

Decode a path, mapping missing and null to Ok(None). Read more
Source§

fn object_entries(&self) -> Result<ObjectEntries<'_>, Error>

Iterate object members at the document root ({"k":...}). Read more
Source§

fn object_entries_at(&self, path: &str) -> Result<ObjectEntries<'_>, Error>

Iterate object members at path.
Source§

impl PartialEq for TypedDoc

Source§

fn eq(&self, other: &TypedDoc) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TypedDoc

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.