I'm trying to deserialize this json data into list of objects:
[{
"a": {
"commonField": 1,
"aField": "AAA"
}
}, {
"b": {
"commonField": 2,
"bField": "BBB"
}
}]
Each object may be one of several types having both common and unique fields. Information about exact shape of an object is stored in json as key in the wrapper object.
I created corresponding classes for every known shape (set of unique fields) extending class containing all common fields. Also, I added Jackson annotations to the classes to enable polymorphic deserialization. Simplified, resulting classes look like this:
#JsonTypeInfo(use = Id.NAME, include = As.WRAPPER_OBJECT, property = "type")
#JsonSubTypes({
#JsonSubTypes.Type(KeyBasedSubTypeA.class),
#JsonSubTypes.Type(KeyBasedSubTypeB.class)
})
public abstract class KeyBasedSuperType {
public String type;
public int commonField;
}
#JsonTypeName("a")
public class KeyBasedSubTypeA extends KeyBasedSuperType {
public String aField;
}
#JsonTypeName("b")
public class KeyBasedSubTypeB extends KeyBasedSuperType {
public String bField;
}
With this setup Jackson works almost perfectly. It is able to choose correct subtype during deserialization and populate all the fields including common and unique. However, the type field is not updated by Jackson, the key value used for selecting subtype is not stored anywhere. In other words, the data is deserialized into following structure:
[KeyBasedSubTypeA { type=null; commonField=1; aField=AAA },
KeyBasedSubTypeB { type=null; commonField=2; bField=BBB }]
Note type field having null value. So, the question is - How can I make Jackson to store wrapper's key used for selecting subtype somewhere in resulting object?
Here is my JUnit test for the process
public class PolymorphicTest {
private static ObjectMapper mapper;
#BeforeClass
public static void init() {
mapper = new ObjectMapper();
}
#Test
public void testKeyDenominator() throws IOException {
TypeReference<List<KeyBasedSuperType>> dataShape =
new TypeReference<List<KeyBasedSuperType>>() {};
List<KeyBasedSuperType> result = mapper.readValue(
PolymorphicTest.class.getResourceAsStream("polymorphic-key.json"), dataShape);
assertEquals(2, result.size());
assertEquals(KeyBasedSubTypeA.class, result.get(0).getClass());
assertEquals(KeyBasedSubTypeB.class, result.get(1).getClass());
assertEquals(1, result.get(0).commonField);
assertEquals(2, result.get(1).commonField);
assertEquals("a", result.get(0).type); // <---- this line fails
assertEquals("b", result.get(1).type); // <---- this line fails
assertEquals("AAA", ((KeyBasedSubTypeA) result.get(0)).aField);
assertEquals("BBB", ((KeyBasedSubTypeB) result.get(1)).bField);
}
}
The solution actually was very close, just missed a tiny step forward. It is #JsonTypeInfo(visible=true) required to make Jackson handle type info as normal property.
#JsonTypeInfo(use = Id.NAME, include = As.WRAPPER_OBJECT, property = "type", visible = true)
#JsonSubTypes({
#JsonSubTypes.Type(KeyBasedSubTypeA.class),
#JsonSubTypes.Type(KeyBasedSubTypeB.class)
})
public abstract class KeyBasedSuperType {
public String type;
public int commonField;
}
Related
I am using a class from another module in my request.
public class KeyInput {
#NotNull
private Long id;
#NotNull
private String startValue;
#NotNull
private String endValue;
}
I cannot put #JsonIgnoreProperties(ignoreUnknown = true) annotation on this class, since the module does not contain jackson library.
Putting it on field level where I used it on my request class didn't work out.
#JsonIgnoreProperties(ignoreUnknown = true)
private List<KeyInput> keys;
Here is the incoming request. Notice the source of the problem, the two fields (name and type), which are not declared in KeyInput class.
{
"id": 166,
"name": "inceptionDate",
"type": "DATE",
"startValue": "22",
"endValue": "24"
}
How am I supposed to tell the jackson to ignore the unknown fields if the class is not in my package?
P.S: I know I can get the keys as json string and serialize it using ObjectMapper (by setting the configuration DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES to false), but I am looking for a cleaner solution here.
Also putting those fields in my class and never use it, is another dirty solution.
Two ways come to my mind.
Method 1
Create an empty child class that inherit from KeyInput class. This is the easiest method.
#JsonIgnoreProperties(ignoreUnknown = true)
public class InheritedKeyInput extends KeyInput{}
Method 2
Create a custom de-serializer for KeyInput class.
public class KeyInputDeserializer extends JsonDeserializer<KeyInput> {
#Override
public KeyInput deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
KeyInput keyInput = new KeyInput();
keyInput.setId(node.get("id").asLong());
keyInput.setEndValue(node.get("startValue").textValue());
keyInput.setStartValue(node.get("startValue").textValue());
return keyInput;
}
}
Bind this de-serializer to KeyInput using a configuration class
#Configuration
public class JacksonConfig implements Jackson2ObjectMapperBuilderCustomizer {
#Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.failOnEmptyBeans(false)
.deserializerByType(KeyInput.class, new KeyInputDeserializer());
}
}
just simple add on above filed which you wanna ignore #JsonIgnore
My goal is to convert a JSON string field to the right class using Jackson.
I have the following class:
public class AnimalRecord {
private String id;
private String source;
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "source", include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
#JsonSubTypes(value = {
#JsonSubTypes.Type(value = CatProbeMetadata.class, name
= "catProbeMetadata"),
#JsonSubTypes.Type(value = DogProbeMetadata.class, name = "dogProbeMetadata"),
})
private AnimalMetadata metadata;
In addition to this class, I have a DB table where I store records of AnimalRecord (AnimalRecord = row). The AnimalMetadata is a different JSON string based on the source of this class. Each source has it's own metadata and class definition. In this example, CatProbeMetadata class will be the output when doing de-serialization from the string when the source is "cat".
The issue is that I'm not sure what to do when reading the rows from the DB. I have the following method:
private class ActiveProbeWrapper implements RowMapper<ActiveProbeRecord> {
#Override
public ActiveProbeRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
String id= rs.getString("id");
String source= rs.getString("source");
Animalmetadata metadata = // NOT SURE WHAT TO DO HERE;
ActiveProbeRecord record = new ActiveProbeRecord(deviceId,segment, source, metadata);
return record;
}
}
I need to convert the string in the DB to the right class instance, but my metadata string will NOT include the source (since it's outside the metadata JSON).
The question: Will I have to add the "source" field to the metadata itself or is there any better way of doing this that I missed?
Updated example:
Example of DB rows:
id | source | metadata
1 | catSource | {"catName": "Mewy"}
2 | dogSource | {"dogName": "Barky"}
When I read the rows from the DB I want to use the source field to de-serialize metadata to the right class - String --> CatMetadata
Jackson 2.12 introduced a
new feature for type deduction :
#JsonTypeInfo(use= JsonTypeInfo.Id.DEDUCTION)
#JsonSubTypes({
#JsonSubTypes.Type(DogMetadata.class),
#JsonSubTypes.Type(CatMetadata.class) })
public abstract class AnimalMetadata {
}
and therefore:
AnimalMetadata metadata = om.readValue("{\"catName\": \"Paws\"}", AnimalMetadata.class);
assertThat(metadata).isInstanceOf(CatMetadata.class);
The downside is that it might break if Jackson can't figure out which subtype to use based solely on properties names.
With this solution, optional json fields (like an absent catName property), or too similar subtypes may rise problems. #Sergei solutions doesn't have these issues (also, his solution makes use of the source field, which was your requirement).
On a side note, if you're working with SpringBoot, upgrading jackson is a matter of adding this property in pom.xml
<jackson-bom.version>2.12.3</jackson-bom.version>
The property attribute of the #JsonTypeInfo annotation marks the property that defines the entity subclass, and include = JsonTypeInfo.As.EXTERNAL_PROPERTY means that this property should be included not inside the metadata value, but on an upper level, as a property of the AnimalRecord class. This will only work if you parse the string as the AnimalRecord class.
This property should contain the value catProbeMetadata for cats and dogProbeMetadata of dogs, otherwise Jackson won't know how to parse the contents of your source field. The property may be also included inside the source string itself, but then you have to use include = JsonTypeInfo.As.PROPERTY.
Approach 1 - type is inside the metadata
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
#JsonSubTypes({
#JsonSubTypes.Type(value = CatProbeMetadata.class, name = "catProbeMetadata"),
#JsonSubTypes.Type(value = DogProbeMetadata.class, name = "dogProbeMetadata"),
})
class AnimalMetadata {
private String type;
}
class CatProbeMetadata extends AnimalMetadata {
private String catName;
}
class DogProbeMetadata extends AnimalMetadata {
private String dogName;
}
class AnimalRecord {
private AnimalMetadata metadata;
}
Then you could parse it as follows:
ObjectMapper mapper = new ObjectMapper();
AnimalRecord catRecord = new AnimalRecord();
catRecord.setMetadata(mapper.readValue("{\"type\":\"catProbeMetadata\",\"catName\": \"Paws\"}", AnimalMetadata.class));
AnimalRecord dogRecord = new AnimalRecord();
dogRecord.setMetadata(mapper.readValue("{\"type\":\"dogProbeMetadata\",\"dogName\": \"Fido\"}", AnimalMetadata.class));
Approach 2 - type is outside of metadata
Just select the class manually, based on the type. You don't need any annotations:
class AnimalMetadata {
}
class CatProbeMetadata extends AnimalMetadata {
private String catName;
}
class DogProbeMetadata extends AnimalMetadata {
private String dogName;
}
class AnimalRecord {
private String type;
private AnimalMetadata metadata;
}
Then you can parse like this. Putting the selection logic inside a separate method has exactly same consequences as putting it into annotations - you just need to update a different piece of code if you want to add a new subclass:
public Class<? extends AnimalMetadata> getMetadataClass(AnimalRecord record) {
switch (record.getType()) {
case "cat":
return CatProbeMetadata.class;
case "dog":
return DogProbeMetadata.class;
default:
throw new UnsupportedOperationException();
}
}
public void parse() {
ObjectMapper mapper = new ObjectMapper();
AnimalRecord catRecord = new AnimalRecord();
catRecord.setType("cat");
catRecord.setMetadata(mapper.readValue("{\"catName\": \"Paws\"}", getMetadataClass(catRecord)));
AnimalRecord dogRecord = new AnimalRecord();
dogRecord.setType("dog");
dogRecord.setMetadata(mapper.readValue("{\"dogName\": \"Fido\"}", getMetadataClass(dogRecord)));
}
I am quite new to Java and I am trying to deserialize the JSON using Jackson and I facing some minor issue with regards to declaring the Object/Variable type. I will provide all the codes then explain the issue for easy understanding.
I have an enum that will have the required type values:
public enum IdentifierTypeValues {
Type1,
Type2,
Type3,
//Constructor and Getter of enum values
}
Then for each of these type, I have different classes which will have different input and do a completely different type of process:
public class GenerateType1 {
private String name;
private String age;
//Getter and Setter
//Some required process based on these values
}
public class GenerateType2 {
private String address;
private String city;
private String country;
//Getter and Setter
//Some required process based on these values
}
public class GenerateType3 {
private String firstName;
private String lastName;
private String fullName;
//Getter and Setter
//Some required process based on these values
}
Now I have a wrapper class for these type of classes which will take the type based on enum and typeInfo values. I want the typeInfo values to be any of the class based type something like this:
public class TypeSyntax {
private IdentifierTypeValues indeitiferType;
private GenerateType1 / GenerateType2 / GenerateType3 identifierTypeValues;
//Here the identifierTypeValues can have the values for anytype
//How to declare a variable of any of these class type?
}
This is the class that will be used by my JSON for deserializing. I know I can add a wrapper class of those 3 types and provide that wrapper class as a type class for this. Something like this:
public class WrapperClass{
private GenerateType1 type1;
private GenerateType2 type2;
private GenerateType3 type3;
}
public class TypeSyntax{
private IdentifierTypeValues indeitiferType;
private WrapperClass identifierTypeValues;
//But using this approach will change my JSON structure which I do not want to do.
}
My JSON structure is something like this and I would like to keep it in the same way.
{
"indeitiferType":"Type1",
"identifierTypeValues":{
"name":"Batman",
"age":"2008"
}
}
Is there a way I can declare the variable of multiple type class? or any better approach to handle this by keeping the json format same? I tried searching but I am unable to search what exactly so any help would be really appriciated.
Because the type identifier exists on a different level than the other properties a wrapper class TypeSyntax needed. There are several open feature requests to add wrapping functionality to Jackson e.g. https://github.com/FasterXML/jackson-databind/issues/512
Fortunately polymorphism is supported in Jackson with #JsonTypeInfo and #JsonSubTypes annotations.
Wrapper class should look like:
public class TypeSyntax {
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "identifierType")
private GenerateTypeBase identifierTypeValues;
// getters and setters (omitted for brevity)
}
GenerateTypeBase is the common parent class
#JsonSubTypes({
#JsonSubTypes.Type(value = GenerateType1.class, name = "Type1"),
#JsonSubTypes.Type(value = GenerateType2.class, name = "Type2"),
})
public abstract class GenerateTypeBase {
private String name;
private String age;
// getters and setters (omitted for brevity)
}
In this different children classes will instantiated based on the identifierType property.
The children must extend this base class:
public class GenerateType2 extends GenerateTypeBase {
// additional properties
}
In a short test it will be:
#Test
void wrapperTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
GenerateType2 a = new GenerateType2();
a.setName("Foo");
a.setAge("13");
TypeSyntax w = new TypeSyntax();
w.setIdentifierTypeValues(a);
String json = mapper.writeValueAsString(w);
System.out.println(json);
}
and the output:
{
"identifierTypeValues":
{
"name":"Foo",
"age":"13"
},
"identifierType":"Type2"
}
Deserialization
#Test
void wrapperTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String input = "{\"identifierTypeValues\": \"name\":\"Foo\",\"age\":\"13\"},\"identifierType\":\"Type2\"}";
TypeSyntax w = mapper.readValue(new StringReader(input), TypeSyntax.class);
assertAll(
() -> assertEquals(GenerateType2.class, o.getIdentifierTypeValues().getClass()),
() -> assertEquals("13", o.getIdentifierTypeValues().getAge())
);
}
If you want more flexibility you can write custom (de)serializer and / or custom resolver. Using custom TypeIdResolver that will possible to convert identifiers to types programmatically instead of using "key-value pairs" in #JsonSubTypes
I am trying to create an java SDK for a front-end library that takes JSON input. Essentially, this SDK converts objects of certain type into JSON which is then consumed by that front-end library.
I am using jackson's polymorphic serialization/deserialization using its annotation system.
I have a base class A and 2 child classes B and C extending A. Class A has a type field, using which I decide what class (B or C) is to be used. The syntax looks something like this:
#JsonTypeInfo({
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property= "type"
})
#JsonSubTypes({
#JsonSubTypes.Type(value = B.class, name = "b"),
#JsonSubTypes.Type(value = C.class, name = "c")
})
public class A {
private String type;
public void setType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
}
public class B extends A {
}
public class C extends A {
}
So now, when I use Jackson's ObjectMapper's readValue function and read the stringified JSON and convert to class A, I get the correct instance of either class A or class B based on the value of the type variable. However, and here is the actual problem, when I try to use the function getType I always get null in those objects. I am not sure why jackson is not setting those values on the object.
String json = "{ type: 'b' }"; // example input json
ObjectMapper om = new ObjectMapper();
A a = om.readValue(json, A.class);
// a is actually an instance of class B
a.getType()// this is null
You need to add to #JsonTypeInfo the parameter visible = true to avoid to remove the type when deserializing.
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property= "type",
visible = true
)
public abstract boolean visible() default false
Property that defines whether type identifier value will be passed as part of JSON stream to deserializer (true), or handled and
removed by TypeDeserializer (false). Property has no effect on
serialization. Default value is false, meaning that Jackson handles
and removes the type identifier from JSON content that is passed to
JsonDeserializer.
I've got the following problem with Jackson and type hierarchy. I'm serializing a class SubA which extends Base into a String,
and trying afterwards to derserialize it back. Of course at compile time, the system does not know whether it will be
Base or SubA so I'm expecting a Base and will do some other operations afterwards, if it is a SubA.
My Base class looks like:
#JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
#JsonSubTypes({
#Type(value = SubA.class, name = "SubA")
})
public class Base {
protected String command; // +get +set
protected String type; // +get +set
}
... and a class deriving from Base:
#JsonTypeName("SubA")
public class SubA extends Base {
private AnotherClass anotherClass; // +get +set
private String test; // +get +set
#JsonIgnore
#Override
public String getType() {
return "SubA";
}
}
... and I'm trying to execute the following code:
ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
Base payload = new SubA(); // + setting anotherClass as well as test variables
String requestStringSend = ow.writeValueAsString(payload);
System.out.println("Sending: " + requestStringSend);
Base received = mapper.readValue(requestStringSend, Base.class);
String requestStringReceived = ow.writeValueAsString(received);
System.out.println("Received: " + requestStringReceived);
The String requestStringSend is:
Sending: {
"command" : "myCommand",
"type" : "SubA",
"anotherClass" : {
"data" : "someData"
},
"test" : "test123"
}
But I'm keep getting the same error over and over again. The mapper does now know what to do with the anotherClass parameter - it does not exist in Base. But I thought the mapper will convert it into an SubA class?
Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "anotherClass" (Class com.test.Base), not marked as ignorable
at [Source: java.io.StringReader#1256ea2; line: 4, column: 21] (through reference chain: com.test.Base["anotherClass"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:649)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:635)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1355)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:717)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at com.test.Foo.main(Foo.java:32)
I had a look at the following questions/resources:
Json deserialization into another class hierarchy using Jackson
http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
Your code looks correct for the use case. One possible problem is that you could be accidentally using Jackson 2 annotations with Jackson 1 ObjectMapper (I can see latter is Jackson from package names in exception). Version of annotations and mapper must match; otherwise annotations will be ignored, and this would explain problems you are seeing.