Generic Method to Convert JSON Array String to List in java

I've been trying to create a utility for my project that lets you make List<DesiredTypeClass> from JSON String of the same.

Following is my current Implementation:

public static <T> List<?> stringToClassList(String data, Class<T> convertType, ObjectMapper mapper) {
    if (!StringUtils.isBlank(data)) {
        try {
            if (mapper.readTree(data).size() != 0) {
                return Arrays.asList(mapper.convertValue(mapper.readTree(data), Array.newInstance(convertType, 0).getClass()));
            }
        }
        catch (JsonProcessingException processingException) {
            LOGGER.error("Error coverting String to Json, Error: {}", processingException);
            throw new UnrecoverableRequestException(processingException);
        }
     }
     return new ArrayList<>();
}

To call this method, you should have: arrayString, a classType (eg: use MyDto.class to get List) and an ObjectMapper instance;

Writing a test case around this, I found and extra array braces in result:

java.lang.AssertionError: 
Expected :[{"startDate":"2020-12-01","endDate":"2020-12-02"},{"startDate":"2020-12-10","endDate":"2020-12-11"}]
Actual   :[[{"startDate":"2020-12-01","endDate":"2020-12-02"},{"startDate":"2020-12-10","endDate":"2020-12-11"}]]

Here's a simplified version of the failed test:

@Test
public void testStringToClassList() {
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode firstNode = new ObjectNode(JsonNodeFactory.instance);
    firstNode.set("startDate", JsonNodeFactory.instance.textNode("2020-12-01"));
    firstNode.set("endDate", JsonNodeFactory.instance.textNode("2020-12-02"));
    ObjectNode secondNode = new ObjectNode(JsonNodeFactory.instance);
    secondNode.set("startDate", JsonNodeFactory.instance.textNode("2020-12-10"));
    secondNode.set("endDate", JsonNodeFactory.instance.textNode("2020-12-11"));
    ArrayNode arrayNode = new ArrayNode(JsonNodeFactory.instance);
    arrayNode.add(firstNode);
    arrayNode.add(secondNode);
    String arrayString = "[{\"startDate\":\"2020-12-01\",\"endDate\":\"2020-12-02\"},{\"startDate\":\"2020-12-10\",\"endDate\":\"2020-12-11\"}]";
    List<?> objectList = ObjectNodeUtils.stringToClassList(arrayString, Object.class, objectMapper);
    Assert.assertEquals(arrayNode, objectMapper.valueToTree(objectList));
}

I'm understanding that problem has to do with:

Arrays.asList(mapper.convertValue(mapper.readTree(data), Array.newInstance(convertType, 0).getClass()))

Any ideas on how to get a list here? (I'm not very used to writing generic code but this seemed like a good candidate)



Read more here: https://stackoverflow.com/questions/64947963/generic-method-to-convert-json-array-string-to-list-in-java

Content Attribution

This content was originally published by Prashant Vishwakarma at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: