如何使用Jackson JSON处理器处理无效值? [英] How to process an invalid value with Jackson JSON processor?

查看:331
本文介绍了如何使用Jackson JSON处理器处理无效值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用杰克逊来处理json。

I use Jackson to proccess json.

现在,我面临着一个问题。

Now, I face a proplem.

我的POJO:

class Person{
    public String name;
    public int age;
}

JSON是

{"name":"Jackson","age":""}.

如果我写这样的代码:

Person person = mapper.readValue("{\"name\":\"Jackson\",\"age\":\"\"}", Person.class);

抛出异常:


无法从String值构造int实例:不是有效的int值。

Can not construct instance of int from String value "": not a valid int value.

如果是JSON是{\name \:\Jackson \,\age \:null},没关系。

If the JSON is "{\"name\":\"Jackson\",\"age\":null}", it’s OK.

但是现在,我不想修改JSON。我该怎么办?

But now , I don’t want to modify the JSON. And how can I do ?

推荐答案

我建议在 http://jira.codehaus.org/browse/JACKSON ,要求将此视为错误,或者添加允许正确处理的功能。 (也许合理的是 DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT 还允许将空的JSON字符串反序列化为默认的原始值,因为当绑定到Java时,这就是如何处理JSON空值的方式原语。)(更新:我为此记录了 issue 616 如果你想要实现它,请投票。)

I recommend logging an issue at http://jira.codehaus.org/browse/JACKSON, requesting that this be considered a bug, or that a feature to allow proper handling is added. (Maybe it's reasonable that DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT would also allow deserialization of empty JSON strings to default primitive values, since that's how JSON null values are otherwise handled, when bound to Java primitives.) (Update: I logged issue 616 for this. Vote for it if you want it implemented.)

在Jackson如此增强之前,需要自定义反序列化处理将JSON空字符串转换为默认原始值(或者想要任何非字符串值)。下面就是这样一个例子,幸运的是很简单,因为反序列化为int的现有代码已经处理了一个空字符串,将其变为0。

Until Jackson is so enhanced, custom deserialization processing would be necessary to transform a JSON empty string to a default primitive value (or to whatever non-string value is wanted). Following is such an example, which is fortunately simple, since the existing code to deserialize to an int already handles an empty string, turning it into 0.

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.deser.StdDeserializer;
import org.codehaus.jackson.map.module.SimpleModule;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    // {"name":"Jackson","age":""}
    String json = "{\"name\":\"Jackson\",\"age\":\"\"}";

    SimpleModule module = new SimpleModule("EmptyJsonStringAsInt", Version.unknownVersion());
    module.addDeserializer(int.class, new EmptyJsonStringAsIntDeserializer(int.class));

    ObjectMapper mapper = new ObjectMapper().withModule(module);
    Person p = mapper.readValue(json, Person.class);
    System.out.println(mapper.writeValueAsString(p));
    // {"name":"Jackson","age":0}
  }
}

class Person
{
  public String name;
  public int age;
}

class EmptyJsonStringAsIntDeserializer extends StdDeserializer<Integer>
{
  protected EmptyJsonStringAsIntDeserializer(Class<?> vc)
  {
    super(vc);
  }

  @Override
  public Integer deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
  {
    return super._parseIntPrimitive(jp, ctxt);
  }
}

(另请注意,如果目标类型为Integer而不是int,那么该字段将填充一个空值(不是那就是想要的)。为此,我记录了 issue 617 ,请求反序列化配置在绑定到原始包装类型字段时自动从JSON空值设置原始默认值。换句话说,从JSON空值反序列化为在整数字段中,目标字段将设置为 Integer.valueOf(0)而不是 null 。)

(Also, note that if the target type were Integer instead of int, then the field would be populated with a null value (not that that's what's wanted). For this, I logged issue 617, to request a deserialization configuration to automatically set the primitive default value from a JSON null value, when binding to a primitive wrapper type field. In other words, when deserializing from a JSON null value to an Integer field, the target field would be set to Integer.valueOf(0) instead of null.)

这篇关于如何使用Jackson JSON处理器处理无效值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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