Search code examples
javajsonjacksonfasterxml

Jackson Databinding


We are using an API to communicate between our services. To bind the data I use the following dependencies:

        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.5</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml</groupId>
        <artifactId>jackson-xml-databind</artifactId>
        <version>0.6.2</version>
    </dependency>

The old way the API offered a list to me :

country: [
   "NL",
   "BE",
   "ES",
   "GB",         
],

Normally i bind this to my pojos as followed: e.g.

@JsonProperty("country")
private List<String> countries;

which was not causing any problems.

Now our API is being updated and data is showed in this way for example:

registeredIn: {
     datatype: "SS",
     item: {
        NL: "NL",
        BE: "BE",
        ES: "ES",
        GB: "GB"
        }
    },

I only need this part of the information of the object:

   item: {
        NL: "NL",
        BE: "BE",
        ES: "ES",
        GB: "GB"
        }

The problem is now the list isn't a string list anymore but it has become a list with objects that has the countrycode in it. Is there an easy way to still get the string values as a list? Doing it the way I always did with the annotations?


Solution

  • Try using a Map<String, String> instead of a List<String>. It seems a bit redundant to use the same value as the key for the Map, but it should serialize the way you want it to.