I have a generic Response Object that takes a status message and a payload object.
public class Response {
private String status;
private Object payload;
// getters / setters
}
The object can be any type, including lists or arrays. (Much like here: Jackson JSON List with Object Type).
In the case where the object is a List, I need a serializer and an annotation to tell Jackson about it:
#JsonSerialize(using = ModelSerializer.class)
private List<Model> models;
How can this be achieved in a generic way? I can introduce a common superclass to the object candidates but don't want to "implement" a seperate Response class for each type.
You can configure your object mapper by registering the serializer for List as described here : http://wiki.fasterxml.com/JacksonHowToCustomSerializers
ObjectMapper mapper = new ObjectMapper();
SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null));
testModule.addSerializer(new ModelSerializer());
mapper.registerModule(testModule);
When serializing your Response, Jackson will use default serialization for all object except for the ones declaring a custom serializer.
Related
I would like to deserialize a json string to a java object. I wanted to write a generic method.
public class ObjectSerializer<T> {
T t;
private ObjectMapper mapper = new ObjectMapper();
/* public Person deSerial(String json) throws Exception {
TypeReference<Person> typeRef = new TypeReference<Person>() {};
return mapper.readValue(json, typeRef);
} */
public T deSerialize(String jsonInput) throws Exception {
TypeReference<T> typeRef
= new TypeReference<T>() {};
return mapper.readValue(jsonInput, typeRef);
}
}
When I call deSerialize(validPersonJsonString) [validPersonJsonString : valid person JSON String], it is not working, it it throwing me the error:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.Person.
Whereas, when in call the commented deSerial method, it works fine. Please explain the issue.
Thanks.
Jackson doesn't support TypeReference with generic type parameters because of type erasure. Here's the bug about it.
As far as your use case is concerned, you don't need to use ObjectSerializer class at all. You can directly expose ObjectMapper object to your service layer and perform the deserialization.
If you want to shield the json serializing mechanism from your service layer then you can wrap ObjectMapper implementation into another class (like you have done in your code) but then, accept the class type as a method argument rather than a generic type.
I wrote a Custom Serializer and Custom Deserializer to serialize properties marked with #Confidential annotation.
#Data
public class Person {
private String name;
#Confidential
private String address;
}
The Custom Serializer serializes a POJO with following values:
{ "name": "John Doe", "address": "Kearney St"}
as follows:
{"name":"John Doe", "address": {"value":"IjIwMzEwIDU4dGggTG4gTkUi"}}
The Custom Deserializer is also able to deserialize the JSON back to the Person POJO fine.
However, when I make the fields in the Person POJO final, serialization continues to work, but deserialization fails.
#Data
public class Person {
private final String name;
#Confidential
private final String address;
}
Here's the BeanSerializerModifier implementation:
#AllArgsConstructor
public class CustomDeserializerModifier extends BeanDeserializerModifier {
private final ObjectMapper objectMapper;
#Override
public BeanDeserializerBuilder updateBuilder(final DeserializationConfig config,
final BeanDescription beanDesc,
final BeanDeserializerBuilder builder) {
Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
beanPropertyIterator.forEachRemaining(settableBeanProperty -> {
final Confidential annotation = settableBeanProperty.getAnnotation(Confidential.class);
if (encryptedProperty != null) {
JsonDeserializer<Object> current = settableBeanProperty.getValueDeserializer();
final SettableBeanProperty newSettableBeanProperty =
settableBeanProperty.withValueDeserializer(
new CustomDeserializer(annotation, current, objectMapper)
);
builder.addOrReplaceProperty(newSettableBeanProperty, true);
}
});
return builder;
}
}
I found that CustomDeserializer, never gets called when the Person POJO fields are final.
Here's the error message:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: {"name":"John Doe","address":{"value":"IjIwMzEwIDU4dGggTG4gTkUi"}}; line: 1, column: 30] (through reference chain: com.custom.model.Person["address"])
Can a Jackson expert please tell me why my CustomDeserializer isn't getting invoked when the POJO fields are final.
Thank you!
As mentioned, serialization will perfectly work for both mutable and immutable fields. Deserialization issue will only occur when using immutable fields as the BeanDeserializerModifier won't work in such a case.
In the Jackson terminology, immutable fields are named creator properties, meaning they are initialized using creators. See BeanDeserializerBase#resolve.
To correctly handle this use case, the ObjectMapper may be created with a custom DeserializationContext (an extended implementation of the ObjectMapper may also set the protected field related to the deserialization context).
Then, through an override of the method DeserializationContext#handleSecondaryContextualization, it will be possible to change the deserialization to make it work.
There is maybe other possibilities but this one is working fine with encryption.
I'm trying to (de)serialize an object that has a property with a type that comes from a maven dependency, so I can't change the class of this type.
The class of this type has a #JsonSerialize and #JsonDeserialize annotation.
However, I want to use the default serializer and deserialzer, because the custom serializer writes an array instead of an object. Is there a way, using annotations, to tell jackson to use the default (de)serializer?
You can disable the annotations using Jackson's mixins feature.
In the following example, any attempt at deserializing to a CustomerObj will result in an exception due to its defective Builder:
#JsonDeserialize(builder = CustomerObj.class)
public class CustomerObj {
public String name;
public int age;
public CustomerObj build() {
throw new RuntimeException("JsonDeserializer invoked");
}
}
Create a mixin with a JsonDeserialize annotation that disables the broken builder:
#JsonDeserialize(builder = java.lang.Void.class)
public static abstract class CustomerMixin { }
Register the mixin on the ObjectMapper instance:
ObjectMapper om = new ObjectMapper();
om.addMixIn(CustomerObj.class, CustomerMixin.class);
Enjoy working deserialization:
final String json = "{\"name\":\"Brian\",\"age\":41}";
CustomerObj customer = om.readValue(json, CustomerObj.class);
I'm using Jackson JSON library to convert some JSON objects to POJO classes. The problem is, when I use JSON Objects with duplicated properties like:
{
"name":"xiaopang",
"email":"xiaopang1#123.com",
"email":"xiaopang2#123.com"
}
Jackson report the last email pair "email":"xiaopang2#123.com" and then parse the object.
I've learned from Does JSON syntax allow duplicate keys in an object? that what happens when deserializing a JSON object with duplicate properties depends on the library implementation, either throwing an error or using the last one for duplicate key.
Despite overheads of tracking all properties, is there any way to tell Jackson to report an error or exception such as "Duplicate key" in this case?
Use JsonParser.Feature.STRICT_DUPLICATE_DETECTION
ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
MyPOJO result = mapper.readValue(json, MyPOJO.class);
Results in:
Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Duplicate field 'email'
You can also try to use DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY (more info) It will be trigggered if you deserialize your json string/input to jackson json tree first and then to you POJO. Can combine it with custom JsonDeserializer like this:
private static class MyPojoDeserializer extends JsonDeserializer<MyPOJO>{
#Override
public MyPOJO deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException{
JsonNode tree = p.readValueAsTree();
return p.getCodec().treeToValue(tree, MyPOJO.class);
}
}
Setup it once and use it same way as before:
// setup ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
SimpleModule module = new SimpleModule();
module.addDeserializer(MyPOJO.class,new MyPojoDeserializer() );
mapper.registerModule(module);
// use
mapper.readValue(json, MyPOJO.class);
Result:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Duplicate field 'email' for ObjectNode: not allowed when FAIL_ON_READING_DUP_TREE_KEY enabled
Other options would be to implement all the logic yourself in custom deserializer or in you POJO setter methods.
I need to provide custom deserialization of the Map and then each Property object has to be serialized by default serializer. This map is part of another object:
class PropertiesHolder {
Map<String, Property> properties;
}
I've defined mixin for the PropertiesHolder class:
class PropertiesHolderMixIn {
#JsonSerialize(using=PropertiesSerializer.class)
#JsonDeserialize(using=PropertiesDeserializer.class)
Map<String, Property> properties;
}
I have also mixin for Property class. The ObjectMapper initialization:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.setMixInAnnotation(Property.class, PropertyMixIn.class);
module.setMixInAnnotation(PropertiesHolder.class, PropertiesHolderMixIn.class);
mapper.registerModule(module);
My deserializer:
class PropertiesDeserializer extends JsonDeserializer<Map<String, Property>> {
public Map<String, Property> deserialize(JsonParser jp, DeserializationContext ctxt) throws ... {
ArrayNode node = (ArrayNode) jp.readValueAsTree();
for (int i = 0, size = node.size() ; i < size ; i++) {
ObjectNode jn = (ObjectNode) node.get(i);
String key = jn.get("propertyName").textValue();
String value = jn.get("propertyValue").toString();
... HERE I need to call registered deserializer for Property class over value ...
}
}
}
I've looked at How do I call the default deserializer from a custom deserializer in Jackson, but it doesn't work form me ... it ends with NPE. Also the solution described in the post creates deserializer for the outer class which for me is defined as mixin and I don't want to create deserializer for this class.
Please, point me to a solution. Where can I get default deserializer for the Property object?
Thanks
The problem is that you will need a fully constructed default deserializer; and this requires that one gets built, and then your deserializer gets access to it. DeserializationContext is not something you should either create or change; it will be provided by ObjectMapper
So all you need to write in the deserialize() method is:
ObjectMapper mapper = (ObjectMapper)jp.getCodec();
Property property = mapper.readValue(jn.get("propertyValue").toString(), Property.class));
Solution is this line of code:
ObjectMapper mapper = (ObjectMapper)jp.getCodec();
Call this method within "deserialize(...)" method. So the important (for me) code fragment is:
ObjectMapper mapper = (ObjectMapper)jp.getCodec();
Property property = mapper.readValue(jn.get("propertyValue").toString(), Property.class));
Found on this blog.