4

I like to serialize my java class object using Messagepack. My class structure is such that

public class A
 {
   private InnerClass obj;
   //Some other variables with getters and setters.
   // There will be a getter and setter for obj(InnerClass) aswell.
 }
 class InnerClass
 {
  //Some variables with getters and setters.
 }
 class InnerDerivedClass extends InnerClass
 {
  //some variables with getters and setters.
 }

What I like to do is to create the object for InnerDerivedClass and assign some values to it. Then I would add the object to the Class A as base class object.Now when I serialize and deserialize the Class A object with Messagepack I am not able to get the object as InnerDerivedClass object rather its comes as InnerClass obj. How can I achieve this using MessagePack. I am not using any annotations rather I register each class in the messagepack.

Kazuki Ohta
  • 1,441
  • 17
  • 17
Muzy
  • 505
  • 2
  • 6
  • 16

2 Answers2

0

I looked at documentation and examples of MessagePack and I couldn`t find anything about nested serialization, only simple types and Maps.

I recommend you to use GSON for your problem 1) Java - Gson parsing nested within nested 2) Deserializing arbitrary object json arrays in inner classes with Gson or nested inner class

You can try use JSON-SIMPLE. It will reprezent you classes as nested Maps https://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities

Community
  • 1
  • 1
0

For nested structures I'd recommend FlexJson; you can avoid all the boiler plate code associated with nested structures:

A obj1 = new A()
String json = JSONSerializer().deepSerialize(obj1);

//get back
A obj2 = JSONDeserializer<A>().deserialize(json);
raffian
  • 31,267
  • 26
  • 103
  • 174