@@ -9,6 +9,15 @@ import kotlinx.serialization.json.JsonObject
9
9
import kotlinx.serialization.json.JsonPrimitive
10
10
import kotlin.jvm.JvmName
11
11
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
+ */
12
21
public operator fun JsonPointer.get (index : Int ): JsonPointer =
13
22
JsonPointer (
14
23
buildString {
@@ -21,6 +30,15 @@ public operator fun JsonPointer.get(index: Int): JsonPointer =
21
30
},
22
31
)
23
32
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
+ */
24
42
public operator fun JsonPointer.div (property : String ): JsonPointer =
25
43
JsonPointer (
26
44
buildString {
@@ -33,6 +51,23 @@ public operator fun JsonPointer.div(property: String): JsonPointer =
33
51
},
34
52
)
35
53
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
+ */
36
71
public operator fun JsonPointer.plus (otherPointer : JsonPointer ): JsonPointer {
37
72
if (this is EmptyPointer ) {
38
73
return otherPointer
@@ -53,16 +88,39 @@ public operator fun JsonPointer.plus(otherPointer: JsonPointer): JsonPointer {
53
88
)
54
89
}
55
90
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
+ */
56
104
public fun JsonPointer.relative (other : JsonPointer ): JsonPointer {
57
105
if (this is EmptyPointer ) {
58
106
return other
59
107
}
60
108
require(other !is EmptyPointer ) { " empty pointer is not relative to any" }
61
109
val currentValue = this .toString()
62
110
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
+ }
64
117
}
65
118
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
+ */
66
124
public tailrec fun JsonElement.at (pointer : JsonPointer ): JsonElement ? {
67
125
return when (pointer) {
68
126
is EmptyPointer -> this
0 commit comments