Class MultiSerialiser

  • All Implemented Interfaces:
    Serializable, Serialiser<Object,​byte[]>, ToBytesSerialiser<Object>

    public class MultiSerialiser
    extends Object
    implements ToBytesSerialiser<Object>
    This class is used to serialise and deserialise multiple object types. This overcomes the limitation of a graph schema requiring all vertex types to use the same serialiser.

    The serialiser used is stored at the first byte of the serialised byte[] The maximum number of different serialiser keys that can be used is limited to 256 (the size of a byte).

     byte array [42,*,*,*,*,*]
                 ^
                 ^
                 serialiser byte key
     
    When multiple serialisers are used that operate on the same value class then the last serialiser in the list will be used for serialisation; this happens regardless of the key's natural-ordering. In the example Integers will be serialised with no.8.
    For deserialising the correct serialiser is always chosen based on the byte key at the start of the byte[].

    In the below example, the MultiSerialiser has 3 Integer serialisers. This has backwards compatibility to read and deserialise each byte array regardless of the keyed serialiser used at the time of writing, as long as its on the classpath. However when re-serialising the Integer object, only the last serialiser will be used (key no.8, CompactRawIntegerSerialiser)
    This allows the MultiSerialiser to be updated with improvements and maintain backwards compatibility. This also allows serialisation to be updated before a Serialiser is deprecated and removed.

         Json
         {
           "serialisers" : [ {
             "key" : 0,
             "serialiser" : {
               "class" : "uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser",
               "charset" : "UTF-8"
             },
             "valueClass" : "java.lang.String"
           }, {
             "key" : 7,
             "serialiser" : {
               "class" : "old.deprecated.IntegerSerialiser",
               "charset" : "ISO-8859-1"
             },
             "valueClass" : "java.lang.Integer"
           }, {
             "key" : 24,
             "serialiser" : {
               "class" : "uk.gov.gchq.gaffer.serialisation.implementation.ordered.OrderedIntegerSerialiser"
             },
             "valueClass" : "java.lang.Integer"
           }, {
             "key" : 8,
             "serialiser" : {
               "class" : "uk.gov.gchq.gaffer.serialisation.implementation.raw.CompactRawIntegerSerialiser"
             },
             "valueClass" : "java.lang.Integer"
           } ]
         }
         
    See Also:
    Serialized Form