Expand description
§jlens-core
The JSONata language engine behind jlens: lexer, parser, evaluator, function library and the evaluation audit subsystem.
Compile an expression once, then evaluate it against any number of JSON documents:
use jlens_core::{Bindings, Expression};
use serde_json::json;
let expr = Expression::compile("$sum(items[type = $kind].(price * qty))").unwrap();
let input = json!({"items": [
{"type": "a", "price": 2.5, "qty": 4},
{"type": "b", "price": 1.0, "qty": 1},
]});
let bindings = Bindings::new().var("kind", "a");
assert_eq!(expr.evaluate(Some(&input), &bindings).unwrap(), Some(json!(10)));
let err = Expression::compile("Account.Order[").unwrap_err();
assert_eq!(err.code(), "S0203");For editors, parse_recover keeps going after syntax errors and returns a partial tree
together with Diagnostics:
let ast = jlens_core::parse_recover("Account.");
assert!(ast.has_errors());
assert_eq!(ast.diagnostics[0].code, "S0207");Parts of this crate are ported from jsonata-js
(MIT license, reproduced in LICENSE); the ported modules say so in their documentation.
Re-exports§
pub use annotation::Annotation;pub use ast::Ast;pub use ast::Node;pub use ast::NodeKind;pub use ast::Span;pub use error::Diagnostic;pub use error::Error;pub use error::Result;pub use error::Severity;pub use functions::Category;pub use functions::FunctionInfo;pub use functions::catalog as function_catalog;
Modules§
- annotation
- Audit annotations written as JSONata doc-comments.
- ast
- The processed abstract syntax tree that the evaluator walks.
- audit
- Evaluation logging and auditing (see ADR 0004).
- debug
- Inspection helpers for the parsing pipeline, used by
jlens astand in tests. - error
- Errors, error codes and diagnostics.
- functions
- The built-in function library. Ported from jsonata-js
src/functions.jsandsrc/datetime.js(MIT). - input
- Inputs read one level at a time (ADR 0010).
- signature
- Function signatures such as
<s-nn?:s>. Ported from jsonata-jssrc/signature.js(MIT). - stack
- Stack headroom checks, so that deep recursion fails with
D1011instead of overflowing the stack.
Structs§
- Bindings
- Variables and host functions made available to an expression as
$name. - Callable
- A function passed to a
HostFunction, which it can call while it runs (ADR 0011). Given byHostCall::function. - Cancel
Token - A handle that stops evaluations from another thread.
- Document
- A JSON document in jlens’s internal representation.
- Expression
- A compiled JSONata expression.
- Host
Call - The context of a call to a
HostFunction. - Host
Function - A function implemented by the host application, bound to an expression through
Bindings::functionand called like a built-in. - Options
- Evaluation guardrails, and the clock and seed that make an evaluation reproducible.
Functions§
- format_
json - Serializes a result as JSON text the way JavaScript’s
JSON.stringifywould. Numbers use JavaScript’s formatting, so2432902008176640000stays an integer and1e21prints as1e+21. Withpretty, nested values are indented by two spaces. - parse
- Parses an expression, failing on the first syntax error.
- parse_
recover - Parses an expression, recovering from syntax errors.