Skip to content

Latest commit

 

History

History
26 lines (14 loc) · 3.37 KB

File metadata and controls

26 lines (14 loc) · 3.37 KB

How to serialize objects in jSpace

Components of a distributed jSpace application interact, by default, via JSON messages. This means that to store and retrieve an object from a remote space this must be serialized and deserialized in JSON.

To implement these features jSpace uses the Gson library. Gson is a Java library that converts Java Objects into their JSON representation and viceversa. One of the main features of Gson is that it can work with arbitrary Java objects including pre-existing objects that you do not have source code of.

Gson natively supports serialization of standard Java Objects, of unmodifiable objects, and of POJOs. In fact, the large part of Java Objects can be serialized/deserialized with Gson without any customization.

However, in some cases, the default Gson approach is not enough and ad hoc mechanisms must be developed. This holds when complex data structures are used.

Gson, and jSpace, uses a factory approach to simplify the integration of customized serializer/deserializer. These are classes that must implement the interfaces JsonSerializer and JsonDeserializer (see Gson documentation for further details). Both these interfaces are parametrized with respect to the type T of objects to serialize/deserialize.

When you have developed the customized serializer you have just to register your classes in jSpace. This can be done via the class org.jspace.io.json.jSonUtils. This class follows a singleton pattern. The reference to the (single) instance of the class can be obtained by using the static method jSonUtils.getInstance(). This instance can be used to register the appropriate JSON serializer/deserializer for your class:

jSonUtils utils = jSonUtils.getInstance();
utils.register( classuri , yourclass.class , YourSerializer , YourDeserializer ); 

Above classuri is a uri that is used to identify the datatype associated with your class yourclass. Note that this value allow the exchange of data among components developed with different langauges (other pSpaces languagews implement a similar approach). This registration code should be executed at the initialization phase of your app.

An example of serializer/deserializer can be found in the jSpace code. Indeed, Tuple and Template. In jSpace customized serializer and deserializer have been developed to simplify the exchanged ot tuples/templates: TupleSerializer, TupleDeserializer, TemplateSerializer, TemplateSerializer.