You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to serialize an object into json so that I can send the type info in an existing field of the class. I am using v2.6.2 and also tried using 2.8.x, but the problem still persists.
B b = new B();
b.setSomething("Test");
String o = JSONObjectMapper.getObjectMapper().writeValueAsString(b);
System.out.println(o);
String s = "{ \"type\":\"B\", \"something\":\"else\" }";
A a = JSONObjectMapper.getObjectMapper().readValue(s, A.class);
System.out.println(a instanceof B);
Class A:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = B.class, name = "B")
})
public abstract class A {
public String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Class B:
public class B extends A {
private String something;
public String getSomething() {
return something;
}
public void setSomething(String something) {
this.something = something;
}
}
The output of the above program is:
{"something":"Test","type":null}
true
The type field is not being set when serializing but it's being recognized while deserializing the Object.
If I change EXISTING_PROPERTY to PROPERTY, it's working as expected and adding "type" twice, once with the info and once without.
The text was updated successfully, but these errors were encountered:
B b = new B();
b.setSomething("Test");
String o = JSONObjectMapper.getObjectMapper().writeValueAsString(b);
System.out.println(o);
String s = "{ \"type\":\"B\", \"something\":\"else\" }";
A a = JSONObjectMapper.getObjectMapper().readValue(s, A.class);
System.out.println(a instanceof B);
Where do you set property "type" for b? Nowhere, so it is null. Existing property used, so providing valid value is your responsibility.
The type field is not being set when serializing
Do you mean serializer have to set it for your bean? Change it's state? The one and only responsibility of serializer is extract your data and output it to specified format. That it what it does.
Where do you set property "type" for b? Nowhere, so it is null. Existing property used, so providing valid value is your responsibility.
Do you mean serializer have to set it for your bean? Change it's state? The one and only responsibility of serializer is extract your data and output it to specified format. That it what it does.
Hi,
I am trying to serialize an object into json so that I can send the type info in an existing field of the class. I am using v2.6.2 and also tried using 2.8.x, but the problem still persists.
Class A:
Class B:
The output of the above program is:
The type field is not being set when serializing but it's being recognized while deserializing the Object.
If I change EXISTING_PROPERTY to PROPERTY, it's working as expected and adding "type" twice, once with the info and once without.
The text was updated successfully, but these errors were encountered: