Java 让杰克逊在任何地方都使用自定义反序列化器(对于不是我的类型)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6553051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:41:51  来源:igfitidea点击:

Make Hymanson use a custom deserializer everywhere (for a type which isn't mine)

javajsondeserializationHymanson

提问by missionE46

I am trying to setup Hymanson JSON custom deserializer to convert JSON values into a Longobject. I followed the instructions on this site : http://wiki.fasterxml.com/HymansonHowToCustomDeserializersto setup the custom deserializer.

我正在尝试设置 Hymanson JSON 自定义解串器以将 JSON 值转换为Long对象。我按照此站点上的说明进行操作:http: //wiki.fasterxml.com/HymansonHowToCustomDeserializers来设置自定义解串器。

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

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

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

采纳答案by Programmer Bruce

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);


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

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

import java.io.IOException;

import org.codehaus.Hymanson.JsonParser;
import org.codehaus.Hymanson.JsonProcessingException;
import org.codehaus.Hymanson.Version;
import org.codehaus.Hymanson.map.DeserializationContext;
import org.codehaus.Hymanson.map.JsonDeserializer;
import org.codehaus.Hymanson.map.ObjectMapper;
import org.codehaus.Hymanson.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;
  }
}