Class GsonSerializer

java.lang.Object
net.sodacan.core.serialize.gson.GsonSerializer
All Implemented Interfaces:
Serializer

public class GsonSerializer extends Object implements Serializer
Serialize and deserialize Actors and Messages to Json using Google's Gson. We instantiate a pool of Gson instances during configuration so that the methods here can be called safely from any thread/actor.
  • Field Details

  • Constructor Details

  • Method Details

    • serializeField

      public byte[] serializeField(Object fieldValue)
      Serialize a field rather than a whole actor
      Specified by:
      serializeField in interface Serializer
      Parameters:
      fieldValue -
      Returns:
      Byte array
    • deserializeField

      public Object deserializeField(byte[] source, Class<?> fieldClass)
      Description copied from interface: Serializer
      Deserialize the @Save fields of an Actor into an existing actor.
      Specified by:
      deserializeField in interface Serializer
      Parameters:
      source -
      fieldClass - Class of the field that we're returning
      Returns:
      An object containing the field value
    • serialize

      public byte[] serialize(Actor actor)
      We are asked to serialize the fields of an actor, rather than the Actor as a whole. We serialize it as a map which is, in turn, taken care of in the MapAdapter
      Specified by:
      serialize in interface Serializer
      Parameters:
      actor -
      Returns:
    • deserialize

      public void deserialize(byte[] source, Actor actor)
      To deserialize an Actor requires two steps. First, deserialize a Map of the Actor's field values. Then populate the Actor's @Save fields from the deserialized Map.
      Specified by:
      deserialize in interface Serializer
      Parameters:
      source -
      actor -
    • serialize

      public byte[] serialize(Message message)
      Description copied from interface: Serializer
      Serialize a Message to a byte array
      Specified by:
      serialize in interface Serializer
      Parameters:
      message -
      Returns:
    • deserialize

      public Message deserialize(byte[] source)
      Description copied from interface: Serializer
      Deserialize a byte array into a new Message.
      Specified by:
      deserialize in interface Serializer
      Parameters:
      source -
      Returns:
      A newly constructed Message