java - Setting up JSON custom deserializer -


i trying setup jackson json custom deserializer convert json values long object. followed instructions on site : http://wiki.fasterxml.com/jacksonhowtocustomdeserializers setup custom deserializer.

however, custom deserializer kick in, have annotate each time e.g.

public class testbean {     long value;      @jsondeserialize(using=longjsondeserializer.class)     public void setvalue(long value) {        this.value = value;      }  } 

is there way tell jackson use custom deserializer deserialize long without having use @jsondeserialize(using=longjsondeserializer.class) annotation each time?

longjsondeserializer deserializer = new longjsondeserializer();  simplemodule module =   new simplemodule("longdeserializermodule",       new version(1, 0, 0, null)); module.adddeserializer(long.class, deserializer);  objectmapper mapper = new objectmapper(); mapper.registermodule(module); 

here's full demo application. works latest release of jackson, , jackson versions going 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;   } } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -