Chapter 16. Serialization

16.1. Serialization and Deserialization in SwitchYard

Serialization is a process of converting an object into a binary format, which can be persisted in a database or transmitted over a network. Deserialization is a process of creating an object from binary format. In SwitchYard, the process of serialization is a concern in case of clustering and execution from a client invoker. In such cases, SwitchYard uses objects that represent the content of a SwitchYard Message or objects that are placed within a SwitchYard Exchange's Context, for serialization and deserialization.

16.2. Custom Objects

Custom (application-defined) objects that are stored as the content of the message or property of the exchange context, are not required to implement This is because SwitchYard does not use the JDK's default object serialization mechanism. It traverses the object graph, extracting the properties along the way, and stores the values in its own graph representation, which by default is serialized to/from the JSON format. For this to work, custom objects must follow one of the following two rules:
  • Adhere to the JavaBeans specification. This means a public, no arg constructor, and public getter/setter pairs for every property you want serialized. If you follow this rule, your custom objects do not require any compilation dependencies on SwitchYard.
  • Use SwitchYard annotations to define your serialization strategy.

16.3. SwitchYard Annotations for Serialization

The org.switchyard.serial.graph package provides the following annotations, enums, interface and class:
Here you can define the serialization strategy, including access type, coverage type, and factory for your class. All these are optional.
  • access=AccessType: BEAN (default) for getter/setter property access, FIELD for member variable access.
  • coverage=CoverageType: INCLUSIVE (default) for serializing all properties, EXCLUSIVE for ignoring all properties.
  • factory=Factory: Interface for how the class gets instantiated.
    • DefaultFactory: Creates an instance of the class using the default constructor.
You can place this on individual getter methods or fields to override CoverageType.EXCLUSIVE.
You can place this on individual getter methods or fields to override CoverageType.INCLUSIVE.

16.4. SwitchYard Serialization API Usage

You can implement Serialization using Serializers from the SerializerFactory. Although it is not recommended to use SwitchYard's serialization API directly, however, if you need to use it, here is an example of Serializer.create invocation:
// See SerializerFactory for overloaded "create" methods.
Serializer ser = SerializerFactory.create(FormatType.JSON, CompressionType.GZIP, true);
// to and from a byte array
Foo fooBefore = new Foo();
byte[] bytes = ser.serialize(fooBefore, Foo.class);
Foo fooAfter = ser.deserialize(bytes, Foo.class);
// to and from output/input streams
Bar barBefore = new Bar();
OutputStream out = ...
ser.serialize(barBefore, Bar.class, out);
InputStream in = ...
Bar barAfter = ser.deserialize(in, Bar.class);
Out of the box, the available FormatTypes are SER_OBJECT, XML_BEAN and JSON, and the available CompressionTypes are GZIP, ZIP, or null (for no compression).