Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Jackson doesn't currently have a built-in configuration to automatically handle this particular case, so custom deserialization processing is necessary.</p> <p>Following is an example of what such custom deserialization might look like.</p> <pre><code>import java.io.IOException; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.Version; import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; import org.codehaus.jackson.annotate.JsonMethod; import org.codehaus.jackson.map.DeserializationContext; import org.codehaus.jackson.map.JsonDeserializer; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.module.SimpleModule; public class JacksonFoo { public static void main(String[] args) throws Exception { // {"property1":{"property2":42}} String json1 = "{\"property1\":{\"property2\":42}}"; // {"property1":[]} String json2 = "{\"property1\":[]}"; SimpleModule module = new SimpleModule("", Version.unknownVersion()); module.addDeserializer(Thing2.class, new ArrayAsNullDeserializer()); ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY).withModule(module); Thing1 firstThing = mapper.readValue(json1, Thing1.class); System.out.println(firstThing); // output: // Thing1: property1=Thing2: property2=42 Thing1 secondThing = mapper.readValue(json2, Thing1.class); System.out.println(secondThing); // output: // Thing1: property1=null } } class Thing1 { Thing2 property1; @Override public String toString() { return String.format("Thing1: property1=%s", property1); } } class Thing2 { int property2; @Override public String toString() { return String.format("Thing2: property2=%d", property2); } } class ArrayAsNullDeserializer extends JsonDeserializer&lt;Thing2&gt; { @Override public Thing2 deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.readValueAsTree(); if (node.isObject()) return new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY).readValue(node, Thing2.class); return null; } } </code></pre> <p>(You could make use of DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY to force the input to always bind to a collection, but that's probably not the approach I'd take given how the problem is currently described.)</p>
    singulars
    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