Вопрос

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.

Это было полезно?

Решение

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

Другие советы

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top