@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()); }
@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); } }
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); } } }
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); } }  }
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); }
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);  }