From 20b1a7b5e5fbdf7d5981a248da96130146e93dc0 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 24 Dec 2024 13:52:49 +0700 Subject: [PATCH] Allow to escape closing brackets in json query --- .../ru/dbotthepony/kstarbound/json/JsonPath.kt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPath.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPath.kt index b1ad96ae..cd5e8d1c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPath.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/json/JsonPath.kt @@ -282,6 +282,13 @@ class JsonPath private constructor(val pieces: ImmutableList) { * * 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) { 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) { current.clear() } else if (char == '[') { readingIndex = true + escapeNext = false } else { current.append(char) }