Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I might approach it.</p> <pre><code>import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.Version; import org.codehaus.jackson.map.BeanProperty; import org.codehaus.jackson.map.ContextualDeserializer; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.DeserializationContext; import org.codehaus.jackson.map.JsonDeserializer; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.module.SimpleModule; public class Foo { public static void main(String[] args) throws Exception { EmptyStringAsMapDeserializer&lt;Map&lt;String, ?&gt;&gt; emptyStringAsMapDeserializer = new EmptyStringAsMapDeserializer&lt;Map&lt;String, ?&gt;&gt;(null, new ObjectMapper()); SimpleModule module = new SimpleModule("ThingsDeserializer", Version.unknownVersion()); module.addDeserializer(Map.class, emptyStringAsMapDeserializer); ObjectMapper mapper = new ObjectMapper().withModule(module); Person person = mapper.readValue(new File("input.json"), Person.class); System.out.println(mapper.writeValueAsString(person)); } } class Person { public int id; public String name; public Map&lt;String, Car&gt; cars; public Map&lt;String, Bike&gt; bikes; } class Car { public String color; public String buying_date; } class Bike { public String color; } class EmptyStringAsMapDeserializer&lt;T&gt; extends JsonDeserializer&lt;Map&lt;String, ?&gt;&gt; implements ContextualDeserializer&lt;Map&lt;String, ?&gt;&gt; { private Class&lt;?&gt; targetType; private ObjectMapper mapper; EmptyStringAsMapDeserializer(Class&lt;?&gt; targetType, ObjectMapper mapper) { this.targetType = targetType; this.mapper = mapper; } @Override public JsonDeserializer&lt;Map&lt;String, ?&gt;&gt; createContextual(DeserializationConfig config, BeanProperty property) throws JsonMappingException { return new EmptyStringAsMapDeserializer&lt;Object&gt;(property.getType().containedType(1).getRawClass(), mapper); } @Override public Map&lt;String, ?&gt; deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.readValueAsTree(); if ("".equals(node.getTextValue())) return new HashMap&lt;String, Object&gt;(); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(node, mapper.getTypeFactory().constructMapType(Map.class, String.class, targetType)); } } </code></pre> <p>The generic type parameters might be a little out of order. I did a bit of quick copy-pasting.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload