Skip to content

Commit d73fc22

Browse files
authored
allow toJson with star projections (#284)
1 parent a0d9cf3 commit d73fc22

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

geojson/src/commonMain/kotlin/org/maplibre/spatialk/geojson/GeoJson.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public data object GeoJson {
2525
public val jsonFormat: Json = Json {
2626
ignoreUnknownKeys = true
2727
serializersModule = SerializersModule {
28-
polymorphicDefaultSerializer(GeoJsonObject::class) {
28+
polymorphicDefaultSerializer(GeoJsonObject::class) { obj ->
2929
val serializer =
30-
when (it) {
30+
when (obj) {
3131
is Point -> Point.serializer()
3232
is MultiPoint -> MultiPoint.serializer()
3333
is LineString -> LineString.serializer()
@@ -39,7 +39,7 @@ public data object GeoJson {
3939
is Feature<*, *> ->
4040
Feature.serializer(
4141
Geometry.serializer().nullable,
42-
when (val props = it.properties) {
42+
when (val props = obj.properties) {
4343
is JsonObject? -> JsonObject.serializer().nullable
4444
else -> {
4545
@OptIn(InternalSerializationApi::class)
@@ -50,7 +50,7 @@ public data object GeoJson {
5050
is FeatureCollection<*, *> ->
5151
FeatureCollection.serializer(
5252
Geometry.serializer().nullable,
53-
when (val props = it.features.firstOrNull()?.properties) {
53+
when (val props = obj.features.firstNotNullOf { it.properties }) {
5454
is JsonObject? -> JsonObject.serializer().nullable
5555
else -> {
5656
@OptIn(InternalSerializationApi::class)

geojson/src/commonMain/kotlin/org/maplibre/spatialk/geojson/helpers.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package org.maplibre.spatialk.geojson
44

55
import kotlin.jvm.JvmSynthetic
6+
import kotlin.reflect.KTypeProjection
7+
import kotlin.reflect.typeOf
68
import kotlinx.serialization.SerializationException
79
import kotlinx.serialization.json.JsonObject
810

@@ -27,7 +29,14 @@ import kotlinx.serialization.json.JsonObject
2729
* @throws SerializationException if serialization fails.
2830
* @see GeoJson.encodeToString
2931
*/
30-
public inline fun <reified T : GeoJsonObject> T.toJson(): String = GeoJson.encodeToString<T>(this)
32+
public inline fun <reified T : GeoJsonObject> T.toJson(): String {
33+
// If the type is known at compile time (no star projections), use that serializer
34+
return if (typeOf<T>().arguments.none { it == KTypeProjection.STAR })
35+
GeoJson.encodeToString<T>(this)
36+
37+
// Otherwise, use runtime lookup (GeoJsonObject polymorphic default serializer)
38+
else GeoJson.encodeToString<GeoJsonObject>(this)
39+
}
3140

3241
/**
3342
* Marks GeoJSON API that should be used with caution.

geojson/src/commonTest/kotlin/org/maplibre/spatialk/geojson/FeatureTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,10 @@ class FeatureTest {
508508
// Map<*, *> properties not tested because the fallback serializer selection does not
509509
// support generic types.
510510

511-
val nullPropsFeature: GeoJsonObject = Feature(null, null)
512-
val jsonObjectPropsFeature: GeoJsonObject =
511+
val nullPropsFeature: Feature<*, *> = Feature(null, null)
512+
val jsonObjectPropsFeature: Feature<*, *> =
513513
Feature(null, JsonObject(mapOf("key" to JsonPrimitive("value"))))
514-
val dataClassPropsFeature: GeoJsonObject = Feature(null, NameProp("test"))
514+
val dataClassPropsFeature: Feature<*, *> = Feature(null, NameProp("test"))
515515

516516
val nullDeserialized = GeoJsonObject.fromJson(nullPropsFeature.toJson()) as Feature<*, *>
517517
val jsonObjectDeserialized =

0 commit comments

Comments
 (0)