Skip to content

Commit 52fc5e1

Browse files
authored
Merge pull request #354 from elm-software/master
adding null support for type adapters w/ back compatibility
2 parents d77ae84 + f2ef7c8 commit 52fc5e1

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

klaxon/src/main/java/com/beust/klaxon/TypeFor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import kotlin.reflect.KClass
44

55
interface TypeAdapter<Output> where Output: Any {
66
fun classFor(type: Any): KClass<out Output>
7+
fun classForNullable(type: Any?): KClass<out Output>{
8+
return classFor(type as Any)
9+
}
710
}
811

912
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)

klaxon/src/main/kotlin/com/beust/klaxon/JsonObjectConverter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ class JsonObjectConverter(private val klaxon: Klaxon, private val allPaths: Hash
230230
// We have polymorphic information for this field. Retrieve its TypeAdapter,
231231
// instantiate it, and invoke it with the discriminant value.
232232
val discriminantFieldName = Annotations.retrieveJsonFieldName(klaxon, kc, polymorphicInfo.discriminantField)
233-
val discriminant = jsonObject[discriminantFieldName] as Any
234-
polymorphicInfo.adapter.createInstance().classFor(discriminant)
233+
val discriminant = jsonObject[discriminantFieldName]
234+
polymorphicInfo.adapter.createInstance().classForNullable(discriminant)
235235
} else {
236236
null
237237
}

klaxon/src/test/kotlin/com/beust/klaxon/TypeAdapterTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,41 @@ class TypeAdapterTest {
9797
assertThat(r[1]).isInstanceOf(Cat::class.java)
9898
}
9999

100+
101+
@TypeFor(field = "type", adapter = VehicleTypeAdapter::class)
102+
open class Vehicle(open val type: String)
103+
data class Car(override val type: String = "car") : Vehicle(type)
104+
data class Truck(override val type: String = "truck") : Vehicle(type)
105+
106+
class VehicleTypeAdapter : TypeAdapter<Vehicle> {
107+
108+
override fun classFor(type: Any): KClass<out Vehicle> {
109+
TODO("Not used - classForNullable replaces this")
110+
}
111+
112+
override fun classForNullable(type: Any?): KClass<out Vehicle> = when (type) {
113+
null -> Car::class
114+
"car" -> Car::class
115+
"truck" -> Truck::class
116+
else -> throw IllegalArgumentException("Unknown type: $type")
117+
}
118+
119+
}
120+
121+
@Test
122+
fun should_default_to_car() {
123+
val json = """
124+
[
125+
{ "type": "car" },
126+
{ "type": "truck" }
127+
{ "no_type": "should default to car..." }
128+
]
129+
"""
130+
val r = Klaxon().parseArray<Vehicle>(json)
131+
println(r)
132+
assertThat(r!![0]).isInstanceOf(Car::class.java)
133+
assertThat(r[1]).isInstanceOf(Truck::class.java)
134+
assertThat(r[2]).isInstanceOf(Car::class.java)
135+
}
136+
100137
}

0 commit comments

Comments
 (0)