JsonTypeConverter { public Object convertToValue(JsonNode jsonValue, ValueFields valueFields) { Object convertedValue = jsonValue; if (jsonValue != null && StringUtils.isNotBlank(javaClassFieldForJackson)) { JsonNode classNode = jsonValue.get(javaClassFieldForJackson); try { if (classNode != null) { final String type = classNode.asText(); convertedValue = convertToType(jsonValue, type); } else if (valueFields.getTextValue2() != null && !jsonValue.getClass().getName().equals(valueFields.getTextValue2())) { convertedValue = convertToType(jsonValue, valueFields.getTextValue2()); } } catch (ClassNotFoundException e) { LOGGER .warn("Unable to obtain type for json variable object " + valueFields.getName(), e); } } return convertedValue; } JsonTypeConverter(ObjectMapper objectMapper, String javaClassFieldForJackson); Object convertToValue(JsonNode jsonValue, ValueFields valueFields);  }
@Test public void should_convertToList() throws Exception { List<Integer> originalValue = asList(1, 2); String json = objectMapper.writeValueAsString(originalValue); JsonNode jsonNode = objectMapper.readTree(json); System.out.println(json); ValueFields valueFields = buildValueFields("numbers", originalValue); Object numbers = converter.convertToValue(jsonNode, valueFields); assertThat(numbers).isInstanceOf(List.class); assertThat(((List<?>) numbers).get(0)).isInstanceOf(Integer.class); assertThat(numbers).isEqualTo(originalValue); }
@Test public void should_convertToPOJO() throws Exception { Person person = new Person("John", "Doe"); String json = objectMapper.writeValueAsString(person); JsonNode jsonNode = objectMapper.readTree(json); Object convertedValue = converter.convertToValue(jsonNode, buildValueFields("person", person)); assertThat(convertedValue).isInstanceOf(Person.class); assertThat(((Person) convertedValue).getFirstName()).isEqualTo("John"); assertThat(((Person) convertedValue).getLastName()).isEqualTo("Doe"); }