File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed
commonMain/kotlin/io/github/optimumcode/json/pointer
commonTest/kotlin/io/github/optimumcode/json/pointer Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1
1
package io.github.optimumcode.json.pointer
2
2
3
+ import kotlinx.serialization.Serializable
3
4
import kotlin.jvm.JvmField
4
5
import kotlin.jvm.JvmStatic
5
6
@@ -13,6 +14,7 @@ public fun JsonPointer(path: String): JsonPointer = JsonPointer.compile(path)
13
14
* Implementation of a JSON pointer described in the specification
14
15
* [RFC6901](https://datatracker.ietf.org/doc/html/rfc6901).
15
16
*/
17
+ @Serializable(JsonPointerSerializer ::class )
16
18
public sealed class JsonPointer (
17
19
internal open val next : JsonPointer ? = null ,
18
20
) {
Original file line number Diff line number Diff line change
1
+ package io.github.optimumcode.json.pointer
2
+
3
+ import kotlinx.serialization.KSerializer
4
+ import kotlinx.serialization.descriptors.PrimitiveKind
5
+ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
6
+ import kotlinx.serialization.descriptors.SerialDescriptor
7
+ import kotlinx.serialization.encoding.Decoder
8
+ import kotlinx.serialization.encoding.Encoder
9
+
10
+ public object JsonPointerSerializer : KSerializer<JsonPointer> {
11
+ override val descriptor: SerialDescriptor =
12
+ PrimitiveSerialDescriptor (
13
+ " io.github.optimumcode.json.pointer.JsonPointer" ,
14
+ PrimitiveKind .STRING ,
15
+ )
16
+
17
+ override fun deserialize (decoder : Decoder ): JsonPointer = JsonPointer (decoder.decodeString())
18
+
19
+ override fun serialize (
20
+ encoder : Encoder ,
21
+ value : JsonPointer ,
22
+ ) {
23
+ encoder.encodeString(value.toString())
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package io.github.optimumcode.json.pointer
2
+
3
+ import io.kotest.core.spec.style.FunSpec
4
+ import io.kotest.matchers.shouldBe
5
+ import kotlinx.serialization.encodeToString
6
+ import kotlinx.serialization.json.Json
7
+
8
+ class JsonPointerSerializationTest : FunSpec () {
9
+ init {
10
+ listOf (
11
+ JsonPointer .ROOT to " " ,
12
+ JsonPointer (" /test" ) to " /test" ,
13
+ JsonPointer (" /te~0st" ) to " /te~0st" ,
14
+ JsonPointer (" /te~1st" ) to " /te~1st" ,
15
+ ).forEach { (pointer, expected) ->
16
+ test(" json pointer $pointer serialized" ) {
17
+ Json .encodeToString(pointer) shouldBe " \" $expected \" "
18
+ }
19
+
20
+ test(" json pointer $pointer deserialized" ) {
21
+ Json .decodeFromString(JsonPointer .serializer(), " \" $expected \" " ) shouldBe pointer
22
+ }
23
+ }
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments