Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit d683914

Browse files
author
Abhishek Shukla
committed
Add kotlinx serialization support
1 parent fb01569 commit d683914

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

jmespath-kotlinx/pom.xml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>jmespath-kotlinx</artifactId>
6+
<name>JMESPath Kotlinx</name>
7+
<description>A JMESPath implementation with kotlinx</description>
8+
9+
<parent>
10+
<groupId>io.burt</groupId>
11+
<artifactId>jmespath</artifactId>
12+
<version>0.5.2-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>jmespath-core</artifactId>
19+
<version>${project.parent.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>${project.groupId}</groupId>
23+
<artifactId>jmespath-core</artifactId>
24+
<version>${project.parent.version}</version>
25+
<type>test-jar</type>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.jetbrains.kotlin</groupId>
30+
<artifactId>kotlin-stdlib</artifactId>
31+
<version>${kotlin.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.jetbrains.kotlinx</groupId>
35+
<artifactId>kotlinx-serialization-json</artifactId>
36+
<version>1.4.1</version>
37+
</dependency>
38+
</dependencies>
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.jetbrains.kotlin</groupId>
43+
<artifactId>kotlin-maven-plugin</artifactId>
44+
<version>${kotlin.version}</version>
45+
<executions>
46+
<execution>
47+
<id>compile</id>
48+
<phase>compile</phase>
49+
<goals>
50+
<goal>compile</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
<configuration>
55+
<compilerPlugins>
56+
<plugin>kotlinx-serialization</plugin>
57+
</compilerPlugins>
58+
</configuration>
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.jetbrains.kotlin</groupId>
62+
<artifactId>kotlin-maven-serialization</artifactId>
63+
<version>${kotlin.version}</version>
64+
</dependency>
65+
</dependencies>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
<properties>
70+
<kotlin.version>1.6.0</kotlin.version>
71+
</properties>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package io.burt.jmespath.kotlinx
2+
3+
import io.burt.jmespath.BaseRuntime
4+
import io.burt.jmespath.JmesPathType
5+
import io.burt.jmespath.RuntimeConfiguration
6+
import kotlinx.serialization.ExperimentalSerializationApi
7+
import kotlinx.serialization.json.Json
8+
import kotlinx.serialization.json.JsonArray
9+
import kotlinx.serialization.json.JsonElement
10+
import kotlinx.serialization.json.JsonNull
11+
import kotlinx.serialization.json.JsonObject
12+
import kotlinx.serialization.json.JsonPrimitive
13+
import kotlinx.serialization.json.boolean
14+
import kotlinx.serialization.json.booleanOrNull
15+
import kotlinx.serialization.json.doubleOrNull
16+
import kotlinx.serialization.json.encodeToJsonElement
17+
import kotlinx.serialization.json.jsonArray
18+
import kotlinx.serialization.json.jsonObject
19+
import kotlinx.serialization.json.jsonPrimitive
20+
21+
22+
@OptIn(ExperimentalSerializationApi::class)
23+
class KotlinRuntime(configuration: RuntimeConfiguration, private val json: Json) :
24+
BaseRuntime<JsonElement>(configuration) {
25+
26+
27+
constructor() : this(RuntimeConfiguration.defaultConfiguration(), Json {
28+
encodeDefaults = true
29+
explicitNulls = true
30+
})
31+
32+
constructor(configuration: RuntimeConfiguration) : this(configuration, Json {
33+
encodeDefaults = true
34+
explicitNulls = true
35+
})
36+
37+
override fun toString(value: JsonElement) = value.jsonPrimitive.content
38+
39+
override fun parseString(str: String): JsonElement {
40+
return json.parseToJsonElement(str)
41+
}
42+
43+
override fun createNull(): JsonElement {
44+
return JsonNull
45+
}
46+
47+
override fun createString(str: String): JsonElement {
48+
return json.parseToJsonElement(str)
49+
}
50+
51+
override fun createBoolean(b: Boolean): JsonElement {
52+
return JsonPrimitive(b)
53+
}
54+
55+
override fun createNumber(n: Double): JsonElement {
56+
return JsonPrimitive(n)
57+
}
58+
59+
override fun createNumber(n: Long): JsonElement {
60+
return JsonPrimitive(n)
61+
}
62+
63+
override fun createObject(obj: MutableMap<JsonElement, JsonElement>?): JsonElement {
64+
return json.encodeToJsonElement(obj)
65+
}
66+
67+
override fun createArray(elements: MutableCollection<JsonElement>): JsonElement {
68+
return JsonArray(elements.toList())
69+
}
70+
71+
override fun getPropertyNames(value: JsonElement): Collection<JsonElement> {
72+
return if (value is JsonObject) {
73+
value.jsonObject.keys.map { JsonPrimitive(it) }
74+
} else
75+
emptyList()
76+
}
77+
78+
override fun getProperty(value: JsonElement, name: JsonElement): JsonElement {
79+
return value.jsonObject[name.jsonPrimitive.content] ?: JsonNull
80+
}
81+
82+
override fun typeOf(value: JsonElement): JmesPathType {
83+
return if (value == JsonNull)
84+
JmesPathType.NULL
85+
else if (value is JsonArray)
86+
JmesPathType.ARRAY
87+
else if (value is JsonObject)
88+
JmesPathType.OBJECT
89+
else if (value.jsonPrimitive.booleanOrNull != null)
90+
JmesPathType.BOOLEAN
91+
else if (value.jsonPrimitive.doubleOrNull != null)
92+
JmesPathType.NUMBER
93+
else
94+
JmesPathType.STRING
95+
}
96+
97+
override fun isTruthy(value: JsonElement): Boolean {
98+
return value.jsonPrimitive.jsonPrimitive.boolean
99+
}
100+
101+
override fun toNumber(value: JsonElement): Number {
102+
return value.jsonPrimitive.content.toDouble()
103+
}
104+
105+
override fun toList(value: JsonElement?): List<JsonElement> {
106+
return when (value) {
107+
is JsonArray -> value.jsonArray
108+
is JsonObject -> value.values.toList()
109+
else -> emptyList()
110+
}
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.burt.jmespath.kotlinx
2+
3+
import io.burt.jmespath.JmesPathComplianceTest
4+
import io.burt.jmespath.Adapter
5+
import io.burt.jmespath.RuntimeConfiguration
6+
import kotlinx.serialization.json.JsonElement
7+
8+
class KotlinxComplianceTest : JmesPathComplianceTest<JsonElement>() {
9+
override fun runtime(): Adapter<JsonElement> = KotlinRuntime(RuntimeConfiguration.defaultConfiguration())
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.burt.jmespath.kotlinx
2+
3+
4+
import io.burt.jmespath.JmesPathRuntimeTest
5+
import io.burt.jmespath.RuntimeConfiguration
6+
import kotlinx.serialization.json.JsonElement
7+
8+
class KotlinxTest : JmesPathRuntimeTest<JsonElement>() {
9+
override fun createRuntime(configuration: RuntimeConfiguration) = KotlinRuntime(configuration)
10+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<module>jmespath-jakarta-jsonp</module>
6161
<module>jmespath-gson</module>
6262
<module>jmespath-vertx</module>
63+
<module>jmespath-kotlinx</module>
6364
</modules>
6465

6566
<dependencies>

0 commit comments

Comments
 (0)