@@ -9,6 +9,15 @@ import kotlinx.serialization.json.JsonObject
99import kotlinx.serialization.json.JsonPrimitive
1010import kotlin.jvm.JvmName
1111
12+ /* *
13+ * Creates a new [JsonPointer] that points to an [index] in the array.
14+ *
15+ * Example:
16+ * ```kotlin
17+ * val pointer = JsonPointer("/test")
18+ * val index = pointer[0] // "/test/0"
19+ * ```
20+ */
1221public operator fun JsonPointer.get (index : Int ): JsonPointer =
1322 JsonPointer (
1423 buildString {
@@ -21,6 +30,15 @@ public operator fun JsonPointer.get(index: Int): JsonPointer =
2130 },
2231 )
2332
33+ /* *
34+ * Creates a new [JsonPointer] that points to a [property] passed as a parameter.
35+ *
36+ * Example:
37+ *
38+ * ```kotlin
39+ * val pointer = JsonPointer.ROOT / "prop1" / "prop2" // "/prop1/prop2"
40+ * ```
41+ */
2442public operator fun JsonPointer.div (property : String ): JsonPointer =
2543 JsonPointer (
2644 buildString {
@@ -33,6 +51,23 @@ public operator fun JsonPointer.div(property: String): JsonPointer =
3351 },
3452 )
3553
54+ /* *
55+ * Appends [otherPointer] to the current [JsonPointer].
56+ * If current or [otherPointer] JSON pointer is an empty JSON pointer. The first not-empty pointer will be returned.
57+ * (or an empty pointer if both pointers are empty).
58+ *
59+ * If both are not-empty pointers the resulting JSON pointer will start from the current one
60+ * and [otherPointer] appended at the end.
61+ *
62+ * Example:
63+ * ```kotlin
64+ * val pointer = JsonPointer.ROOT + JsonPointer.ROOT // ""
65+ *
66+ * val pointer = JsonPointer.ROOT + JsonPointer("/test") // "/test"
67+ *
68+ * val pointer = JsonPointer("/prop") + JsonPointer("/test") // "/prop/test"
69+ * ```
70+ */
3671public operator fun JsonPointer.plus (otherPointer : JsonPointer ): JsonPointer {
3772 if (this is EmptyPointer ) {
3873 return otherPointer
@@ -53,16 +88,39 @@ public operator fun JsonPointer.plus(otherPointer: JsonPointer): JsonPointer {
5388 )
5489}
5590
91+ /* *
92+ * Returns a [JsonPointer] that is a relative pointer from current pointer to [other] pointer.
93+ * If current pointer is an empty pointer the [other] pointer will be returned.
94+ *
95+ * If the [other] pointer is not starts from the current pointer the [other] pointer will be returned.
96+ *
97+ * Example:
98+ * ```kotlin
99+ * val pointer = JsonPointer("/test").relative(JsonPointer("/test/0/data") // "/0/data"
100+ * ```
101+ *
102+ * @throws IllegalArgumentException when [other] is an empty pointer
103+ */
56104public fun JsonPointer.relative (other : JsonPointer ): JsonPointer {
57105 if (this is EmptyPointer ) {
58106 return other
59107 }
60108 require(other !is EmptyPointer ) { " empty pointer is not relative to any" }
61109 val currentValue = this .toString()
62110 val otherValue = other.toString()
63- return JsonPointer (otherValue.substringAfter(currentValue))
111+ val relative = otherValue.substringAfter(currentValue)
112+ return if (relative == otherValue) {
113+ other
114+ } else {
115+ JsonPointer (relative)
116+ }
64117}
65118
119+ /* *
120+ * Extracts [JsonElement] from the current JSON element that corresponds to the specified [JsonPointer].
121+ *
122+ * If [pointer] path does not exist in the current [JsonElement] the `null` will be returned.
123+ */
66124public tailrec fun JsonElement.at (pointer : JsonPointer ): JsonElement ? {
67125 return when (pointer) {
68126 is EmptyPointer -> this
0 commit comments