Working with Endpoint SmartObjects: Serialization and Deserialization

The Generic or "EndPoint" Service Brokers make extensive use of .NET Serialization and Deserialization to deal with complex types. While this topic explains how these functions are used by the generic service brokers, understanding Serialization and Deserialization requires some knowledge of how .NET stores and transfers data. (For more information on Serialization in .NET, see https://msdn.microsoft.com/en-us/library/7ay27kt9(v=vs.110).aspx.). In practice, you may find yourself combining serialization and deserialization methods in advanced SmartObject methods to pass values between methods.

The main methods that you use with the endpoint brokers are serialization and deserialization. Simply put, these methods are used to send and store complex data. To serialize the results of a service call means to represent it in a state that can be transferred and/or stored, such as in a database. To deserialize that data is to turn it back into a state that can be consumed by code or an application. These methods (while based on the .NET Framework for the purposes of the K2 Endpoint Service Brokers), represent a device-independent, cross-platform method of representing complex, relational data. For more information, see http://en.wikipedia.org/wiki/Serialization.

For entities with complex types or nested objects, the complex type or nested object will be returned as serialized items. The serialized item will need to be deserialized before being displayed and in a case where data is being captured, the item should be serialized then passed to the SmartObject as a serialized item. (It is also possible to chain SmartObject methods to execute more than one method. This is useful for deserializing nested or complex typed items in one method execution.)

Serialization converts an object into a form that can be persisted or transported. Most commonly, this means converting an object to a string format.
Deserialization is the process of taking a string and converting it back into an object so the underlying system can work with it.

Serializing an object to string

The opposite of serialization is deserialization, which converts a string to an object.

K2 will create the serialization and deserialization Service Object methods for you, but you may need to “chain” them in the correct way for the underlying provider to understand. The key to using the brokers is to understand what you are trying to do. Ask yourself the following questions:

  1. Am I submitting data?
  2. Am I retrieving data?
  3. Do I have the data I need?
  4. Is the data in the right structure?
  5. Does the main structure have any nested types?
  6. If it has nested types, do I need to serialize those before submitting? Or deserialize them after retrieving?

It is important to know what your multiple levels of serialization are, and then keep deserializing things until you get to simple types. The exact sequence of number of calls you need to make will depend on the service.

Use the Properties, Methods, and the ability to Chain Methods to process the data through the Serialization/Deserialization steps.