Allow to escape closing brackets in json query
This commit is contained in:
parent
d6ef396257
commit
20b1a7b5e5
@ -282,6 +282,13 @@ class JsonPath private constructor(val pieces: ImmutableList<Piece>) {
|
|||||||
*
|
*
|
||||||
* This allows to effectively escape dots (`.`), because
|
* This allows to effectively escape dots (`.`), because
|
||||||
* when reading array index, everything is read up until closing bracket.
|
* 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
|
* Another change is - empty keys
|
||||||
* (sequences like `..a. .key.value` -> `"" -> "" -> "a" -> " " -> "key" -> "value"`)
|
* (sequences like `..a. .key.value` -> `"" -> "" -> "a" -> " " -> "key" -> "value"`)
|
||||||
@ -293,15 +300,21 @@ class JsonPath private constructor(val pieces: ImmutableList<Piece>) {
|
|||||||
|
|
||||||
val current = StringBuilder()
|
val current = StringBuilder()
|
||||||
var readingIndex = false
|
var readingIndex = false
|
||||||
|
var escapeNext = false
|
||||||
|
|
||||||
for (char in path) {
|
for (char in path) {
|
||||||
if (readingIndex) {
|
if (readingIndex) {
|
||||||
if (char == ']') {
|
if (escapeNext) {
|
||||||
|
escapeNext = false
|
||||||
|
current.append(char)
|
||||||
|
} else if (char == ']') {
|
||||||
val finish = current.toString()
|
val finish = current.toString()
|
||||||
val finishInt = finish.toIntOrNull()
|
val finishInt = finish.toIntOrNull()
|
||||||
pieces.add(Piece(finish, if (finishInt != null) Hint.ARRAY else null))
|
pieces.add(Piece(finish, if (finishInt != null) Hint.ARRAY else null))
|
||||||
current.clear()
|
current.clear()
|
||||||
readingIndex = false
|
readingIndex = false
|
||||||
|
} else if (char == '\\') {
|
||||||
|
escapeNext = true
|
||||||
} else {
|
} else {
|
||||||
current.append(char)
|
current.append(char)
|
||||||
}
|
}
|
||||||
@ -311,6 +324,7 @@ class JsonPath private constructor(val pieces: ImmutableList<Piece>) {
|
|||||||
current.clear()
|
current.clear()
|
||||||
} else if (char == '[') {
|
} else if (char == '[') {
|
||||||
readingIndex = true
|
readingIndex = true
|
||||||
|
escapeNext = false
|
||||||
} else {
|
} else {
|
||||||
current.append(char)
|
current.append(char)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user