I am trying to serialize/deserialize the following
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
#JsonSubTypes({
#JsonSubTypes.Type(value = IdBundleCombine.class),
#JsonSubTypes.Type(value = IdBundleDistinct.class) })
public abstract class IdBundle
{
String sharedId;
Long internalId;
//getters
}
public class IdBundleCombine extends IdBundle
{
//setters
}
public class IdBundleDistinct extends IdBundle
{
//setters
}
with the following code
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("foo.json"), someInstanceOfIdBundle);
Which produce the following (without type information as you can see):
{"sharedId":"foobar","internalId":1234}
So i get an error missing property '#type' that is to contain type id when I try to deserialize it.
I tried every combination of parameters for #JsonTypeInfo and #JsonSubTypes I could find without ever succeeding in getting the type information to show in my file. I also tried to play with the #JsonTypeName on the subType without results.
My only guess is that I am doing something wrong with the mapper, but I can't find anything on the subject since most of the people seem to either don't want the type information to show up in the json string, or to have problems with the deserialization process.
I did try using the following annotation and it worked, even with the property tag.
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "#type")
Add 'name' attribute to #Type for sub types and give 'property' attribute of #JsonTypeInfo any value of your choice. Class below
<!-- language: java -->
#JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "meta-type")
#JsonSubTypes({#Type(value = IdBundleCombine.class, name = "bundle-combine"),
#Type(value = IdBundleDistinct.class, name = "bundle-distinct")})
public abstract class IdBundle{
}
will produce following json in case it's IdBundleCombine
{"meta-type": "bundle-combine", "sharedId":"foobar","internalId":1234}
Related
I have old Pojos inside jar. Example;
public class human implements Serializable {
}
public class Man extend human {
}
public class Woman extend human {
}
I have json like {"type":"man",...}
I want to deserialize proper class using type But I cannot use these annotations because Base class generated automatically from xsd.
#JsonTypeInfo(// use = JsonTypeInfo.Id.NAME, // include =
JsonTypeInfo.As.EXISTING_PROPERTY, // property = "queryType", //
visible = true) #JsonSubTypes({ // #Type(value =
ScenarioByCountryQuery.class, name = "scenarioByCountry"), //
#Type(value = ScenarioByMeasureQuery.class, name =
"scenarioByMeasure") })
I don want to desearlize like if type = '' etc.
I want to deserialize dynamically using this type value.
I have class as follows
class XYZ {
private String type;
private TypeSpecific typeSpecific;
public TypeSpecific getTypeSpecific() {
return typeSpecific;
}
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type"
)
#JsonSubTypes({
#JsonSubTypes.Type(value = ATypeSpecific.class, name = "a")
})
public void setTypeSpecific(TypeSpecific typeSpecific) {
this.typeSpecific = typeSpecific;
}
}
Class ATypeSpecific extends TypeSpecific.
I want to deserialize JSON
{"type":"b"}
where typeSpecific will be set as null in object. But I am getting following exception:
com.fasterxml.jackson.databind.JsonMappingException: Missing property 'typeSpecific' for external type id 'type'
How do I deserialize above mentioned JSON into the object?
Dependency versions:
jackson-annotations: 2.7.0,
jackson-core: 2.7.4,
jackson-databind: 2.7.4
I have tried with latest patch, i.e. 2.7.9 and latest version 2.8.6. But it is not working.
Please let me know if this is possible. Thanks in advance.
In my case, having:
#JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, property = "#c")
class CommonReq{};
class AReq extend CommonReq{};
In spring mvc, use #RequestBody AReq to receive json http body, i got this error like missing subtype,
and I add #JsonTypeInfo(use = JsonTypeInfo.Id.NONE) annotation for AReq, finally fixed this problem.
Hope it helps.
You need to set following property to make it run:
// for version 1.x
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// for newer versions
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
Try:
#JsonProperty
private Optional<TypeSpecific> typeSpecific;
You can try this:
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type",
defaultImpl = NoClass.class)
I am trying to serialize an object to json using jackson 2.6.3
I want to include type info in the serialized json. This does not work for members nested within this class.
Here is the test code.
public class Test {
#JsonSubTypes({ #JsonSubTypes.Type(value = ConcreteA.class)})
interface A {
}
#JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
class ConcreteA implements A {
}
#JsonSubTypes({ #JsonSubTypes.Type(value = ConcreteB.class)})
interface B {
}
#JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
class ConcreteB implements B {
A a;
public A getA() {
return a=new ConcreteA();
}
}
#org.junit.Test
public void testSerialisation() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
System.out.println(objectMapper.writeValueAsString(new ConcreteB()));
}
}
The json being converted by jackson is
{"#class":"Test$ConcreteB","a":{}}
Note that it does not include type info for field 'a'. The type info does get included while serializing just A.
{"#class":"Test$ConcreteA"}
UPDATE:
Here is the explanation and solution from jackson dev for the problem
https://github.com/FasterXML/jackson-databind/issues/1015
It seems you were pretty close, all you need to add is a way to tell jackson how to handle your interface type.
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "#class")
#JsonSubTypes({
#JsonSubTypes.Type(value = ConcreteA.class)})
interface A {
}
So adding the #JsonTypeInfo to your interface will add the type information in a property you can configure to be the one you expect, in this case I chose #class.
The Json output I got from my example:
{"#class":"com.company.jackson.Test$ConcreteB","a":{"#type":"Test$ConcreteA"}}
Hope it helps,
José Luis
You also have to set the visible property to true, otherwise it might still not work(my case). I will enhance one of the above answers:
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "#class",
visible = true)
#JsonSubTypes({
#JsonSubTypes.Type(value = ConcreteA.class)})
interface A {
}
I have faced the same issue.
I had imported wrong ObjectMapper class.
Ideally, it should be jackson-databind.jar
com.fasterxml.jackson.databind.ObjectMapper
but I have imported jackson-mapper-asl.jar
org.codehaus.jackson.map.ObjectMapper
I have this response:
{
"id":"decaa828741611e58bcffeff819cdc9f",
"statement":"question statement",
"exercise_type":"QUESTION"
}
Then, based on exercise_type attribute, I want to instantiate different objects instances (subclasses of ExerciseResponseDTO), so I create this mix in:
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "exercise_type")
#JsonSubTypes({
#Type(value = ExerciseChoiceResponseDTO.class, name = "CHOICE"),
#Type(value = ExerciseQuestionResponseDTO.class, name = "QUESTION")})
public abstract class ExerciseMixIn
{}
public abstract class ExerciseResponseDTO {
private String id;
private String statement;
#JsonProperty(value = "exercise_type") private String exerciseType;
// Getters and setters
}
public class ExerciseQuestionResponseDTO
extends ExerciseResponseDTO {}
public class ExerciseChoiceResponseDTO
extends ExerciseResponseDTO {}
So I create my ObjectMapper as follows
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(ExerciseResponseDTO.class, ExerciseMixIn.class);
My test:
ExerciseResponseDTO exercise = mapper.readValue(serviceResponse, ExerciseResponseDTO.class)
Assert.assertTrue(exercise.getClass() == ExerciseQuestionResponseDTO.class); // OK
Assert.assertEquals("decaa828741611e58bcffeff819cdc9f" exercise.getId()); // OK
Assert.assertEquals("question statement", exercise.getStatement()); // OK
Assert.assertEquals("QUESTION", exercise.getExerciseType()); // FAIL. Expected: "QUESTION", actual: null
The problem is that, for some reason, the exercise_type attribute being used as property on #JsonTypeInfo is being mapped as null.
Any idea how i can solve this?
Finally, I've found the solution in the API Doc
Note on visibility of type identifier: by default, deserialization
(use during reading of JSON) of type identifier is completely handled
by Jackson, and is not passed to deserializers. However, if so
desired, it is possible to define property visible = true in which
case property will be passed as-is to deserializers (and set via
setter or field) on deserialization.
So the solution was simply adding the 'visible' attribute as follows
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "exercise_type",
visible = true)
#JsonSubTypes({
#Type(value = ExerciseChoiceResponseDTO.class, name = "CHOICE"),
#Type(value = ExerciseQuestionResponseDTO.class, name = "QUESTION")})
public abstract class ExerciseMixIn
{}
As per #jscherman answer by setting, 'visible' true in JsonTypeInfo will help in accessing exercise_type as a field.
If you use the same class to serialize also then resulting JSON will have exercise_type appear twice. So it's better to also update include to JsonTypeInfo.As.EXISTING_PROPERTY
And it's also worth looking at all other options for include.
I'm familiar with the normal polymorphic deserialization stuff where you deserialize an object based on the string value of a certain field. For instance:
#JsonSubTypes(
{
#JsonSubTypes.Type(value = LionCage.class, name = "LION"),
#JsonSubTypes.Type(value = TigerCage.class, name = "TIGER"),
}
)
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
Is there any way to do basically the same thing if the "type" field of the incoming object is an integer instead of a string? So in the example above, "LION" and "TIGER" would be 1 and 2. For whatever reason, I haven't been able to figure this out.
Also, how should I have been able to figure this out? Seems like it should be something obvious.
Jackson automatically converts string to numbers and vice versa. Just use string values for numbers. Like "1" for 1 value. Just try this yourself (jackson version is 2.5.1):
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HelloWorldJacksonNumber {
public static class A extends Base {
String a;
}
public static class B extends Base {
String b;
}
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
#JsonSubTypes({
#JsonSubTypes.Type(value = A.class, name = "1"),
#JsonSubTypes.Type(value = B.class, name = "2")})
public static class Base {
int type;
}
public static void main (String[] args) throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.version());
System.out.println(objectMapper.writeValueAsString(new A()));
System.out.println(objectMapper.writeValueAsString(new B()));
System.out.println(objectMapper.readValue("{\"type\":\"1\"}", Base.class).getClass());
System.out.println(objectMapper.readValue("{\"type\":\"2\"}", Base.class).getClass());
}
}
Output is:
2.5.1
{"type":"1"}
{"type":"2"}
class HelloWorldJacksonNumber$A
class HelloWorldJacksonNumber$B
No, that's not an option via the annotations. The TypeIdResolver interface takes and returns strings. You could do it with a custom parser/serializer using Jackson's stream API, but that seems like a lot of work to switch it to numeric field. I would only do it if someone else's system required it. If I owned the whole thing, I would just use the setup you posted in the question.