JsonEntityProvider implements EntityProvider<T> { @SuppressWarnings({"unchecked"}) @Override public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { try { JsonParser jsonParser = new JsonParser(); jsonParser.parse(entityStream); JsonValue jsonValue = jsonParser.getJsonObject(); if (JsonValue.class.isAssignableFrom(type)) { return (T)jsonValue; } Types jsonType = JsonUtils.getType(type); if (jsonType == Types.ARRAY_BOOLEAN || jsonType == Types.ARRAY_BYTE || jsonType == Types.ARRAY_SHORT || jsonType == Types.ARRAY_INT || jsonType == Types.ARRAY_LONG || jsonType == Types.ARRAY_FLOAT || jsonType == Types.ARRAY_DOUBLE || jsonType == Types.ARRAY_CHAR || jsonType == Types.ARRAY_STRING || jsonType == Types.ARRAY_OBJECT) { return (T)ObjectBuilder.createArray(type, jsonValue); } if (jsonType == Types.COLLECTION) { return (T)ObjectBuilder.createCollection((Class)type, genericType, jsonValue); } if (jsonType == Types.MAP) { return (T)ObjectBuilder.createObject((Class)type, genericType, jsonValue); } return ObjectBuilder.createObject(type, jsonValue); } catch (JsonException e) { LOG.debug(e.getMessage(), e); throw new IOException(String.format("Can't read from input stream %s", e), e); } }  @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @SuppressWarnings({"unchecked"}) @Override T readFrom(Class<T> type,
                      Type genericType,
                      Annotation[] annotations,
                      MediaType mediaType,
                      MultivaluedMap<String, String> httpHeaders,
                      InputStream entityStream); @Override long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @SuppressWarnings("unchecked") @Override void writeTo(T t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void readsContentOfEntityStreamAsJsonValue() throws Exception { String content = "{\"title\": \"JUnit in Action\", \"pages\": 386, \"isdn\": 93011099534534, \"price\": 19.37, \"author\": \"Vincent Massol\"}"; JsonValue result = (JsonValue)jsonEntityProvider.readFrom(JsonValue.class, null, null, APPLICATION_JSON_TYPE, new MultivaluedMapImpl(), new ByteArrayInputStream(content.getBytes())); assertEquals("Vincent Massol", result.getElement("author").getStringValue()); assertEquals("JUnit in Action", result.getElement("title").getStringValue()); assertEquals(386, result.getElement("pages").getIntValue()); assertEquals(19.37, result.getElement("price").getNumberValue()); assertEquals(93011099534534L, result.getElement("isdn").getLongValue()); }
@Test public void readsContentOfEntityStreamAsObject() throws Exception { String content = "{\"title\": \"JUnit in Action\", \"pages\": 386, \"isdn\": 93011099534534, \"price\": 19.37, \"author\": \"Vincent Massol\"}"; Book result = (Book)jsonEntityProvider.readFrom(Book.class, null, null, APPLICATION_JSON_TYPE, new MultivaluedMapImpl(), new ByteArrayInputStream(content.getBytes())); assertEquals("Vincent Massol", result.getAuthor()); assertEquals("JUnit in Action", result.getTitle()); assertEquals(386, result.getPages()); assertEquals(19.37, result.getPrice(), 0.01); assertEquals(93011099534534L, result.getIsdn()); }
@Test public void readsContentOfEntityStreamAsMapStringToObject() throws Exception { String content = "{\"book\": {\"title\": \"JUnit in Action\", \"pages\": 386, \"isdn\": 93011099534534, \"price\": 19.37, \"author\": \"Vincent Massol\"}}"; Class aClass = Map.class; Type genericType = newParameterizedType(aClass, String.class, Book.class); Map<String, Book> result = (Map<String, Book>)jsonEntityProvider.readFrom(aClass, genericType, null, APPLICATION_JSON_TYPE, new MultivaluedMapImpl(), new ByteArrayInputStream(content.getBytes())); assertEquals(1, result.size()); assertNotNull(result.get("book")); Book book = result.get("book"); assertEquals("Vincent Massol", book.getAuthor()); assertEquals("JUnit in Action", book.getTitle()); assertEquals(386, book.getPages()); assertEquals(19.37, book.getPrice(), 0.01); assertEquals(93011099534534L, book.getIsdn()); }
@Test public void readsContentOfEntityStreamAsCollectionOfObjects() throws Exception { String content = "[{\"title\": \"JUnit in Action\", \"pages\": 386, \"isdn\": 93011099534534, \"price\": 19.37, \"author\": \"Vincent Massol\"}]"; Class aClass = List.class; Type genericType = newParameterizedType(aClass, Book.class); List<Book> result = (List<Book>)jsonEntityProvider.readFrom(aClass, genericType, null, APPLICATION_JSON_TYPE, new MultivaluedMapImpl(), new ByteArrayInputStream(content.getBytes())); assertEquals(1, result.size()); Book book = result.get(0); assertEquals("Vincent Massol", book.getAuthor()); assertEquals("JUnit in Action", book.getTitle()); assertEquals(386, book.getPages()); assertEquals(19.37, book.getPrice(), 0.01); assertEquals(93011099534534L, book.getIsdn()); }
@Test public void readsContentOfEntityStreamAsArrayOfObjects() throws Exception { String content = "[{\"title\": \"JUnit in Action\", \"pages\": 386, \"isdn\": 93011099534534, \"price\": 19.37, \"author\": \"Vincent Massol\"}]"; Class aClass = Book[].class; Book[] result = (Book[])jsonEntityProvider.readFrom(aClass, null, null, APPLICATION_JSON_TYPE, new MultivaluedMapImpl(), new ByteArrayInputStream(content.getBytes())); assertEquals(1, result.length); Book book = result[0]; assertEquals("Vincent Massol", book.getAuthor()); assertEquals("JUnit in Action", book.getTitle()); assertEquals(386, book.getPages()); assertEquals(19.37, book.getPrice(), 0.01); assertEquals(93011099534534L, book.getIsdn()); }
@Test(expected = IOException.class) public void throwsIOExceptionWhenParsingFailed() throws Exception { String invalidContent = "\"title\": \"JUnit in Action\"}"; jsonEntityProvider.readFrom(Book.class, null, null, APPLICATION_JSON_TYPE, new MultivaluedMapImpl(), new ByteArrayInputStream(invalidContent.getBytes())); }