java - How to apply a transformation on all values when deserializing Json with Jackson -


i want parse json document jackson , apply transformation on nodes. example, let's want values in uppercase after deserialization.

the actual use case bit more complex:

  • transformation more complex, transformer class need injected configuration, i'd configureable instance
  • transformation has happen on properties, i'd able not add annotation on each property of each class deserialized.

there enough configuration options / hooks in jackson, i'm sure possible, can't find way around.

the test below shows i'm trying achieve:

public class jsonvaluefiltertest {      private objectmapper mapper;      @before     public void setupobjectmapper() {         mapper = new objectmapper();         // todo: configure mapper upper case values     }      @test     public void printjson() throws ioexception {         entity myentity = new entity("myname");         mapper.writevalue(system.out, myentity); // prints: {"name":"myname"}     }      @test     public void valuesareuppercasedwhenloaded() throws ioexception {         entity myentity = mapper.readvalue("{\"name\":\"myname\"}", entity.class);         assertthat(myentity.getname()).isequalto("myname"); // fails     }      public static class entity {         private final string name;          @jsoncreator         public entity(@jsonproperty("name") string name) { this.name = name; }          public string getname() { return name; }          @override         public string tostring() { return "name='" + name + "'"; }     } } 

you can use converter simple case not implement custom deserializer. don't know why, it's not working on creator constructors, though. have use non-final fields.

public class jsonvaluefiltertest {      private objectmapper mapper;      @beforetest     public void setupobjectmapper() {         mapper = new objectmapper();     }      @test     public void printjson() throws ioexception {         entity myentity = new entity("myname");         mapper.writevalue(system.out, myentity); // prints: {"name":"myname"}     }      @test     public void valuesareuppercasedwhenloaded() throws ioexception {         entity myentity = mapper.readvalue("{\"name\":\"myname\"}", entity.class);         assert.assertequals(myentity.getname(), "myname"); // fails     }      public static class upcaseconverter extends stdconverter<string, string> {         public string convert(string value) {             return value==null ? null : value.touppercase();         }     }      public static class entity {          private string name;          public entity() {}          public entity(string name) {             this.name = name;         }          @jsondeserialize(converter = upcaseconverter.class)         public void setname(string name) {             this.name = name;         }          public string getname() {             return name;         }          @override         public string tostring() {             return "name='" + name + "'";         }     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -