-1

I have some Pojo classes which are generated through avro schema. The problem is that the field name in the schema is declared with the first character as a capital letter. I am receiving a JSON string and in a JSON string also each key name starts with a capital letter, due to which Jackson is not able to map the data.

Is there any way we can override the logic of the Jackson parser to find the getter/setter in pojo with just get + FieldName and set + FieldName.

Bellow is the sample POJO class and JSON which I am trying to parse.

POJO:

public class Sample {
    private java.lang.CharSequence Field1;
    private java.lang.CharSequence Field2;
    private java.lang.CharSequence Field3;

    public java.lang.CharSequence getField1() {
        return Field1;
    }

    public void setField1(java.lang.CharSequence Field1) {
        this.Field1 = Field1;
    }

    public java.lang.CharSequence getField2() {
        return Field2;
    }

    public void setField2(java.lang.CharSequence Field2) {
        this.Field2 = Field2;
    }

    public java.lang.CharSequence getField3() {
        return Field3;
    }

    public void setField3(java.lang.CharSequence Field3) {
        this.Field3 = Field3;
    }
}

JSON:

{
  "Field1": "Value1",
  "Field2": "Value2",
  "Field3": "Value3"
}

Code to parse the JSON:

String json = = "";
ObjectMapper objectMapper = new ObjectMapper();
Sample sample = objectMapper.readValue(json, Sample.class);

If I pass the below JSON then it's working perfectly fine.

{
  "field1": "Value1",
  "field2": "Value2",
  "field3": "Value3"
}

But I am receiving the JSON in the previous format and I don't want to write a code to convert each JSON key's first character into lowercase (which would still create an issue for some fields where more than one starting letter is capital).

Deepak Kumar
  • 1,246
  • 14
  • 38

1 Answers1

0

You should be able to utilise Jackson's MixIn feature to implement an abstract class or interface that provides getter methods that are seen in the Sample class. Then you can apply @JsonProperty annotations to the interface getter methods and apply the MixIn to your objectMapper. You can take a look here. See also

You could try:

public interface SampleMixIn {
    @JsonProperty("Field1")
    CharSequence getField1();
    @JsonProperty("Field2")
    CharSequence getField2();
    @JsonProperty("Field3")
    CharSequence getField3();
}

Then when you call your object mapper:

mapper.addMixIn(Sample.class, SampleMixIn.class);
mapper.readValue(json, Sample.class); //or whatever other method you want to call to map/read

You can allow the mapping of incoming JSON to be case insensitive using the following feature (only available in Jackson 2.5 and after). See also this thread which gives other possible solutions.

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); 
  • note, I havent tried to call it with the `.readValue()` method but I have used it with `.convertValue()` earlier today and since I m not able to access my computer atm, you would have to do some tweaking – experiment unit 1998X Jul 06 '23 at 05:59
  • Though, the mix-in annotations should only affect the serialization and deserialization behavior of the ObjectMapper when converting Java objects to JSON and vice versa. So readValue() should be fine – experiment unit 1998X Jul 06 '23 at 06:16
  • As I mentioned, these classes are auto-generated on the build. And the actual JSON is way too complex and it contains more than 50 POJO classes. Creating those many Interfaces might not be a good idea as maintainability will become an issue. – Deepak Kumar Jul 06 '23 at 17:36
  • Then you will have to write your own custom serializer and deserializer to serialize and deserialize your JSON/POJO – experiment unit 1998X Jul 07 '23 at 01:45
  • @DeepakKumar though you can give this a shot. This seems to be the solution to your problem. That is to use case insensitive deserialization of your fields – experiment unit 1998X Jul 07 '23 at 01:51