Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

java - Make Jackson use a custom deserializer everywhere (for a type which isn't mine)

I am trying to setup Jackson JSON custom deserializer to convert JSON values into a 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;
     }
 }

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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 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;
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...