Make JsonGetter Case Insensitive

I am using JacksonAnnotation along with the Spring Framework to parse the JSON that I get from the web service for my application.

I have the same data structure coming from two different methods, but in one of them there is a field that happens to be uppercase. Because of this, I do not want to create two data structures.

Is there a way for me to make the JsonGetter case insensitive, or at least accept two versions of a string?

Currently I have to use this for method A

@JsonGetter("CEP") public String getCEP() { return this.cep; } 

and this is for method B

 @JsonGetter("CEP") public String getCEP() { return this.cep; } 

thanks

+4
source share
1 answer

You can create a new setter method for each property name option:

 import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonProgram { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.readValue("{\"Cep\":\"value\"}", Entity.class)); System.out.println(mapper.readValue("{\"CEP\":\"value\"}", Entity.class)); } } class Entity { private String cep; public String getCep() { return cep; } @JsonSetter("Cep") public void setCep(String cep) { this.cep = cep; } @JsonSetter("CEP") public void setCepCapitalized(String cep) { this.cep = cep; } @Override public String toString() { return "Entity [cep=" + cep + "]"; } } 
+3
source

Source: https://habr.com/ru/post/1491177/


All Articles