设置JSON自定义反序列化器 [英] Setting up JSON custom deserializer

查看:375
本文介绍了设置JSON自定义反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置Jackson JSON自定义反序列化器以将JSON值转换为Long对象。
我按照本网站上的说明操作: http://wiki.fasterxml.com/JacksonHowToCustomDeserializers 设置自定义反序列化器。

I am trying to setup Jackson JSON custom deserializer to convert JSON values into Long object. I followed the instructions on this site : http://wiki.fasterxml.com/JacksonHowToCustomDeserializers to setup the custom deserializer.

但是,要启动自定义反序列化器,我必须每次都注释
例如

However, for the custom deserializer to kick in, I have to always annotate each time e.g.

public class TestBean {
    Long value;

    @JsonDeserialize(using=LongJsonDeserializer.class)
    public void setValue(Long value) {
       this.value = value;
     }
 }

有没有办法告诉杰克逊总是使用自定义反序列化器反序列化Long而不必每次都使用@JsonDeserialize(using = LongJsonDeserializer.class)注释?

Is there a way to tell Jackson to always use the custom deserializer to deserialize Long without having to use @JsonDeserialize(using=LongJsonDeserializer.class) annotation each time?

推荐答案

LongJsonDeserializer deserializer = new LongJsonDeserializer();

SimpleModule module =
  new SimpleModule("LongDeserializerModule",
      new Version(1, 0, 0, null, null, null));
module.addDeserializer(Long.class, deserializer);

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






这是一个完整的演示应用程序。这适用于Jackson的最新版本,也可能与Jackson版本一起回溯到1.7。


Here's a full demo application. This works with the latest release of Jackson, and probably also with Jackson versions going back to 1.7.

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.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    TestBean bean = new TestBean();
    bean.value = 42L;

    ObjectMapper mapper = new ObjectMapper();

    String beanJson = mapper.writeValueAsString(bean);
    System.out.println(beanJson);
    // output: {"value":42}

    TestBean beanCopy1 = mapper.readValue(beanJson, TestBean.class);
    System.out.println(beanCopy1.value);
    // output: 42

    SimpleModule module =
      new SimpleModule("LongDeserializerModule",
          new Version(1, 0, 0, null));
    module.addDeserializer(Long.class, new LongJsonDeserializer());

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

    TestBean beanCopy2 = mapper.readValue(beanJson, TestBean.class);
    System.out.println(beanCopy2.value);
    // output: 126
  }
}

class TestBean
{
  Long value;
  public Long getValue() {return value;}
  public void setValue(Long value) {this.value = value;}
}

class LongJsonDeserializer extends JsonDeserializer<Long>
{
  @Override
  public Long deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
  {
    Long value = jp.getLongValue();
    return value * 3;
  }
}

这篇关于设置JSON自定义反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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