Skip to main content

Document

Struct Document 

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

A JSON document in jlens’s internal representation.

Every evaluate call converts the parts of its input document that the expression reads from serde_json::Value, one level at a time. An expression that reads most of a large document can spend more on that than on the evaluation itself. To evaluate several expressions (a set of rules, say) or the same one several times against one document, convert it once into a Document and use Expression::evaluate_document.

use jlens_core::{Bindings, Document, Expression, Options};

let doc = Document::parse(r#"{"items": [{"price": 2}, {"price": 3}]}"#).unwrap();
let rules = ["$sum(items.price) > 4", "$count(items) < 10"];
for rule in rules {
    let expr = Expression::compile(rule).unwrap();
    let result = expr.evaluate_document(&doc, &Bindings::new(), &Options::default());
    assert_eq!(result.unwrap(), Some(serde_json::json!(true)));
}

Evaluations never modify the document. The one exception is a transform (|...|) whose expression rebinds $clone to a function that does not copy its argument.

A Document is not Send. Convert it on the thread that evaluates against it.

Implementations§

Source§

impl Document

Source

pub fn new(json: &JsonValue) -> Self

Converts a serde_json value.

Source

pub fn parse(text: &str) -> Result<Self>

Parses JSON text straight into the internal representation. This is faster than parsing into a serde_json::Value and converting it.

Source

pub fn to_json(&self) -> JsonValue

The document as JSON.

Trait Implementations§

Source§

impl Debug for Document

Source§

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

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

impl From<&Value> for Document

Source§

fn from(json: &JsonValue) -> Self

Converts to this type from the input type.

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