8

I get JSON from a web service and can not influence the JSON format. The JSON code below is just an example to illustrate the problem. The field cars can either be an object containing Car objects or it can be an empty string. If I could change the web service, I'd change the empty String to be an empty object like "cars" : {} instead of "cars" : "".

When trying to map JSON to this Java object:

public class Person {
    public int id;
    public String name;
    public Map<String, Car> cars;
}

This works:

{
    "id" : "1234",
    "name" : "John Doe",
    "cars" : {
        "Tesla Model S" : {
            "color" : "silver",
            "buying_date" : "2012-06-01"
        },
        "Toyota Yaris" : {
            "color" : "blue",
            "buying_date" : "2005-01-01"
        }
    }
}

And this fails:

{
    "id" : "1",
    "name" : "The Dude",
    "cars" : ""
}

What would be the best way to handle this case in Jackson? If there's the empty string, I'd like to get null for the field cars. I tried using ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, but it didn't help.

Adrian H.
  • 569
  • 6
  • 17

1 Answers1

7

The field cars can either contain a list of Car objects ... This works:

{
    "id" : "1234",
    "name" : "John Doe",
    "cars" : {
        "Tesla Model S" : {
            "color" : "silver",
            "buying_date" : "2012-06-01"
        },
        "Toyota Yaris" : {
            "color" : "blue",
            "buying_date" : "2005-01-01"
        }
    }
}

The "cars" element value is not a list (aka array). It's a JSON object, which can also be considered a map-type collection, but it is not a list.

So, to rephrase the issue, the goal is to deserialize JSON that is sometimes an object and sometimes an empty string into a Java Map.

To solve this, I'm surprised ACCEPT_EMPTY_STRING_AS_NULL_OBJECT didn't work. I recommend logging an issue at http://jira.codehaus.org/browse/JACKSON.

You could implement custom deserialization. Following is an example solution. If the target data structure has other Map references, then this solution would need to be accordingly changed.

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.ObjectCodec;
import org.codehaus.jackson.Version;
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;
import org.codehaus.jackson.type.TypeReference;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    SimpleModule module = new SimpleModule("CarsDeserializer", Version.unknownVersion());
    module.addDeserializer(Map.class, new CarsDeserializer());

    ObjectMapper mapper = new ObjectMapper().withModule(module);

    Person person1 = mapper.readValue(new File("input1.json"), Person.class);
    System.out.println(mapper.writeValueAsString(person1));
    // {"id":1234,"name":"John Doe","cars":{"Tesla Model S":{"color":"silver","buying_date":"2012-06-01"},"Toyota Yaris":{"color":"blue","buying_date":"2005-01-01"}}}

    Person person2 = mapper.readValue(new File("input2.json"), Person.class);
    System.out.println(mapper.writeValueAsString(person2));
    // {"id":1,"name":"The Dude","cars":{}}
  }
}

class Person
{
  public int id;
  public String name;
  public Map<String, Car> cars;
}

class Car
{
  public String color;
  public String buying_date;
}

class CarsDeserializer extends JsonDeserializer<Map<String, Car>>
{
  @Override
  public Map<String, Car> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
      JsonProcessingException
  {
    ObjectCodec codec = jp.getCodec();
    JsonNode node = codec.readTree(jp);
    if (!"".equals(node.getTextValue()))
    {
      ObjectMapper mapper = new ObjectMapper();
      return mapper.readValue(node, new TypeReference<Map<String, Car>>() {});
    }
    return new HashMap<String, Car>(); // or return null, if preferred
  }
}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • Thanks for the hint concerning lists. I changed the description accordingly. So I probably have to write a custom deserialization. – Adrian H. Jul 13 '11 at 20:00
  • Indeed. I just posted an update with such an example. (If it's too clumsy in the repetitive type declaration for your needs, it's possible to change to use a contextual deserializer. Also, note the need for a second decoder (ObjectMapper/ObjectCodec) in the custom deserializer, since the original is configured to just pass Map deserialization back to the custom deserializer. This second decoder could be externally configured and passed to the CarsDeserializer during instance construction.) – Programmer Bruce Jul 13 '11 at 20:06
  • `ACCEPT_EMPTY_STRING_AS_NULL_OBJECT` does actually work if the field is a simple object like `public Car car;`, but not if it is a map like `public Map cars;`. – Adrian H. Jul 13 '11 at 20:08
  • Right, which to me should be considered a bug, or at least something for which an enhancement is warranted. – Programmer Bruce Jul 13 '11 at 20:11
  • I need not only to create `Map` Objects but also some other Maps, let's say `Map`. So I created a generic `MapsGenericDeserializer extends JsonDeserializer>`. Now I would like the `createContextual()` method of the ContextualDeserializer to return one of these Deserializers, depending on the BeanProperty parameter type. That however does not work, because the method `public JsonDeserializer> createContextual()` can not return `new MapsGenericDeserializer()`. Do you have an idea how to work around this problem? – Adrian H. Jul 14 '11 at 23:41
  • Yes. Please post a new question. – Programmer Bruce Jul 14 '11 at 23:45
  • Ok, I just posted it [here](http://stackoverflow.com/questions/6704992/contextualdeserializer-for-mapping-json-to-different-types-of-maps-with-jackson). Thanks in advance! – Adrian H. Jul 15 '11 at 09:21