File tree Expand file tree Collapse file tree 2 files changed +42
-22
lines changed
src/commonMain/kotlin/com/github/optimumcode/json/pointer Expand file tree Collapse file tree 2 files changed +42
-22
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,46 @@ public sealed class JsonPointer(
20
20
internal val next : JsonPointer ? = null ,
21
21
) {
22
22
23
+ /* *
24
+ * Creates a new [JsonPointer] that points to an [index] in the array.
25
+ *
26
+ * Example:
27
+ * ```kotlin
28
+ * val pointer = JsonPointer("/test").atIndex(0) // "/test/0"
29
+ * ```
30
+ */
31
+ public fun atIndex (index : Int ): JsonPointer =
32
+ JsonPointer (
33
+ buildString {
34
+ val pointer = this @JsonPointer.toString()
35
+ append(pointer)
36
+ if (! pointer.endsWith(SEPARATOR )) {
37
+ append(SEPARATOR )
38
+ }
39
+ append(index)
40
+ },
41
+ )
42
+
43
+ /* *
44
+ * Creates a new [JsonPointer] that points to a [property] passed as a parameter.
45
+ *
46
+ * Example:
47
+ * ```kotlin
48
+ * val pointer = JsonPointer.ROOT.atProperty("prop1").atProperty("prop2") // "/prop1/prop2"
49
+ * ```
50
+ */
51
+ public fun atProperty (property : String ): JsonPointer =
52
+ JsonPointer (
53
+ buildString {
54
+ val pointer = this @JsonPointer.toString()
55
+ append(pointer)
56
+ if (! pointer.endsWith(SEPARATOR )) {
57
+ append(SEPARATOR )
58
+ }
59
+ append(property)
60
+ },
61
+ )
62
+
23
63
override fun toString (): String {
24
64
return if (pathOffset <= 0 ) {
25
65
fullPath
Original file line number Diff line number Diff line change @@ -18,17 +18,7 @@ import kotlin.jvm.JvmName
18
18
* val index = pointer[0] // "/test/0"
19
19
* ```
20
20
*/
21
- public operator fun JsonPointer.get (index : Int ): JsonPointer =
22
- JsonPointer (
23
- buildString {
24
- val pointer = this @get.toString()
25
- append(pointer)
26
- if (! pointer.endsWith(JsonPointer .SEPARATOR )) {
27
- append(JsonPointer .SEPARATOR )
28
- }
29
- append(index)
30
- },
31
- )
21
+ public operator fun JsonPointer.get (index : Int ): JsonPointer = atIndex(index)
32
22
33
23
/* *
34
24
* Creates a new [JsonPointer] that points to a [property] passed as a parameter.
@@ -39,17 +29,7 @@ public operator fun JsonPointer.get(index: Int): JsonPointer =
39
29
* val pointer = JsonPointer.ROOT / "prop1" / "prop2" // "/prop1/prop2"
40
30
* ```
41
31
*/
42
- public operator fun JsonPointer.div (property : String ): JsonPointer =
43
- JsonPointer (
44
- buildString {
45
- val pointer = this @div.toString()
46
- append(pointer)
47
- if (! pointer.endsWith(JsonPointer .SEPARATOR )) {
48
- append(JsonPointer .SEPARATOR )
49
- }
50
- append(property)
51
- },
52
- )
32
+ public operator fun JsonPointer.div (property : String ): JsonPointer = atProperty(property)
53
33
54
34
/* *
55
35
* Appends [otherPointer] to the current [JsonPointer].
You can’t perform that action at this time.
0 commit comments