Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Polymorphism

I have a List of javascript objects on my client side, which are a list of "events" that a user has executed. When the user is ready, I want to send this to the server. The order of events is important, so preserving the list order is necessary.

What I would like to do is to have a JSON library (don't mind which one) to bind the JSON to some Event objects in my Java code, where Event is an abstract class, and I have 3 concrete classes that all extend Event (lets say EventA, EventB and EventC).

Ideal scenario would be something like

List<Event> events = jsonlibrary.deserialise(jsonString);

which may contain a list of items such as

[eventA, eventC, eventA, eventA, eventB]

Is this possible, or do I have to inspect the JSON tree manually, and deserialise the individual elements of the json array?

like image 749
Codemwnci Avatar asked Aug 29 '12 15:08

Codemwnci


People also ask

What is polymorphic JSON?

You can use this schema when defining XML Type hierarchies by using only the base XML Types. The XML schema defines XML Types that inherit from each other. In the JSON, an object carries no additional information about the type.

Is polymorphic deserialization possible in System text JSON?

Text. Json doesn't support the serialization of polymorphic type hierarchies. For example, if a property's type is an interface or an abstract class, only the properties defined on the interface or abstract class are serialized, even if the runtime type has additional properties.

What is polymorphic deserialization?

A polymorphic deserialization allows a JSON payload to be deserialized into one of the known gadget classes that are documented in SubTypeValidator. java in jackson-databind in GitHub. The deserialized object is assigned to a generic base class in your object model, such as java. lang. Object or java.

What does Jsonconvert Deserializeobject do?

Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.


2 Answers

JSON objects are just key/value pairs and contain no type information. That means identifying the type of a JSON object automatically isn't possible. You have to implement some logic on the server-side to find out what kind of event you are dealing with.

I would suggest to use a factory method which takes a json string, parses it to find out what kind of Event it is, builds an Event object of the correct subclass and returns it.

like image 132
Philipp Avatar answered Sep 21 '22 02:09

Philipp


You could use Genson library http://code.google.com/p/genson/. It can deserialize to concrete types if the json was produced using Genson. Otherwise you only need to add something like [{"@class":"my.java.class", "the rest of the properties"}...]

// an example
abstract class Event {
 String id;
}

class Click extends Event {
 double x, y;
}

// you can define aliases instead of plain class name with package (its a bit nicer and more secure)
Genson genson = new Genson.Builder().setWithClassMetadata(true).addAlias("click",
            Click.class).create();
String json = "[{\"@class\":\"click\", \"id\":\"here\", \"x\":1,\"y\":2}]";

// deserialize to an unknown type with a cast warning
List<Event> events =  genson.deserialize(json, List.class);

// or better define to which generic type
GenericType<List<Event>> eventListType = new GenericType<List<Event>>() {};
events = genson.deserialize(json, eventListType);

EDIT here is the wiki example http://code.google.com/p/genson/wiki/GettingStarted#Interface/Abstract_classes_support

like image 39
eugen Avatar answered Sep 20 '22 02:09

eugen