mirror of
https://github.com/corda/corda.git
synced 2025-05-31 22:50:53 +00:00
Move toward allowing serialisation of concrete types
This commit is contained in:
parent
4b8d6e32c3
commit
de05a11511
@ -5,6 +5,7 @@ import java.io.NotSerializableException
|
|||||||
import java.lang.reflect.ParameterizedType
|
import java.lang.reflect.ParameterizedType
|
||||||
import java.lang.reflect.Type
|
import java.lang.reflect.Type
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.collections.LinkedHashMap
|
||||||
import kotlin.collections.Map
|
import kotlin.collections.Map
|
||||||
import kotlin.collections.iterator
|
import kotlin.collections.iterator
|
||||||
import kotlin.collections.map
|
import kotlin.collections.map
|
||||||
@ -17,10 +18,16 @@ class MapSerializer(val declaredType: ParameterizedType, factory: SerializerFact
|
|||||||
override val typeDescriptor = "$DESCRIPTOR_DOMAIN:${fingerprintForType(type, factory)}"
|
override val typeDescriptor = "$DESCRIPTOR_DOMAIN:${fingerprintForType(type, factory)}"
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val supportedTypes: Map<Class<out Map<*, *>>, (Map<*, *>) -> Map<*, *>> = mapOf(
|
private val supportedTypes: Map<Class<out Any>, (Map<*, *>) -> Map<*, *>> = mapOf(
|
||||||
Map::class.java to { map -> Collections.unmodifiableMap(map) },
|
Map::class.java to { map -> map },
|
||||||
SortedMap::class.java to { map -> Collections.unmodifiableSortedMap(TreeMap(map)) },
|
SortedMap::class.java to { map -> TreeMap(map) },
|
||||||
NavigableMap::class.java to { map -> Collections.unmodifiableNavigableMap(TreeMap(map)) }
|
NavigableMap::class.java to { map -> TreeMap(map) },
|
||||||
|
Dictionary::class.java to { map -> Hashtable(map) },
|
||||||
|
// concrete types for user convienience
|
||||||
|
HashMap::class.java to { map -> LinkedHashMap(map) },
|
||||||
|
LinkedHashMap::class.java to { map -> LinkedHashMap(map) },
|
||||||
|
TreeMap::class.java to { map -> TreeMap(map) },
|
||||||
|
Hashtable::class.java to { map -> Hashtable(map) }
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun findConcreteType(clazz: Class<*>): (Map<*, *>) -> Map<*, *> {
|
private fun findConcreteType(clazz: Class<*>): (Map<*, *>) -> Map<*, *> {
|
||||||
|
@ -0,0 +1,93 @@
|
|||||||
|
package net.corda.nodeapi.internal.serialization.amqp
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import java.util.*
|
||||||
|
import org.apache.qpid.proton.codec.Data
|
||||||
|
|
||||||
|
class DeserializeCollectionTests {
|
||||||
|
class TestSerializationOutput(
|
||||||
|
private val verbose: Boolean,
|
||||||
|
serializerFactory: SerializerFactory = SerializerFactory()) : SerializationOutput(serializerFactory) {
|
||||||
|
|
||||||
|
override fun writeSchema(schema: Schema, data: Data) {
|
||||||
|
if (verbose) println(schema)
|
||||||
|
super.writeSchema(schema, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* If you want to see the schema encoded into the envelope after serialisation change this to true
|
||||||
|
*/
|
||||||
|
private const val VERBOSE = false
|
||||||
|
}
|
||||||
|
|
||||||
|
val sf = SerializerFactory()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun mapTest() {
|
||||||
|
data class C(val c: Map<String, Int>)
|
||||||
|
val c = C (mapOf("A" to 1, "B" to 2))
|
||||||
|
|
||||||
|
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
DeserializationInput(sf).deserialize(serialisedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun sortedMapTest() {
|
||||||
|
data class C(val c: SortedMap<String, Int>)
|
||||||
|
val c = C(sortedMapOf ("A" to 1, "B" to 2))
|
||||||
|
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
DeserializationInput(sf).deserialize(serialisedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun dictionaryTest() {
|
||||||
|
data class C(val c: Dictionary<String, Int>)
|
||||||
|
var v : Hashtable<String, Int> = Hashtable()
|
||||||
|
v.put ("a", 1)
|
||||||
|
v.put ("b", 2)
|
||||||
|
val c = C(v)
|
||||||
|
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
DeserializationInput(sf).deserialize(serialisedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun navigableMapTest() {
|
||||||
|
data class C(val c: NavigableMap<String, Int>)
|
||||||
|
val c = C(TreeMap (mapOf("A" to 1, "B" to 3)).descendingMap())
|
||||||
|
|
||||||
|
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
DeserializationInput(sf).deserialize(serialisedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=java.lang.IllegalArgumentException::class)
|
||||||
|
fun HashMapTest() {
|
||||||
|
data class C(val c : HashMap<String, Int>)
|
||||||
|
val c = C (HashMap (mapOf("A" to 1, "B" to 2)))
|
||||||
|
|
||||||
|
// expect this to throw
|
||||||
|
TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun concreteTreeMapTest() {
|
||||||
|
data class C(val c: TreeMap<String, Int>)
|
||||||
|
val c = C(TreeMap (mapOf("A" to 1, "B" to 3)))
|
||||||
|
|
||||||
|
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
DeserializationInput(sf).deserialize(serialisedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun concreteLinkedHashMapTest() {
|
||||||
|
data class C(val c : LinkedHashMap<String, Int>)
|
||||||
|
val c = C (LinkedHashMap (mapOf("A" to 1, "B" to 2)))
|
||||||
|
|
||||||
|
val serialisedBytes = TestSerializationOutput(VERBOSE, sf).serialize(c)
|
||||||
|
val deserializedObj = DeserializationInput(sf).deserialize(serialisedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user