JsonEntityProvider implements EntityProvider<T> { @SuppressWarnings("unchecked") @Override public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { JsonValue jsonValue; if (t instanceof JsonValue) { jsonValue = (JsonValue)t; } else { Types jType = JsonUtils.getType(type); if (jType == Types.ARRAY_BOOLEAN || jType == Types.ARRAY_BYTE || jType == Types.ARRAY_SHORT || jType == Types.ARRAY_INT || jType == Types.ARRAY_LONG || jType == Types.ARRAY_FLOAT || jType == Types.ARRAY_DOUBLE || jType == Types.ARRAY_CHAR || jType == Types.ARRAY_STRING || jType == Types.ARRAY_OBJECT) { jsonValue = JsonGenerator.createJsonArray(t); } else if (jType == Types.COLLECTION) { jsonValue = JsonGenerator.createJsonArray((Collection<?>)t); } else if (jType == Types.MAP) { jsonValue = JsonGenerator.createJsonObjectFromMap((Map<String, ?>)t); } else { jsonValue = JsonGenerator.createJsonObject(t); } } JsonWriter jsonWriter = new JsonWriter(entityStream); jsonValue.writeTo(jsonWriter); jsonWriter.flush(); } catch (JsonException e) { LOG.debug(e.getMessage(), e); throw new IOException(String.format("Can't write to output stream. %s", e.getMessage()), 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 writesJsonValueToOutputStream() throws Exception { ObjectValue jsonBook = createJsonBook(createJunitBook()); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonEntityProvider.writeTo(jsonBook, Book.class, null, null, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), out); assertEquals("{\"author\":\"Vincent Massol\",\"title\":\"JUnit in Action\",\"pages\":386,\"price\":19.37,\"isdn\":93011099534534}", out.toString()); }
@Test public void writesObjectToOutputStream() throws Exception { Book book = createJunitBook(); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonEntityProvider.writeTo(book, Book.class, null, null, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), out); String outputAsString = out.toString(); assertTrue(outputAsString.contains("\"author\":\"Vincent Massol\"")); assertTrue(outputAsString.contains("\"title\":\"JUnit in Action\"")); assertTrue(outputAsString.contains("\"pages\":386")); assertTrue(outputAsString.contains("\"price\":19.37")); assertTrue(outputAsString.contains("\"isdn\":93011099534534")); }
@Test public void writesArrayOfObjectsToOutputStream() throws Exception { Book[] books = new Book[]{createJunitBook()}; ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonEntityProvider.writeTo(books, Book[].class, null, null, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), out); String outputAsString = out.toString(); assertTrue(outputAsString.startsWith("[")); assertTrue(outputAsString.endsWith("]")); assertTrue(outputAsString.contains("\"author\":\"Vincent Massol\"")); assertTrue(outputAsString.contains("\"title\":\"JUnit in Action\"")); assertTrue(outputAsString.contains("\"pages\":386")); assertTrue(outputAsString.contains("\"price\":19.37")); assertTrue(outputAsString.contains("\"isdn\":93011099534534")); }
@Test public void writesCollectionOfObjectsToOutputStream() throws Exception { List<Book> books = newArrayList(createJunitBook()); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonEntityProvider.writeTo(books, List.class, newParameterizedType(List.class, Book.class), null, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), out); String outputAsString = out.toString(); assertTrue(outputAsString.startsWith("[")); assertTrue(outputAsString.endsWith("]")); assertTrue(outputAsString.contains("\"author\":\"Vincent Massol\"")); assertTrue(outputAsString.contains("\"title\":\"JUnit in Action\"")); assertTrue(outputAsString.contains("\"pages\":386")); assertTrue(outputAsString.contains("\"price\":19.37")); assertTrue(outputAsString.contains("\"isdn\":93011099534534")); }
@Test public void writesMapStringToObjectToOutputStream() throws Exception { Map<String, Book> books = ImmutableMap.of("1", createJunitBook()); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonEntityProvider.writeTo(books, Map.class, newParameterizedType(Map.class, String.class, Book.class), null, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), out); String outputAsString = out.toString(); assertTrue(outputAsString.startsWith("{\"1\":{")); assertTrue(outputAsString.endsWith("}")); assertTrue(outputAsString.contains("\"author\":\"Vincent Massol\"")); assertTrue(outputAsString.contains("\"title\":\"JUnit in Action\"")); assertTrue(outputAsString.contains("\"pages\":386")); assertTrue(outputAsString.contains("\"price\":19.37")); assertTrue(outputAsString.contains("\"isdn\":93011099534534")); }