1

I know how to use @JsonProperty in jackson API but the below case is different.

I've below json snippet.

{"results":[{"uniqueCount":395}

So, to parse with jackson API the above json, I've written below java pojo class.

package com.jl.models;

import lombok.Data;

@Data
public class Results
{
    private int uniqueCount;

}

Later, I got to parse below similar json snippet.

{"results":[{"count":60}

Now, the problem here is I'm unable to parse this json with Results class as it expects a string uniqueCount.

I can easily create another java pojo class having count member variable but I've to create all the parent java classes having instance of Results class.

So, is there any way I can customize Results class having lombok behaviour to parse both the json without impacting each others?

Thanks for your help in advance.

harshavmb
  • 3,404
  • 3
  • 21
  • 55
  • You can get rid of `Results` and just use a `List`, then only `{"results":[395]}` would be enough isn't it? – buræquete Apr 06 '17 at 12:42
  • okay.. Thanks for this suggestion. Have heavily used `results` object in frontend to parse the json. Now, I've to rewrite in all the locations. Is there a way I can do by appending results while generating json so as to avoid making changes in frontend? – harshavmb Apr 06 '17 at 12:46
  • You can have another `Results2` class with `count` field, and use either of these classes when deserializing, then map to the original `Results` if you get the second type. Though I'd prefer you to use my original suggestion, since the field `uniqueCount` adds nothing to your structure but trouble, it seems. Or this -> http://stackoverflow.com/a/19566131/3641067 (looks horrible imo) – buræquete Apr 06 '17 at 12:48
  • yes, as I said earlier, creating a new class is easy but I'll have to rewrite all the parent classes which has an instance of `Results` class and I don't want to do as it leads to code duplication just because of 1 field. – harshavmb Apr 06 '17 at 12:51
  • Possible duplicate of [Different names of JSON property during serialization and deserialization](http://stackoverflow.com/questions/8560348/different-names-of-json-property-during-serialization-and-deserialization) – buræquete Apr 06 '17 at 12:53
  • nope, it is not duplicate. It is different. The example shared is just shown how to read the json field having different name than the variable in java. – harshavmb Apr 06 '17 at 12:57
  • @bureaquete, before marking this as duplicate, you should validate whether it follows the same pattern. Not sure how you are relating to the one you posted. I've already gone through that and it doesn't solve my issue. – harshavmb Apr 06 '17 at 13:00
  • It is duplicate, if you use the answer of that question, that works exactly for your case, both `count` & `uniqueCount` from JSON can map to `uniqueCount` field. https://i.snag.gy/hOBirM.jpg & https://i.snag.gy/F5dYgQ.jpg Obviously the `@JsonProperty` for `uniqueCount` is redundant. Just using lombok @Data plus count setter should be enough for you. – buræquete Apr 06 '17 at 13:16
  • LOL mate, alright, sorry for trying to be helpful. Enjoy – buræquete Apr 06 '17 at 13:28

1 Answers1

3

You can use Jackson's @JsonAnySetter annotation to direct all unknown keys to one method and there you can do the assignment yourself:

@Data
public class Results
{
    private int uniqueCount;

    // all unknown properties will go here
    @JsonAnySetter
    public void setUnknownProperty(String key, Object value) {
        if (key.equals("count")) {
            uniqueCount = (Integer)value;
        }
    }
}
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47