|
| 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 | +} |
0 commit comments