0

the problem is to fill Java object fields both of java standart types(Long, Boolean, etc.) and collections of those from a Map<String, List<String>>.

Inspecting target object's fields via reflecton one can see which field is a Collection and which is not. In second case if Map element has a single value one can use something like BeanUtils to set such fields:

BeanUtils.setProperty(bean, name, stringList.get(0));

But in first case I don't have idea how to determine a type of collection(except of sequentially checking if it is List, Set, Map) and type of it's elements. Simple

BeanUtils.setProperty(bean, name, stringList);

will be valid only if target field type is a List<String>.

But the Jackson library succesfully solves the same problem. For example, consider class:

public static class TestObject {
    private int integerField;
    private String stringField;
    private List<String> stringList;
    private List<Integer> intList;

    ...getters...setters...
}

Now it can be filled with content using Jackson library:

    ObjectMapper mapper = new ObjectMapper();
    TestObject obj = mapper.readValue("{" +
            "\"stringField\": \"stringValue\", " +
            "\"integerField\": 42, " +
            "\"stringList\":[\"1\", \"2\", \"3\"]," +
            "\"intList\":[3, 2, 1]" +
            "}", TestObject.class);

Works perfect. So my question: is there way to use a library such Jackson for solving my problem?

Thanks.

Aeteros
  • 643
  • 1
  • 10
  • 24

0 Answers0