Consider this property
#JsonProperty
private Map<String, Object> myMap;
When a contained java.util.Date value is serialized as long, it will not be deserialized to Date again because the type information is not present in Map<String, Object>. How can I bypass the problem? I read answers about this question which would be a work around but there would be no way to distinguish strings containing dates from dates serialized as strings in the map. Can I tell Jackson to include type information for each map value such that Jackson can deserialize them correctly?
Implement a custom Deserializer and add the Annotation #JsonDeserialize(using = DateDeserializer.class) to your field.
Take a look at this example:
Your Json-Bean:
public class Foo {
private String name;
#JsonProperty
#JsonDeserialize(using = DateDeserializer.class)
private Map<String, Object> dates;
[...] // getter, setter, equals, hashcode
}
Deserializer:
public class DateDeserializer extends JsonDeserializer<Map<String, Object>> {
private TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {};
#Override
public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt, Map<String, Object> target) throws IOException, JsonProcessingException {
Map<String, Long> map = new ObjectMapper().readValue(p, typeRef);
for(Entry<String, Long> e : map.entrySet()){
Long value = e.getValue();
String key = e.getKey();
if(value instanceof Long){ // or if("date".equals(key)) ...
target.put(key, new Date(value));
} else {
target.put(key, value); // leave as is
}
}
return target;
}
#Override
public Map<String, Object> deserialize(JsonParser paramJsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return this.deserialize(paramJsonParser, ctxt, new HashMap<>());
}
}
Simple test:
public static void main(String[] args) throws Exception {
Foo foo1 = new Foo();
foo1.setName("foo");
foo1.setData(new HashMap<String, Object>(){{
put("date", new Date());
put("bool", true);
put("string", "yeah");
}});
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(foo1);
System.out.println(jsonStr);
Foo foo2 = mapper.readValue(jsonStr, Foo.class);
System.out.println(foo2.equals(foo1));
}
Finally, I came up with this solution. Deserializer:
private TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {
};
#Override
public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt, Map<String, Object> target) throws IOException {
Map<String, Object> map = new ObjectMapper().readValue(p, typeRef);
for (Map.Entry<String, Object> e : map.entrySet()) {
if (e.getKey().endsWith("[date]")) {
target.put(e.getKey().substring(0, e.getKey().length() - 6), new Date((Long) e.getValue()));
}
else {
target.put(e.getKey(), e.getValue());
}
}
return target;
}
Serializer:
#Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
Map<String, Object> adaptedValue = new HashMap<>(value);
for (Map.Entry<String, Object> e : value.entrySet()) {
if (e.getValue() instanceof Date) {
adaptedValue.put(e.getKey() + "[date]", ((Date) e.getValue()).getTime());
adaptedValue.remove(e.getKey());
}
}
new ObjectMapper().writeValue(gen, adaptedValue);
}
The map key is adapted dependent on the data type. This is easily extendable.
Related
I have a JAVA POJO which has many fields. One of the fields is Map<String, Object> for which I am using the Custom JsonSerializer as it can have many type of Objects. All I want to know is how can I avoid the Serialization of the fieldname only for this Map<String,Object> field. For all other fields in POJO, I would like to have the field name but only for this, I want to remove it.
As of now when use Jackson searlizer then I get the following output:
{
"isA" : "Human",
"name" : "Batman",
"age" : "2008",
"others" : {
"key1" : "value1",
"key2" : {
"key3" : "value3"
},
"key5" : {
"key4" : "One",
"key4" : "Two"
}
}
}
I want to get the following output: (All I want to do is remove the Map<String,Object> field name but keep its children.)
{
"isA" : "Human",
"name" : "Batman",
"age" : "2008",
"key1" : "value1",
"key2" : {
"key3" : "value3"
},
"key5" : {
"key4" : "One",
"key4" : "Two"
}
}
Following is my Human.class POJO which is used by ObjectMapper:
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
#Getter
#Setter
#NoArgsConstructor
#ToString
class Human {
private String isA;
private String name;
private String age;
#JsonSerialize(using = MyCustomSearlize.class)
private Map<String, Object> others = new HashMap<>();
}
Following is my Custom searlizer which is used by MAP during searlization:
class MyCustomSearlize extends JsonSerializer<Map<String, Object>> {
private static final ObjectMapper mapper = new ObjectMapper();
#Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
recusiveSerializer(value, gen, serializers);
gen.writeEndObject();
}
public void recusiveSerializer(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
for (Map.Entry<String, Object> extension : value.entrySet()) {
if (extension.getValue() instanceof Map) {
//If instance is MAP then call the recursive method
gen.writeFieldName(extension.getKey());
gen.writeStartObject();
recusiveSerializer((Map) extension.getValue(), gen, serializers);
gen.writeEndObject();
} else if (extension.getValue() instanceof String) {
//If instance is String directly add it to the JSON
gen.writeStringField(extension.getKey(), (String) extension.getValue());
} else if (extension.getValue() instanceof ArrayList) {
//If instance if ArrayList then loop over it and add it to the JSON after calling recursive method
for (Object dupItems : (ArrayList<Object>) extension.getValue()) {
if (dupItems instanceof Map) {
gen.writeFieldName(extension.getKey());
gen.writeStartObject();
recusiveSerializer((Map) dupItems, gen, serializers);
gen.writeEndObject();
} else {
gen.writeStringField(extension.getKey(), (String) dupItems);
}
}
}
}
}
}
Following is my Main class:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
Human person = new Human();
person.setName("Batman");
person.setAge("2008");
Map<String, Object> others = new HashMap<>();
others.put("key1", "value1");
Map<String, Object> complex = new HashMap<>();
complex.put("key3", "value3");
others.put("key2", complex);
Map<String, Object> complex2 = new HashMap<>();
List<String> dup = new ArrayList<>();
dup.add("One");
dup.add("Two");
complex2.put("key4", dup);
others.put("key5", complex2);
person.setOthers(others);
final ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
objectMapper.registerModule(simpleModule);
final String jsonEvent = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);
System.out.println(jsonEvent);
}
}
Following things I tried:
I tried to add #JsonValue on the Map but this will remove all my other values (name,age, isA, etc.)
I tried the #JsonAnyGetter this works for Map and String but does not work for ArrayList as I want. I am handling the ArrayList bit differently in my application as a part of my requirement.
Is there a way to may it work with #JsonSerialize and #JsonAnyGetter because I am unable to use both together.
Can someone please help in solving this issue? Please guide me to appropriate documentation or workaround thanks a lot.
From the wiki page it sounds like the #JsonUnwrapped annotation should do what you want.
#JsonUnwrapped: property annotation used to define that value should be "unwrapped" when serialized (and wrapped again when deserializing), resulting in flattening of data structure, compared to POJO structure.
The Javadoc for the class also has an example that looks appropriate.
As mentioned in another answer #JsonUnwrapped might work but I used the following approach to get it working. Posting here as it can be helpful to someone in the future:
I added the #JsonAnyGetter and #JsonSearlize on the Getter method of Map and got it to work.
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
#Getter
#Setter
#NoArgsConstructor
#ToString
class Human {
private String isA;
private String name;
private String age;
#JsonIgnore
private Map<String, Object> others = new HashMap<>();
#JsonAnyGetter
#JsonSerialize(using = MyCustomSearlize.class)
public Map<String,Object> getOther(){
return others;
}
}
In the MyCustomSearlize class code I removed the start and end object
#Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
recusiveSerializer(value, gen, serializers);
}
I am new to java and trying to learn about objectmapper. I am using it to convert a map to a pojo. The keys in the map are string and all the values are string values except one which I want to convert to a Map.
Please go through the below example code for more clearer picture.
POJO Class:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.annotation.Nonnull;
import java.util.Map;
public class POJOClass {
private String string1;
private Map<String, String> map1;
#JsonCreator
public POJOClass(#Nonnull #JsonProperty(value="String1", required = true) String string1,
#Nonnull #JsonProperty(value = "Map1", required = true) Map<String, String> map1) {
this.string1 = string1;
this.map1 = map1;
}
#Nonnull
public String getString1() {
return string1;
}
#Nonnull
public Map<String, String> getMap1() {
return map1;
}
}
Test Code:
#Test
public void testPOJOClass() {
Map<String, String> map = new HashMap<>();
map.put("String1", "string");
map.put("Map1", "{\"key1\" : \"value1\", \"key2\":\"value2\", \"key3\" : null }");
ObjectMapper mapper = new ObjectMapper();
POJOClass pojoClass = mapper.convertValue(map, POJOClass.class);
}
Exception:
java.lang.IllegalArgumentException: Can not instantiate value of type [map type; class java.util.LinkedHashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from String value ('{"key1" : "value1", "key2":"value2", "key3" : null }'); no single-String constructor/factory method
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3286)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3212)
Variant Options Tried:
I know that I can keep map1 field also as a String and later on convert it into map using another instance of object mapper, but I want to avoid it. Is there any way to directly convert the string in the test code to the mentioned Pojo directly.
I even tried changing the type of map1 from Map to Map but even that didn't work.
public class CustomerDeserializer extends JsonDeserializer<Map<String, String>> {
#Override
public Map<String, String> deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
final ObjectMapper mapper = (ObjectMapper) p.getCodec();
return mapper.readValue(p.getText(), new TypeReference<Map<String, String>>() {
});
}
}
You can write a customer deserializer, annotate JsonDeserialize on map1 parameter in your constructor
#JsonDeserialize(using = CustomerDeserializer.class)
#JsonProperty(value = "Map1", required = true) Map<String, String> map1) {
I'm using Jackson in a java Rest Api to handle request params.
My Bean class :
public class ZoneModifBeanParam extends ModifBeanParam<Zone> {
#FormParam("type")
private String type;
#FormParam("geometry")
private Geometry geometry;
#FormParam("name")
private String name;
...
My API interface :
#POST
#Consumes("application/json")
#Produces("application/json; subtype=geojson")
#ApiOperation(value = "Create a zone", notes = "To create a zone")
public Response createZone(ZoneModifBeanParam zoneParam) {
...
This Works fine but I need to receive other params that aren't specified by my Bean in a Map.
Example :
{
"geometry": {...},
"name": "A circle name",
"type": "4",
"hello": true
}
By receiving this I need to store in a Map (named unrecognizedFields and declared in my bean) the couple ("hello", true).
Is there any annotation or object allowing this?
Just use #JsonAnySetter. That's what it's made for. Here is a test case
public class JacksonTest {
public static class Bean {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private Map<String, Object> unrecognizedFields = new HashMap<>();
#JsonAnyGetter
public Map<String, Object> getUnrecognizedFields() {
return this.unrecognizedFields;
}
#JsonAnySetter
public void setUnrecognizedFields(String key, Object value) {
this.unrecognizedFields.put(key, value);
}
}
private final String json
= "{\"name\":\"paul\",\"age\":600,\"nickname\":\"peeskillet\"}";
private final ObjectMapper mapper = new ObjectMapper();
#Test
public void testDeserialization() throws Exception {
final Bean bean = mapper.readValue(json, Bean.class);
final Map<String, Object> unrecognizedFields = bean.getUnrecognizedFields();
assertEquals("paul", bean.getName());
assertEquals(600, unrecognizedFields.get("age"));
assertEquals("peeskillet", unrecognizedFields.get("nickname"));
}
}
The #JsonAnyGetter is used on the serialization side. When you serialize the bean, you will not see the unrecognizedFields in the JSON. Instead all the properties in the map will be serialized as top level properties in the JSON.
You may be able to ignore the unrecognized fields safely by configuring the ObjectMapper, however to specifically put them as key-value pairs of a Map field, you'll need your own de-serializer.
Here's a (heavily simplified) example:
Given your POJO...
#JsonDeserialize(using=MyDeserializer.class)
class Foo {
// no encapsulation for simplicity
public String name;
public int value;
public Map<Object, Object> unrecognized;
}
... and your custom de-serializer...
class MyDeserializer extends JsonDeserializer<Foo> {
#Override
public Foo deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
// new return object
Foo foo = new Foo();
// setting unrecognized container
Map<Object, Object> unrecognized = new HashMap<>();
foo.unrecognized = unrecognized;
// initializing parsing from root node
JsonNode node = p.getCodec().readTree(p);
// iterating node fields
Iterator<Entry<String, JsonNode>> it = node.fields();
while (it.hasNext()) {
Entry<String, JsonNode> child = it.next();
// assigning known fields
switch (child.getKey()) {
case "name": {
foo.name = child.getValue().asText();
break;
}
case "value": {
foo.value = child.getValue().asInt();
break;
}
// assigning unknown fields to map
default: {
foo.unrecognized.put(child.getKey(), child.getValue());
}
}
}
return foo;
}
}
Then, somewhere...
ObjectMapper om = new ObjectMapper();
Foo foo = om.readValue("{\"name\":\"foo\",\"value\":42,\"blah\":true}", Foo.class);
System.out.println(foo.unrecognized);
Output
{blah=true}
I'm trying to serialize/deserialize a Map<?, ?> with arbitrary object as keys with Jackson version 2.8. The JSON counterpart should be an array of couples, i.e. given
public class Foo {
public String foo;
public Foo(String foo) {
this.foo = foo;
}
}
public class Bar {
public String bar;
public Bar(String bar) {
this.bar = bar;
}
}
then
Map<Foo, Bar> map;
map.put(new Foo("foo1"), new Bar("bar1"));
map.put(new Foo("foo2"), new Bar("bar2"));
should be represented by this JSON
[
[ { "foo": "foo1" }, { "bar": "bar1" } ],
[ { "foo": "foo2" }, { "bar": "bar2" } ]
]
So I did the serializer part as
public class MapToArraySerializer extends JsonSerializer<Map<?, ?>> {
#Override
public void serialize(Map<?, ?> value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartArray();
for (Map.Entry<?, ?> entry : value.entrySet()) {
gen.writeStartArray();
gen.writeObject(entry.getKey());
gen.writeObject(entry.getValue());
gen.writeEndArray();
}
gen.writeEndArray();
}
}
but I have no idea how to write a JsonDeserializer to do the inverse job. Any suggestions?
Note: I need the [ [ "key1", "value1" ], [ "key2", "value2" ] ] notation to be able to consume that JSON in JavaScript a new Map( ... ) and JSON.stringify(map) would produce that notation too (see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map).
To clarify, such a map would be a field of other classes, e.g.
public class Baz {
#JsonSerialize(using = MapToArraySerializer.class)
#JsonDeserialize(using = ArrayToMapDeserializer.class, keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
and ArrayToMapDeserializer extends JsonDeserializer<Map<?, ?>> is where I'm asking for help.
I came up with this solution:
public class ArrayToMapDeserializer extends JsonDeserializer<SortedMap<Object, Object>>
implements ContextualDeserializer {
private Class<?> keyAs;
private Class<?> contentAs;
#Override
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return this.deserialize(p, ctxt, new HashMap<>());
}
#Override
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt,
Map<Object, Object> intoValue) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
ObjectCodec codec = p.getCodec();
if (node.isArray()) {
node.forEach(entry -> {
try {
JsonNode keyNode = entry.get(0);
JsonNode valueNode = entry.get(1);
intoValue.put(keyNode.traverse(codec).readValueAs(this.keyAs),
valueNode.traverse(codec).readValueAs(this.contentAs));
} catch (NullPointerException | IOException e) {
// skip entry
}
});
}
return intoValue;
}
#Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
JsonDeserialize jsonDeserialize = property.getAnnotation(JsonDeserialize.class);
this.keyAs = jsonDeserialize.keyAs();
this.contentAs = jsonDeserialize.contentAs();
return this;
}
}
which can be used like this:
public class Baz {
#JsonSerialize(using = MapToArraySerializer.class)
#JsonDeserialize(using = ArrayToMapDeserializer.class,
keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
Here is the deserialize:
#Override
public Map<?, ?> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
Map map = new LinkedHashMap();
ObjectCodec oc = p.getCodec();
JsonNode anode = oc.readTree(p);
for (int i = 0; i < anode.size(); i++) {
JsonNode node = anode.get(i);
map.put(node.get(0), node.get(1));
}
return map;
}
I added a few test cases, with a new Oson implementation, to the original solution, in which I used oson to do the conversion, but with a different convension: map to json: {key1: value1, key2: value2, ...}, so the json output becomes:
{
{
"foo": "foo1"
}: {
"bar": "bar1"
},
{
"foo": "foo2"
}: {
"bar": "bar2"
}
}
You can check out the source code!
Assume, I have the following structure:
public class SomeClass {
private String id;
#JsonProperty("key-value")
private Map<String, Object> keyValue;}
Obviously, it will be serialized to
{
"id" : "id1",
"key-value" :
{"key1" : "value1",
"key2 : "value2"}
}
Is it possible to represent it like this?
{
"id" : "id1",
"key1" : "value1",
"key2 : "value2"
}
Thanks in advance!
It is quite possible with the help of Jackson's custom serializer:
add the #JsonSerialize annoation to your POJO:
(also added necessary ctor and getters)
#JsonSerialize(using = SomeClassSerializer.class)
public static class SomeClass {
private String id;
#JsonProperty("key-value")
private Map<String, Object> keyValue;
public SomeClass(String id, Map<String, Object> keyValue) {
this.id = id;
this.keyValue = keyValue;
}
public String getId() { return id; }
public Map<String, Object> getKeyValue() { return keyValue; }
}
the custom serializer looks like this:
:
public class SomeClassSerializer extends JsonSerializer<SomeClass>
{
#Override
public void serialize(SomeClass sc, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException
{
gen.writeStartObject();
// write id propertry
gen.writeStringField("id", sc.getId());
// loop on keyValue entries, write each as property
for (Map.Entry<String, Object> keyValueEntry : sc.getKeyValue().entrySet()) {
gen.writeObjectField(keyValueEntry.getKey(), keyValueEntry.getValue());
}
gen.writeEndObject();
}
}
calling Jackson's mapper is done in the usual manner:
:
public static void main(String[] args)
{
Map<String, Object> keyValue = new HashMap<>();
keyValue.put("key1", "value1");
keyValue.put("key2", "value2");
keyValue.put("key3", new Integer(10));
SomeClass sc = new SomeClass("id1", keyValue);
try {
new ObjectMapper().writeValue(System.out, sc);
} catch (IOException e) {
e.printStackTrace();
}
}
output:
{"id":"id1","key1":"value1","key2":"value2","key3":10}