JsonUtils { public static <T> List<T> unwrapRootToList(final Class<T> clazz, final String jsonString) throws OpenstackJsonSerializationException { List<T> list = null; try { final ObjectMapper mapper = createDefaultDeserializationMapper(); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); final JsonNode readTree = mapper.readTree(jsonString); if (readTree != null && readTree.getElements().hasNext()) { final JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); JsonNode next = readTree.getElements().next(); if (!"[0]".equals(next.toString())) { list = mapper.readValue(next, type); } } } catch (final Exception e) { throw new OpenstackJsonSerializationException(e); } return list; } private  JsonUtils(); static T mapJsonToObject(final Class<T> clazz, final String jsonString); static T mapJsonToObject(final Class<T> clazz, final String jsonString, final boolean translateCamelCase); static T unwrapRootToObject(final Class<T> clazz, final String jsonString); static T unwrapRootToObject(final Class<T> clazz, final String jsonString,
			final boolean translateCamelCase); static List<T> unwrapRootToList(final Class<T> clazz, final String jsonString); static String toJson(final Object javabean); static String toJson(final Object javabean, final boolean translateCamelCase);  }
@Test public void testUnwrapRootToList() throws Exception { List<Network> list = JsonUtils.unwrapRootToList(Network.class, "{\"networks\":[{\"name\":\"test\"}]}"); Assert.assertNotNull(list); Assert.assertEquals(1, list.size()); Assert.assertEquals("test", list.get(0).getName()); list = JsonUtils.unwrapRootToList(Network.class, "{\"networks\": [0]}"); Assert.assertNull(list); }
@Test public void testPortsResponse() throws Exception { File file = new File("./src/test/resources/openstack/mappings/ports-response.json"); String jsonString = FileUtils.readFileToString(file); List<Port> ports = JsonUtils.unwrapRootToList(Port.class, jsonString); Assert.assertNotNull(ports); Assert.assertFalse(ports.isEmpty()); for (Port port : ports) { Assert.assertNotNull(port.getFixedIps()); Assert.assertFalse(port.getFixedIps().isEmpty()); Assert.assertNotNull(port.getFixedIps().get(0).getIpAddress()); } }