Skip to content

This example shows how to map classes with inheritance/subclass relationships with Jackson and Morphia

Notifications You must be signed in to change notification settings

denniskuczynski/morphia_jackson_polymorphism_example

Repository files navigation

Class Inheritance Mappings in Jackson (JSON) and Morphia (MongoDB)

Jackson is a library for mapping Java classes to JSON.

Morphia is a library for mapping Java classes to MongoDB.

This example shows how to map classes with inheritance/subclass relationships with both frameworks.

It maps an Animal class with two subtypes (Lion and Tiger), each containing a list of Habitats which also has two subtypes (Grassland and Woodland).

Mapping with Jackson

Jackson leverages the JsonTypeInfo annotation to define the discriminators for polymorphic types. http://wiki.fasterxml.com/JacksonPolymorphicDeserialization

See:

Example Mapping from Jackson:
---------------------------------
Lion
---------------------------------
{"_t":"lion","maneColor":"tan","habitat":[{"_t":"woodland","country":"Africa"},{"_t":"grassland","country":"Africa"}]}
Tiger
---------------------------------
{"_t":"tiger","stripeCount":42,"habitat":[{"_t":"grassland","country":"India"},{"_t":"grassland","country":"Asia"}]}

Mapping with Morphia

Morphia doesn't need any special annotations, but the Entity annotation must have noClassnameStored=false because the classname is used for deserialization.

Example Mapping from Morphia:
---------------------------------
Lion
---------------------------------
{ "className" : "morphia_jackson_polymorphism.morphia_only.animals.Lion" , "maneColor" : "tan" , "_t" : "lion" , "habitat" : [ { "className" : "morphia_jackson_polymorphism.morphia_only.habitats.Woodland" , "_t" : "woodland" , "country" : "Africa"} , { "className" : "morphia_jackson_polymorphism.morphia_only.habitats.Grassland" , "_t" : "grassland" , "country" : "Africa"}]}
Tiger
---------------------------------
{ "className" : "morphia_jackson_polymorphism.morphia_only.animals.Tiger" , "stripeCount" : 42 , "_t" : "tiger" , "habitat" : [ { "className" : "morphia_jackson_polymorphism.morphia_only.habitats.Grassland" , "_t" : "grassland" , "country" : "India"} , { "className" : "morphia_jackson_polymorphism.morphia_only.habitats.Grassland" , "_t" : "grassland" , "country" : "Asia"}]}
---------------------------------
Lion (from DB)
---------------------------------
morphia_jackson_polymorphism.morphia_only.animals.Lion@59b80684
Tiger (from DB)
---------------------------------
morphia_jackson_polymorphism.morphia_only.animals.Tiger@40b3f220

Customizing Morphia to use Jackson JsonTypeInfo Annotations

We can leverage the style of Jackson of serialization with a type field discriminator by configuring Morphia to use a custom object factory that inspects for Jackson's JsonTypeInfo annotation which defines discriminators. Now classnames are not required in the serialized objects, which makes the records smaller and lets us rename classes without migrations.

Note that Embedded classes must also have an Entity annotation with noClassnameStored=false to prevent their classname from being serialized.

Morphia morphia = new Morphia();
Mapper morphiaMapper = morphia.getMapper();
morphiaMapper.getOptions().objectFactory = new CustomMorphiaObjectFactory();
Example Mapping from Morphia with Jackson:
---------------------------------
Lion
---------------------------------
{ "maneColor" : "tan" , "_t" : "lion" , "habitat" : [ { "_t" : "woodland" , "country" : "Africa"} , { "_t" : "grassland" , "country" : "Africa"}]}
Tiger
---------------------------------
{ "stripeCount" : 42 , "_t" : "tiger" , "habitat" : [ { "_t" : "grassland" , "country" : "India"} , { "_t" : "grassland" , "country" : "Asia"}]}
---------------------------------
Lion (from DB)
---------------------------------
morphia_jackson_polymorphism.morphia_with_jackson.animals.Lion@3bf77f86
Tiger (from DB)
---------------------------------
morphia_jackson_polymorphism.morphia_with_jackson.animals.Tiger@75d5552b

Running the Example

mvn compile mvn exec:java -Dexec.mainClass="morphia_jackson_polymorphism.Example"

About

This example shows how to map classes with inheritance/subclass relationships with Jackson and Morphia

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages