@@ -6,8 +6,111 @@ data model and syntax.
66
77JSON Expressions are JIT compiled to efficient machine code.
88
9- Example ` 1 + 2 ` JSON Expression:
9+ JSON Expression is a simple JSON DSL, which allows to write expressions and
10+ evaluate expressions.
1011
11- ``` json
12- [" +" , 1 , 2 ]
12+ For example, the following expression
13+
14+ ``` js
15+ [' +' , 1 , 2 ]; // 1 + 2
16+ ```
17+
18+ evaluates to 3.
19+
20+
21+ ## Usage
22+
23+ ` json-expression ` library can immediately evaluate expressions or it can
24+ compile an efficient expression to a function, which will execute about
25+ an order of magnitude faster.
26+
27+ Evaluating expression immediately as-is.
28+
29+ ``` ts
30+ import {evaluate } from ' @jsonjoy.com/json-expression' ;
31+
32+ const expression = [' +' , 1 , [' $' , ' /foo' ]];
33+ const data = {foo: 2 };
34+
35+ evaluate (expression , {data }); // 3
36+ ```
37+
38+ Pre-compiling expression to an optimized function.
39+
40+ ``` ts
41+ import {JsonExpressionCodegen } from ' @jsonjoy.com/json-expression' ;
42+
43+ const expression = [' +' , 1 , [' $' , ' /foo' ]];
44+ const codegen = new JsonExpressionCodegen ({expression });
45+ const fn = codegen .run ().compile ();
46+ const data = {foo: 2 };
47+
48+ fn ({data }); // 3
49+ ```
50+
51+
52+ ## Documentation
53+
54+ ` json-expression ` library supports few dozen operators, see full list in ` Expr `
55+ type [ here] ( ./types.ts ) .
56+
57+ Parsing rules:
58+
59+ 1 . JSON Expression is a valid JSON value.
60+ 2 . All expressions are JSON arrays, which start with a string which specifies
61+ the operator and remaining array elements are operands. For example, the
62+ "get" operator fetches some value from supplied data using JSON
63+ Pointer:` ["get", "/some/path"] ` .
64+ 3 . All other values are treated as literals. Except for arrays, which need to
65+ be enclosed in square brackets. For example, to specify an empty array, you
66+ box your array in square brackets: ` [[]] ` . This evaluates to an empty array
67+ JSON value ` [] ` .
68+
69+
70+ ## Use Cases
71+
72+ Consider you application receives a stream of JSON Cloud Events, like this:
73+
74+ ``` js
75+ {
76+ " specversion" : " 1.0" ,
77+ " type" : " com.example.someevent" ,
78+ " source" : " /mycontext" ,
79+ " subject" : null ,
80+ " id" : " C234-1234-1234" ,
81+ " time" : " 2018-04-05T17:31:00Z" ,
82+ " comexampleextension1" : " value" ,
83+ " comexampleothervalue" : 5 ,
84+ " datacontenttype" : " application/json" ,
85+ " data" : {
86+ " appinfoA" : " abc" ,
87+ " appinfoB" : 123 ,
88+ " appinfoC" : true
89+ }
90+ }
91+ ```
92+
93+ You could write and compile a JSON Expression to efficiently filter out events
94+ you are interested in, for example your expression could look like this:
95+
96+ ``` js
97+ [
98+ ' and' ,
99+ [' ==' , [' $' , ' /specversion' ], ' 1.0' ],
100+ [' starts' , [' $' , ' /type' ], ' com.example.' ],
101+ [' in' , [' $' , ' /datacontenttype' ], [[' application/octet-stream' , ' application/json' ]]],
102+ [' ==' , [' $' , ' /data/appinfoA' ], ' abc' ],
103+ ];
104+ ```
105+
106+
107+ ## Benchmark
108+
109+ ```
110+ node benchmarks/json-expression/main.js
111+ json-joy/json-expression JsonExpressionCodegen x 14,557,786 ops/sec ±0.09% (100 runs sampled), 69 ns/op
112+ json-joy/json-expression JsonExpressionCodegen with codegen x 170,098 ops/sec ±0.13% (101 runs sampled), 5879 ns/op
113+ json-joy/json-expression evaluate x 864,956 ops/sec ±0.10% (101 runs sampled), 1156 ns/op
114+ json-logic-js x 821,799 ops/sec ±0.18% (99 runs sampled), 1217 ns/op
115+ Fastest is json-joy/json-expression JsonExpressionCodegen
13116```
0 commit comments