Serialization and deserialization

The Cruzr system will process a large number of inter-process communications during system operation, followed by frequent data serialization and deserialization. In order to simplify data serialization for developers, the Cruzr system provides Marshaller interface to process data serialization and deserialization.

Abstract interface

  • Marshaller

Serializing raw data into byte [] is completed by the marshall function, and the operation of deserializing byte [] into raw data is completed by the unmarshall function, as follows:

  public interface Marshaller {
      <T> T unmarshall(byte[] value, Type type) throws IOException;

      byte[] marshall(Object value) throws IOException;
  }
  • MarshallerFactory
  public interface MarshallerFactory {
      Marshaller create();

      String contentType();
  }

Note: MarshallerFactory is used to create the Marshaller of specified types. The Cruzr system provides the commonly used Marshaller implementation by default. Developers can also create customized Marshaller as required.

Data type

The Cruzr system provides Marshaller implementations of three common data types:

  • PARCELABLE

ParcelableMarshaller is used to handle serialization and deserialization operations of the Android Parcelable data type. It is created using the ParcelableMarshallerFactory.

  public void testMarshaller() throws IOException {
      Marshaller marshaller = new ParcelableMarshallerFactory().create();
      Bundle parcelable = createYourValue();

      byte[] bytes = marshaller.marshall(parcelable);
      parcelable = marshaller.unmarshall(bytes, Bundle.class);
  }
  • PROTOBUF

ProtobufMarshaller is used to handle the serialization and deserialization of the Protobufdata type. It is created using the ProtobufMarshallerFactory.

  public void testMarshaller() throws IOException {
      Marshaller marshaller = new ProtobufMarshallerFactory().create();
      ProtoClass protobuf = createYourValue();

      byte[] bytes = marshaller.marshall(protobuf);
      protobuf = marshaller.unmarshall(bytes, ProtoClass.class);
  }
  • JSON

GsonMarshaller is used to handle the serialization and deserialization of common types. It is created using the GsonMarshallerFactory.

  public void testMarshaller() throws IOException {
      Marshaller marshaller = new GsonMarshallerFactory().create();
      NormalClass normal = createYourValue();

      byte[] bytes = marshaller.marshall(normal);
      normal = marshaller.unmarshall(bytes, NormalClass.class);
  }

Note: The original data type xxx.class of the above deserialization operation must be consistent with the original type of the serialized data.

The definitions of the above types of constant parameters are located at com.ubtrobot.marshall.ContentTypes

How to use

The Cruzr system comes with the serialization and deserialization implementations of the PARCELABLE type. Other types or user-defined types can be added to the Cruzr system in the following ways:

// add JSON marshaller
Master.get().addMarshallerFactory(new GsonMarshallerFactory());

// add custom marshaller
Master.get().addMarshallerFactory(new CustomMarshallerFactory());
  • Using serialization

The use of serialization operations in the Cruzr system focuses on sending instructions and publishing events. The following is an example of publishing an event:

    // publish interface
    void publish(String topic, Object parameterObj, String contentType);

    //demo code
    robotContext.publish("event.action.power.BATTERY_CHANGE", batteryProperties,
                           ContentTypes.PARCELABLE);

Note: The size of the serialization parameters cannot exceed 1 MB.

The serialization operation of publishing events only needs to fill in the original data of serialization parameterObj and specify the serialization type parameter contentType. The serialization operation of sending instructions is the same.

  • Using deserialization

The deserialization in the Cruzr system has been completed for developers. As long as the Marshaller of the corresponding type is added to the Cruzr system, the system will automatically complete it when deserialization is needed.