I'd like to use Jackson to bind a Map<String, String>
to a bean. The pitfall here is that the not all the fields are collections so it doesn't work.
I would need to tweak the ObjectMapper to only bind collections when the corresponding bean property is a collection.
public class MapperTest {
public static class Person {
public String fname;
public Double age;
public List<String> other;
}
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Map<String, String[]> props = new HashMap<>();
props.put("fname", new String[] {"mridang"});
props.put("age", new String[] {"1"});
props.put("other", new String[] {"one", "two"});
mapper.convertValue(props, Person.class);
}
}
The above example doesn't work as Jackson expects all the fields to be collections.
I cannot change the Map structure as this is a legacy system I'm dealing with so I'm pretty much stuck with Map<String, String[]>