2

I use ObjectMapper like:

MyObject object = new ObjectMapper().readValue(jsonString, MyObject.class);

MyObject has hundred fields and they all are capitalized.

I don't want to put @JsonProperty(value = "CapitalizedFieldName") on each field as it is already named that way. I tried to set a setter also in capitalized way but it didn't do anything. Only fields with @JsonProperty annotation are getting serialized.

Is there some global annotation I could use on the class?

Kamil
  • 1,456
  • 4
  • 32
  • 50

2 Answers2

2

You can use:

objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

You can find more details here.

HRgiger
  • 2,750
  • 26
  • 37
  • It returns me: `com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token` – Kamil Feb 09 '18 at 14:20
  • @Kamil can u add your MyObject java and exception stacktrace. In basic you should see instack trace what is failing, something like this: at [Source: line: 1, column: 1095] (through reference chain: JsonGen["platforms"]) – HRgiger Feb 09 '18 at 14:23
  • Yeah, you're right. There was types error. I've got String instead of String[]. – Kamil Feb 09 '18 at 14:26
  • glad it solved! feel free to select as answer:) – HRgiger Feb 09 '18 at 14:27
0

You can achieve this by using Gson Library from Google and by creating a custom deserializer.

For Example : Consider an Employee class to which you want to deserialize into.

  public class Employee {
        private String name;
        private String cell;
        public Employee(String name, String cell) {
            super();
            this.name = name;
            this.cell = cell;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCell() {
            return cell;
        }
        public void setCell(String cell) {
            this.cell = cell;
        }
       }

Write a custom deserializer as :

public class CapsDeserializer implements JsonDeserializer<Employee> {

    @Override
    public Employee deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2)
            throws JsonParseException {
        JsonObject jobject = json.getAsJsonObject();

        try {
            Employee emp = new Employee(
                    jobject.get(Employee.class.getDeclaredField("name").getName().toUpperCase()).getAsString(),
                    jobject.get(Employee.class.getDeclaredField("cell").getName().toUpperCase()).getAsString());
            return emp;
        } catch (NoSuchFieldException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

Note : the field name are accessed by reflection and then converted into caps. Since the field names of the class are in small letters while the inputted json has field names in caps.

Sample program to execute :

public class GsonExampleApplication {

    public static void main(String[] args) {
        Gson gson = new Gson();

        Employee test = new Employee("", "");
        test.setCell("1234567891");
        test.setName("abhishek");

        System.out.println("" + gson.toJson(test));

        String json = "{\"NAME\":\"abhishek\",\"CELL\":\"1234567891\"}";
        FieldNamingStrategy f = gson.fieldNamingStrategy();

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Employee.class, new CapsDeserializer());
        Gson cgson = gsonBuilder.create();

        Employee obj = cgson.fromJson(json, Employee.class);

        System.out.println(obj.getName() + " " + obj.getCell());
    }
}
Abhishek
  • 15
  • 4
  • I'm already aware of Gson. As you can see I asked about Jackson's ObjectMapper but thanks for your input. Maybe someone will benefit from it – Kamil Feb 12 '18 at 07:24