Jackson Exception when parsing Yaml with a Map of List values

I have a Yaml document that includes map entries that correspond to lists. Here's an example:

environments:
  qa: [ 'us-east-1' ]
  staging: [ 'us-east-1', 'us-west-2' ]

I am using Jackson 2.3.2 to parse the document into a class using the following field & accessors:

private Map<String, List<String>> environments = new HashMap<String, List<String>>();

@JsonProperty
public Map<String, List<String>> getEnvironments() {
    return environments;
}

@JsonProperty
public void setEnvironments(Map<String, List<String>> environments) {
    this.environments = environments;
}

I'm testing the reading of the Yaml file with the following:

inputStream = new FileInputStream("src/test/resources/configuration_test.yml");
config = mapper.readValue(inputStream, MyConfiguration.class);

The following exception is thrown with a stacktrace that's not particularly helpful:

Caused by: java.lang.IllegalArgumentException: Can not find a deserializer for non-concrete Collection type [collection type; class com.google.common.collect.ImmutableList, contains [simple type, class io.dropwizard.metrics.ReporterFactory]]
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.createCollectionDeserializer(BasicDeserializerFactory.java:814)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:392)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:350)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:263)
... 42 more

Any idea how to get Jackson to parse the Map value as a List?

Answers


You need to properly configure your ObjectMapper. In dropwizard, there is Jackson.newObjectMapper()


I had the same problem extending dropwizard's configuration class. As CAB mentioned, using Jackson.newObjectMapper instead of new ObjectMapper solved the issue. Now my test looks like this:

ObjectMapper mapper = Jackson.newObjectMapper(new YAMLFactory());

MyConfiguration myConfig = mapper.readValue(fixture("fixtures/configuration.yml"), MyConfiguration.class);

// Asserts

PS: I am using io.dropwizard.testing.FixtureHelpers.fixture to fetch the YAML file.


This was actually due to my binding class extending from the DropWizard 'Configuration' class. Parsing the Yaml mentioned in the issue worked fine when using a binding class that did not extended from the DropWizard 'Configuration' class.

It's worth noting that the exception occurred only when run in a JUnit test (TDD, of course), not when running the DropWizard application. The exception did not occur when the Yaml file was read during normal application startup.


Need Your Help

How to choose between layouts using if else?

android layout if-statement

I am trying to switch between two layouts based on the if else statement.I dont know where I went wrong. Anyone please help.

response to the tab in primefaces very slow

jsf-2

I have 4 tabs in my jsf page. But when I click on the one of the tab, the response is very slow to get the actual page on that tab. Any idea on this part?