2017-11-08 16:23:38 +00:00
|
|
|
Pluggable Serializers for CorDapps
|
|
|
|
==================================
|
2017-12-04 13:10:02 +00:00
|
|
|
|
|
|
|
.. contents::
|
|
|
|
|
2017-12-04 17:56:27 +00:00
|
|
|
To be serializable by Corda Java classes must be compiled with the -parameters switch to enable matching of its properties
|
2017-12-04 13:10:02 +00:00
|
|
|
to constructor parameters. This is important because Corda's internal AMQP serialization scheme will only construct
|
2017-11-24 12:04:17 +00:00
|
|
|
objects using their constructors. However, when recompilation isn't possible, or classes are built in such a way that
|
2017-12-04 16:55:59 +00:00
|
|
|
they cannot be easily modified for simple serialization, CorDapps can provide custom proxy serializers that Corda
|
2017-12-04 13:10:02 +00:00
|
|
|
can use to move from types it cannot serialize to an interim representation that it can with the transformation to and
|
2017-11-24 12:04:17 +00:00
|
|
|
from this proxy object being handled by the supplied serializer.
|
2017-11-08 16:23:38 +00:00
|
|
|
|
|
|
|
Serializer Location
|
|
|
|
-------------------
|
2017-12-04 13:10:02 +00:00
|
|
|
Custom serializer classes should follow the rules for including classes found in :doc:`cordapp-build-systems`
|
2017-11-08 16:23:38 +00:00
|
|
|
|
|
|
|
Writing a Custom Serializer
|
2017-11-24 12:04:17 +00:00
|
|
|
---------------------------
|
2017-11-08 16:23:38 +00:00
|
|
|
Serializers must
|
|
|
|
* Inherit from net.corda.core.serialization.SerializationCustomSerializer
|
2017-11-24 12:04:17 +00:00
|
|
|
* Provide a proxy class to transform the object to and from
|
2017-12-04 17:56:27 +00:00
|
|
|
* Implement the ``toProxy`` and ``fromProxy`` methods
|
2017-11-08 16:23:38 +00:00
|
|
|
|
2017-12-04 17:56:27 +00:00
|
|
|
Serializers inheriting from SerializationCustomSerializer have to implement two methods and two types.
|
2017-11-08 16:23:38 +00:00
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
Consider this example class
|
|
|
|
|
2017-12-04 13:10:02 +00:00
|
|
|
|
2017-11-08 16:23:38 +00:00
|
|
|
.. sourcecode:: java
|
2017-12-04 13:10:02 +00:00
|
|
|
|
2017-11-08 16:23:38 +00:00
|
|
|
public final class Example {
|
|
|
|
private final Int a
|
|
|
|
private final Int b
|
|
|
|
|
|
|
|
private Example(Int a, Int b) {
|
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Example of (int[] a) { return Example(a[0], a[1]); }
|
|
|
|
|
|
|
|
public int getA() { return a; }
|
|
|
|
public int getB() { return b; }
|
|
|
|
}
|
|
|
|
|
2017-12-04 13:10:02 +00:00
|
|
|
Without a custom serializer we cannot serialize this class as there is no public constructor that facilitates the
|
2017-12-04 17:56:27 +00:00
|
|
|
initialisation of all of its properties.
|
2017-11-24 12:04:17 +00:00
|
|
|
|
2017-12-04 17:56:27 +00:00
|
|
|
To be serializable by Corda this would require a custom serializer as follows:
|
2017-11-08 16:23:38 +00:00
|
|
|
|
|
|
|
.. sourcecode:: kotlin
|
2017-12-04 13:10:02 +00:00
|
|
|
|
|
|
|
class ExampleSerializer : SerializationCustomSerializer<Example, ExampleSerializer.Proxy> {
|
2017-11-08 16:23:38 +00:00
|
|
|
data class Proxy(val a: Int, val b: Int)
|
|
|
|
|
2017-12-04 13:10:02 +00:00
|
|
|
override fun toProxy(obj: Example) = Proxy(obj.a, obj.b)
|
2017-11-08 16:23:38 +00:00
|
|
|
|
2017-12-04 13:10:02 +00:00
|
|
|
override fun fromProxy(proxy: Proxy) : Example {
|
2017-11-08 16:23:38 +00:00
|
|
|
val constructorArg = IntArray(2);
|
2017-12-04 13:10:02 +00:00
|
|
|
constructorArg[0] = proxy.a
|
2017-11-08 16:23:38 +00:00
|
|
|
constructorArg[1] = proxy.b
|
|
|
|
return Example.create(constructorArg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 12:04:17 +00:00
|
|
|
Whitelisting
|
|
|
|
------------
|
|
|
|
By writing a custom serializer for a class it has the effect of adding that class to the whitelist, meaning such
|
2017-12-04 13:10:02 +00:00
|
|
|
classes don't need explicitly adding to the CorDapp's whitelist.
|
2017-11-08 16:23:38 +00:00
|
|
|
|
|
|
|
|