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