Skip to content

Commit

Permalink
Added json package, and added wip of jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
halvardssm committed Oct 13, 2024
1 parent a87f4b0 commit 445d2a9
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 6 deletions.
2 changes: 2 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@stdext/crypto": "jsr:@stdext/crypto",
"@stdext/encoding": "jsr:@stdext/encoding",
"@stdext/http": "jsr:@stdext/http",
"@stdext/json": "jsr:@stdext/json",
"@stdext/lexer": "jsr:@stdext/lexer",
"@stdext/types": "jsr:@stdext/types"
},
Expand All @@ -29,6 +30,7 @@
"./crypto",
"./encoding",
"./http",
"./json",
"./lexer",
"./types"
],
Expand Down
3 changes: 3 additions & 0 deletions json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @stdext/json

The json package, contains helpers for json parsing, querying and processing
8 changes: 8 additions & 0 deletions json/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "0.0.1",
"name": "@stdext/json",
"exports": {
".": "./mod.ts",
"./jsonpath": "./jsonpath.ts"
}
}
277 changes: 277 additions & 0 deletions json/jsonpath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
import { assertEquals } from "@std/assert";
import type {} from "./jsonpath.ts";
import { JSONPath, type JSONPathToken } from "./jsonpath.ts";

Deno.test("JSONPath > lexer has correct matchers", async (t) => {
await t.step("simple expression", () => {
const expected: JSONPathToken[] = [
{
index: 0,
type: "Root",
value: "$",
},
{
index: 1,
type: "Accessor",
value: ".",
},
{
index: 2,
type: "NameSelector",
value: "foo",
},
{
index: 5,
type: "SelectorStart",
value: "[",
},
{
index: 6,
type: "QuotedNameSelector",
value: "bar",
},
{
index: 11,
type: "SelectorEnd",
value: "]",
},
{
index: 12,
type: "Accessor",
value: ".",
},
{
index: 13,
type: "NameSelector",
value: "baz",
},
{
index: 16,
type: "SelectorStart",
value: "[",
},
{
index: 17,
type: "NumberSelector",
value: 1,
},
{
index: 18,
type: "SelectorEnd",
value: "]",
},
{
index: 19,
type: "SelectorStart",
value: "[",
},
{
index: 20,
type: "NumberSelector",
value: 1,
},
{
index: 21,
type: "SliceSeparator",
value: ",",
},
{
index: 22,
type: "NumberSelector",
value: 2,
},
{
index: 23,
type: "SelectorEnd",
value: "]",
},
{
index: 24,
type: "SelectorStart",
value: "[",
},
{
index: 25,
type: "NumberSelector",
value: 1,
},
{
index: 26,
type: "SliceSelector",
value: ":",
},
{
index: 27,
type: "NumberSelector",
value: 2,
},
{
index: 28,
type: "SliceSelector",
value: ":",
},
{
index: 29,
type: "NumberSelector",
value: 3,
},
{
index: 30,
type: "SelectorEnd",
value: "]",
},
{
index: 31,
type: "SelectorStart",
value: "[",
},
{
index: 32,
type: "Wildcard",
value: "*",
},
{
index: 33,
type: "SelectorEnd",
value: "]",
},
{
index: 34,
type: "FilterSelectorStart",
value: "[?",
},
{
index: 36,
type: "CurrentNode",
value: "@",
},
{
index: 37,
type: "Accessor",
value: ".",
},
{
index: 38,
type: "NameSelector",
value: "a",
},
{
index: 40,
type: "ComparisonEqual",
value: "==",
},
{
index: 43,
type: "QuotedNameSelector",
value: "a",
},
{
index: 46,
type: "SelectorEnd",
value: "]",
},
{
index: 47,
type: "FilterSelectorStart",
value: "[?",
},
{
index: 49,
type: "GroupStart",
value: "(",
},
{
index: 50,
type: "CurrentNode",
value: "@",
},
{
index: 51,
type: "Accessor",
value: ".",
},
{
index: 52,
type: "NameSelector",
value: "a",
},
{
index: 54,
type: "ComparisonEqual",
value: "==",
},
{
index: 57,
type: "QuotedNameSelector",
value: "a",
},
{
index: 60,
type: "GroupEnd",
value: ")",
},
{
index: 61,
type: "SelectorEnd",
value: "]",
},
{
index: 62,
type: "FilterSelectorStart",
value: "[?",
},
{
index: 64,
type: "NameSelector",
value: "search",
},
{
index: 70,
type: "GroupStart",
value: "(",
},
{
index: 71,
type: "CurrentNode",
value: "@",
},
{
index: 72,
type: "Accessor",
value: ".",
},
{
index: 73,
type: "NameSelector",
value: "a",
},
{
index: 74,
type: "SliceSeparator",
value: ",",
},
{
index: 76,
type: "QuotedNameSelector",
value: "[bc]",
},
{
index: 82,
type: "GroupEnd",
value: ")",
},
{
index: 83,
type: "SelectorEnd",
value: "]",
},
];
const l = new JSONPath({});
assertEquals(
l._getLexer(
`$.foo['bar'].baz[1][1,2][1:2:3][*][?@.a == 'a'][?(@.a == 'a')][?search(@.a, "[bc]")]`,
).tokenize(),
expected,
);
});
});
Loading

0 comments on commit 445d2a9

Please sign in to comment.