Serialising null chars was broken

The recent changes that were put in to allow chars to be serialised at
all failed to take into account nullability and thus if a char was null
and that object was first serlialised then desierliased it would throw
a NPE
This commit is contained in:
Katelyn Baker 2017-07-27 12:08:03 +01:00
parent c721316d67
commit 06a8a4bada
2 changed files with 17 additions and 3 deletions

View File

@ -113,13 +113,16 @@ sealed class PropertySerializer(val name: String, val readMethod: Method, val re
* casting back to a char otherwise it's treated as an Integer and a TypeMismatch occurs
*/
class AMQPCharPropertySerializer(name: String, readMethod: Method) :
PropertySerializer(name, readMethod, Char::class.java) {
PropertySerializer(name, readMethod, Character::class.java) {
override fun writeClassInfo(output: SerializationOutput) {}
override fun readProperty(obj: Any?, schema: Schema, input: DeserializationInput) = (obj as Int).toChar()
override fun readProperty(obj: Any?, schema: Schema, input: DeserializationInput): Any? {
return if(obj == null) null else (obj as Int).toChar()
}
override fun writeProperty(obj: Any?, data: Data, output: SerializationOutput) {
data.putChar((readMethod.invoke(obj) as Char).toInt())
val input = readMethod.invoke(obj)
if (input != null) data.putChar((input as Char).toInt()) else data.putNull()
}
}
}

View File

@ -50,6 +50,17 @@ class DeserializeSimpleTypesTests {
assertEquals(c.c, deserializedC.c)
}
@Test
fun testNullCharacter() {
data class C(val c: Char?)
val c = C(null)
val serialisedC = SerializationOutput().serialize(c)
val deserializedC = DeserializationInput().deserialize(serialisedC)
assertEquals(c.c, deserializedC.c)
}
@Test
fun testArrayOfInt() {
class IA(val ia: Array<Int>)