A jqlite program runs on one input value and produces a stream of results: none, one or many. The Jq class parses programs and evaluates them; a parsed program is a JqExpression that you can evaluate any number of times.
Jq.Evaluate(expression, input) parses the program with the default options, runs it and
returns all its results as an IReadOnlyList<JsonElement>:
using System.Text.Json;
using JQLite;
using var doc = JsonDocument.Parse("""{"items":[3,1,2]}""");
var results = Jq.Evaluate(".items | sort | .[]", doc.RootElement);
Console.WriteLine(results.Count); // 3
Console.WriteLine(string.Join(",", results.Select(r => r.GetRawText()))); // 1,2,3
The list is complete when it is returned: the whole program has run. A third, optional argument passes variables.
Parsing is the expensive step. When a program runs on many inputs, parse it once with
Jq.Parse and keep the JqExpression:
using System.Text.Json;
using JQLite;
JqExpression activeNames = Jq.Parse(".users[] | select(.active) | .name");
string[] payloads =
[
"""{"users":[{"name":"Alice","active":true},{"name":"Bob","active":false}]}""",
"""{"users":[{"name":"Carol","active":true}]}""",
];
foreach (var json in payloads)
{
using var doc = JsonDocument.Parse(json);
foreach (var name in activeNames.Evaluate(doc.RootElement))
Console.WriteLine(name);
}
// Alice
// Carol
Jq.Parse checks everything that does not depend on the input, before any evaluation:
Program too deeply nested);shout/0 is not defined);import and include, which are found and read through the resolver of the
options (see Modules).A failure is a JqParseException with a line and a column (see
Errors). A $name that the program does not bind is not checked: it is a host
variable, looked up when evaluated (see Variables).
Jq.Parse has three overloads: Parse(expression) with the default options,
Parse(expression, options) with a JqOptions, and
Parse(expression, resolver), the same as new JqOptions { Resolver = resolver }. None of the
arguments may be null. The options are read when the expression is parsed and apply to every
evaluation of it.
The overload of JqExpression.Evaluate that takes a callback gives each result as soon as it is
produced. The callback returns true to go on and false to stop; stopping is not an error:
using System.Text.Json;
using JQLite;
using var doc = JsonDocument.Parse("""{"limit": 1000000}""");
var multiples = new List<int>();
Jq.Parse("range(.limit) | select(. % 7 == 0)").Evaluate(doc.RootElement, result =>
{
multiples.Add(result.GetInt32());
return multiples.Count < 3; // false stops the evaluation
});
Console.WriteLine(string.Join(", ", multiples)); // 0, 7, 14
Only the work needed for the first three results is done: range is lazy. Use the callback when
a program can produce many or endless results, when you need only the first few, or when you want
to handle results while the rest are computed.
The two overloads differ when an error ends the evaluation:
| Overload | Results produced before the error |
|---|---|
Evaluate(input, variables), returns a list |
lost: the exception is thrown and no list is returned |
Evaluate(input, onResult, variables) |
already given to the callback |
An exception thrown by the callback is not caught: it ends the evaluation and reaches the caller
of Evaluate.
An evaluation has exactly one input value. jqlite has no input or inputs builtins (spec
§13); a program that uses them fails to parse with
input/0 is not defined. To process several values, evaluate the expression on each one, or use
the JSON Lines helpers, which do that for you.
halt ends the evaluation normally: the results produced before it are kept, and no exception is
thrown. halt_error throws (see Errors).
An empty program, or one with only whitespace and comments, is the identity .:
using System.Text.Json;
using JQLite;
using var doc = JsonDocument.Parse("[1,2]");
Console.WriteLine(Jq.Evaluate("", doc.RootElement)[0].GetRawText()); // [1,2]
A JqExpression is immutable. You can evaluate the same instance from many threads at once, each
evaluation with its own input and variables; one evaluation owns all its state.
using System.Text.Json;
using JQLite;
var scale = Jq.Parse("[.[] | . * $k] | add");
using var doc = JsonDocument.Parse("[1,2,3]");
var input = doc.RootElement;
var totals = new string[8];
Parallel.For(0, totals.Length, i =>
{
using var k = JsonDocument.Parse(i.ToString());
var variables = new Dictionary<string, JsonElement> { ["k"] = k.RootElement };
totals[i] = scale.Evaluate(input, variables)[0].GetRawText();
});
Console.WriteLine(string.Join(" ", totals)); // 0 6 12 18 24 30 36 42
An evaluation runs on the calling thread, from start to end; there is no asynchronous evaluation.
What you share between evaluations must be safe to share: the input element, the
resolver, which modulemeta may call from several evaluations at once, and the
Debug and Stderr handlers, which run on the thread of each
evaluation.
Deep recursion never overflows the .NET stack, whatever the size of the thread's stack: the
recursion limit is MaxDepth.