Skip to content

Commit

Permalink
Add RFC6901 json pointer support
Browse files Browse the repository at this point in the history
now Path can convert RFC6901 style path to json path
for example
Path.ofJosnPointer("/abc/0") can convert to Path.of(".abc.[0]")
  • Loading branch information
leonchen83 committed Feb 24, 2022
1 parent ffffec5 commit 8178cff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/redislabs/modules/rejson/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Path of(final String strPath) {
return new Path(strPath);
}

public static Path ofJosnPointer(final String strPath) {
public static Path ofJsonPointer(final String strPath) {
return new Path(parse(strPath));
}

Expand Down Expand Up @@ -111,9 +111,9 @@ private static String parse(String path) {
builder.append('~');
}
if (builder.length() > 0 && num) {
r.append(".[" + builder + "]");
r.append(".[" + builder.toString() + "]");
} else {
r.append("." + builder);
r.append("." + builder.toString());
}
num = true;
builder.setLength(0);
Expand Down Expand Up @@ -153,9 +153,9 @@ private static String parse(String path) {
}
if (builder.length() > 0) {
if (num) {
r.append(".[" + builder + "]");
r.append(".[" + builder.toString() + "]");
} else {
r.append("." + builder);
r.append("." + builder.toString());
}
}
return r.toString();
Expand Down
22 changes: 12 additions & 10 deletions src/test/java/com/redislabs/modules/rejson/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ public void testPathHashCode() {

@Test
public void testJsonPointer() {
assertEquals(Path.ofJosnPointer("/"), Path.ROOT_PATH);
assertEquals(Path.ofJosnPointer("/a/b/c"), Path.of(".a.b.c"));
assertEquals(Path.ofJosnPointer("/a/0/c"), Path.of(".a.[0].c"));
assertEquals(Path.ofJosnPointer("/a/0b/c"), Path.of(".a.0b.c"));
assertEquals(Path.ofJosnPointer("/ab/cd/1010"), Path.of(".ab.cd.[1010]"));
assertEquals(Path.ofJosnPointer("/a/b/c").hashCode(), Path.of(".a.b.c").hashCode());
assertEquals(Path.ofJsonPointer("/"), Path.ROOT_PATH);
assertEquals(Path.ofJsonPointer("/a/b/c"), Path.of(".a.b.c"));
assertEquals(Path.ofJsonPointer("/a/0/c"), Path.of(".a.[0].c"));
assertEquals(Path.ofJsonPointer("/a/0b/c"), Path.of(".a.0b.c"));
assertEquals(Path.ofJsonPointer("/ab/cd/1010"), Path.of(".ab.cd.[1010]"));
assertEquals(Path.ofJsonPointer("/a/b/c").hashCode(), Path.of(".a.b.c").hashCode());

//escape test
assertEquals(Path.ofJosnPointer("/a/~0/c"), Path.of(".a.~.c"));
assertEquals(Path.ofJosnPointer("/a/~1/c"), Path.of(".a./.c"));
assertEquals(Path.ofJosnPointer("/a/~~/c"), Path.of(".a.~~.c"));
assertEquals(Path.ofJosnPointer("/~/~~~/~~"), Path.of(".~.~~~.~~"));
assertEquals(Path.ofJsonPointer("/a/~0/c"), Path.of(".a.~.c"));
assertEquals(Path.ofJsonPointer("/a/~1/c"), Path.of(".a./.c"));
assertEquals(Path.ofJsonPointer("/a/~~/c"), Path.of(".a.~~.c"));
assertEquals(Path.ofJsonPointer("/~/~~~/~~"), Path.of(".~.~~~.~~"));
assertEquals(Path.ofJsonPointer("/~/~~~0/~~"), Path.of(".~.~~~.~~"));
assertEquals(Path.ofJsonPointer("/~/'.'/~~"), Path.of(".~.'.'.~~"));
}
}

0 comments on commit 8178cff

Please sign in to comment.