Chapter 16. Serialization
16.1. Serialization and Deserialization in SwitchYard
16.2. Custom Objects
java.io.Serializable. 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
- 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
// 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).