This repository was archived by the owner on Mar 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Inheritance
thamtech edited this page Jul 19, 2013
·
2 revisions
Inheritance of POJOs is supported out of the box. Suppose you have the following class hirarchy:
abstract class Animal
{
String name;
int legs;
double intelligence;
}
abstract class Mammal extends Animal
{
Gender gender;
}
public class Cat extends Mammal
{
public interface CatJsonReader extends JsonReader<Cat> {}
public static final CatJsonReader JSON = GWT.create(CatJsonReader.class);
String color;
}
and the following JSON data
{ "name": "Snowball", "legs": 4, "intelligence": 0.4, "gender": "female", "color": "dark grey" }
You can read Snowball using
String json = ...;
Cat snowball = Cat.JSON.read(json);
When (de)serializing POJOs Piriti automatically walks up the class hirarchy (upto but not including java.lang.Object). You can change that by using the @!MapUpTo annotation:
@MapUpTo(Mammal.class)
public class Cat extends Mammal
{
public interface CatJsonReader extends JsonReader<Cat> {}
public static final CatJsonReader JSON = GWT.create(CatJsonReader.class);
String color;
}
In this setup the fields
- name
- legs and
- intelligence won't be processed.