Allow to escape closing brackets in json query

This commit is contained in:
DBotThePony 2024-12-24 13:52:49 +07:00
parent d6ef396257
commit 20b1a7b5e5
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -282,6 +282,13 @@ class JsonPath private constructor(val pieces: ImmutableList<Piece>) {
*
* This allows to effectively escape dots (`.`), because
* when reading array index, everything is read up until closing bracket.
* To escape a bracket, use backward slash, like this:
*
* `someValue.a[bb\]s]`
*
* Which will parse `bb]s` as last index string
*
* Back slashes do not have unique treatment outside bracket context
*
* Another change is - empty keys
* (sequences like `..a. .key.value` -> `"" -> "" -> "a" -> " " -> "key" -> "value"`)
@ -293,15 +300,21 @@ class JsonPath private constructor(val pieces: ImmutableList<Piece>) {
val current = StringBuilder()
var readingIndex = false
var escapeNext = false
for (char in path) {
if (readingIndex) {
if (char == ']') {
if (escapeNext) {
escapeNext = false
current.append(char)
} else if (char == ']') {
val finish = current.toString()
val finishInt = finish.toIntOrNull()
pieces.add(Piece(finish, if (finishInt != null) Hint.ARRAY else null))
current.clear()
readingIndex = false
} else if (char == '\\') {
escapeNext = true
} else {
current.append(char)
}
@ -311,6 +324,7 @@ class JsonPath private constructor(val pieces: ImmutableList<Piece>) {
current.clear()
} else if (char == '[') {
readingIndex = true
escapeNext = false
} else {
current.append(char)
}