0

Here is my use case:

I have these classes on the serverside.

class Individual {
  protected String uri;
  protected int id;
}

class Person extends Individual {
  // Person properties like names, address etc
  String type = "Person";
}

class Role extends Individual {
  // Role properties like name, title etc
  String type = "Role";
}

class Organization extends Individual {
  // Org properties like name name, address etc
  String type = "Organization";
}

I have a class say Action like below.

class Action {
  String performedBy; // This can be any Individual
}

I have a controller that accepts an Action. I want the Individual to be correctly assigned depending on what the client sends. How should my Action & other classes be defined to achieve this?

if I send the following, I want performedBy to be a Person.

{
 "id": 10,
 "uri":"uri_blah",
 "lastname": "last_name",
 "type":"Person"
}
approxiblue
  • 6,982
  • 16
  • 51
  • 59
rtnyc
  • 145
  • 1
  • 2
  • 10
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – MikaelF Feb 08 '17 at 04:33

1 Answers1

4

There is @JsonType annotation which allows to serialize the type into property:

@JsonTypeInfo(
   use = JsonTypeInfo.Id.NAME, 
   include = JsonTypeInfo.As.PROPERTY, 
   property = "type")
@JsonSubTypes({ 
   @Type(value = Person.class, name = "Person"), 
   @Type(value = Role.class, name = "Role") 
   // ...
 })
 public abstract class Individual {
    // ...
 }
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43