On August 26, 2013 at 10:08:17 PM, Jones, Nathan (nathan...@airnz.co.nz) wrote:
I am not too concerned about the behaviour with arrays because in reality I am only using collections. However, now I am seeing the same differences between JSON serialised in my unit tests and JSON serialised by JAX-RS. Here is a complete working example (assuming you have a JAX-RS runtime with com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider registed):
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeId;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.ObjectMapper;
@Path("animals")
public class AnimalResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Animal> animals() {
return createAnimals();
}
public static List<Animal> createAnimals() {
Dog dog = new Dog();
dog.name = "Scooby";
Elephant elephant = new Elephant();
elephant.name = "Dumbo";
GenericAnimal camel = new GenericAnimal();
camel.name = "Alice";
camel.setType("camel");
return Arrays.asList(dog, elephant, camel);
}
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(createAnimals());
System.out.println(json);
}
@JsonTypeInfo(use = Id.NAME, property = "type", visible = true, defaultImpl = GenericAnimal.class)
@JsonSubTypes({ @Type(Elephant.class), @Type(Dog.class) })
public static abstract class Animal
{
@JsonTypeId
public String getType()
{
JsonTypeName annotation = this.getClass().getAnnotation(JsonTypeName.class);
return annotation.value();
}
public String name;
}
@JsonTypeName("elephant")
public static class Elephant
extends Animal
{
}
@JsonTypeName("dog")
public static class Dog
extends Animal
{
}
public static class GenericAnimal
extends Animal
{
private String type;
@Override
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
}
}
In the above example the output of the main method is:
[{"name":"Scooby"},{"name":"Dumbo"},{"name":"Alice"}]
The body of the HTTP response for requesting "GET /animals" is:
[{"type":"dog","name":"Scooby"},{"type":"elephant","name":"Dumbo"},{"type":"camel","name":"Alice"}]
I can't figure out what is causing these differences in serialisation. Is the JacksonJsonProvider doing something to reconfigure the object mapper?
- Nathan
________________________________________
From: Tatu Saloranta [mailto:tsalo...@gmail.com]
Sent: Tuesday, 27 August 2013 9:13 a.m.
To: us...@jackson.codehaus.org
Subject: Re: [jackson-user] Binding polymorphic type discriminator as deserialized property
On Sun, Aug 25, 2013 at 10:35 PM, Jones, Nathan <Nathan...@airnz.co.nz> wrote:
I am seeing some strange differences with the handling of the type info between polymorphic collections and polymorphic arrays. When the @JsonTypeId annotation is present on the getType() method then an array of Animal will be serialised with the "type" property as expected but a List of animal will be serialized with no "type" property and then cannot be deserialized. When the @JsonTypeId annotation is missing on the getType() method then a List of animal will be serialized with the "type" property as expected but an array of Animal will be serialized with the "type" property included twice; the values are identical for the "known" sub types but for the defaultImpl one of the values is the simple name of the class.
Is this a bug?
Without sample code I can't say, but keep in mind that JVM's handling of arrays and Collections differs radically: arrays are about the only things that retain full non-type-erased information. Collections (and are other generic objects) lose their type information more easily.
This explains many observed differences.
Are than this, handling by Jackson should be similar, once type information has been resolved.
-+ Tatu +-
Good planets are hard to find - please think of the environment before you print this email.
____________________________________________________________________
CAUTION - This message may contain privileged and confidential
information intended only for the use of the addressee named above.
If you are not the intended recipient of this message you are hereby
notified that any use, dissemination, distribution or reproduction
of this message is prohibited. If you have received this message in
error please notify Air New Zealand immediately. Any views expressed
in this message are those of the individual sender and may not
necessarily reflect the views of Air New Zealand.
_____________________________________________________________________
For more information on the Air New Zealand Group, visit us online
at http://www.airnewzealand.com
_____________________________________________________________________
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.