JMESPath expressions, parsing and JSON evaluation for MoonBit
///|
test {
let input = @json.parse(
(
#|{"items":[{"name":"Zoe","active":true},{"name":"Ada","active":true},{"name":"Bob","active":false}]}
),
)
let result = @jmespath.search("items[?active].name | sort(@)", input)
assert_eq(result, @json.parse("[\"Ada\",\"Zoe\"]"))
}///|
test {
let expression = @jmespath.parse("length(@)")
assert_eq(expression.evaluate(@json.parse("[1,2,3]")), Json::number(3))
assert_eq(expression.evaluate(Json::string("hello")), Json::number(5))
}///|
test {
let source = "items["
try {
@jmespath.parse(source) |> ignore
fail("expected an incomplete expression")
} catch {
@jmespath.JmespathError(diagnostic) => {
assert_eq(diagnostic.code, @jmespath.Syntax)
assert_eq(diagnostic.line_column(source), (1, 7))
}
error => raise error
}
}fn Expression::evaluate(self : Expression, input : Json, max_steps? : Int) -> Json raise JmespathErrorpub(all) enum NodeKind {
Identity
Field(String)
Literal(Json)
Subexpression(Node, Node)
Index(Node, Int)
Slice(Node, Int?, Int?, Int?)
Projection(Node, Node)
ObjectProjection(Node, Node)
Flatten(Node)
Filter(Node, Node, Node)
MultiList(Array[Node])
MultiHash(Array[(String, Node)])
Function(String, Array[Node])
ExpressionRef(Node)
Binary(Operator, Node, Node)
Not(Node)
} derive(Eq, Debug)Install
Download zipJMESPath expressions, parsing and JSON evaluation for MoonBit