映射与Jackson有不同类型的JSON字段? [英] Map a JSON field that can have different types with Jackson?

查看:276
本文介绍了映射与Jackson有不同类型的JSON字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Web服务获取JSON并且不能影响JSON格式。下面的JSON代码只是一个例子来说明问题。字段 cars 可以是包含 Car 对象的对象,也可以是空字符串。如果我可以更改Web服务,我会将空字符串更改为空对象,如cars:{} 而不是cars :

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" : "".

尝试将JSON映射到此Java对象时:

When trying to map JSON to this Java object:

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

这有效:

{
    "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"
        }
    }
}

此失败:

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

杰克逊处理此案件的最佳方法是什么?如果有空字符串,我想获得字段 cars null 。我尝试使用 ACCEPT_EMPTY_STRING_AS_NULL_OBJECT ,但它没有帮助。

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.

推荐答案


现场汽车可以包含汽车对象列表
...
这样可行:

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"
        }
    }
}

cars元素值不是列表(aka数组)。它是一个JSON对象,也可以被认为是一个map类型的集合,但它不是一个列表。

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.

因此,为了重新解释这个问题,目标是反序列化JSON有时是一个对象,有时候是一个空字符串到Java Map

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.

为了解决这个问题,我'我很惊讶 ACCEPT_EMPTY_STRING_AS_NULL_OBJECT 无效。我建议在 http://jira.codehaus.org/browse/JACKSON 上记录问题。

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.

您可以实施自定义反序列化。以下是一个示例解决方案。如果目标数据结构具有其他 Map 引用,则需要相应地更改此解决方案。

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
  }
}

这篇关于映射与Jackson有不同类型的JSON字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆