use of javax.json.stream.JsonGenerator in project javaee7-firstcup by ecabrerar.
the class BookCollectionWriter method writeTo.
@Override
public void writeTo(List<Book> books, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
try (StringWriter writer = new StringWriter()) {
try (JsonGenerator generator = Json.createGenerator(writer)) {
HashMap<String, Object> configs = new HashMap<>(1);
configs.put(JsonGenerator.PRETTY_PRINTING, true);
generator.writeStartArray();
books.stream().forEach(book -> generator.writeStartObject().write("Name", book.getName()).write("ISBN", book.getIsbn()).write("Author", book.getAuthor()).writeEnd());
generator.writeEnd();
}
entityStream.write(writer.toString().getBytes());
}
} else if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE)) {
StringBuilder stringBuilder = new StringBuilder("Book ");
books.stream().forEach((Book book) -> stringBuilder.append(book.toString()).append("\n"));
entityStream.write(stringBuilder.toString().getBytes());
}
}
use of javax.json.stream.JsonGenerator in project javaee7-firstcup by ecabrerar.
the class BookWriter method writeTo.
@Override
public void writeTo(Book book, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
try (StringWriter writer = new StringWriter()) {
try (JsonGenerator generator = Json.createGenerator(writer)) {
HashMap<String, Object> configs = new HashMap<>(1);
configs.put(JsonGenerator.PRETTY_PRINTING, true);
generator.writeStartObject().write("Name", book.getName()).write("ISBN", book.getIsbn()).write("Author", book.getAuthor()).writeEnd();
}
entityStream.write(writer.toString().getBytes());
}
} else if (mediaType.equals(MediaType.TEXT_PLAIN_TYPE)) {
String string = "Book ".concat(book.toString()).concat("\n");
entityStream.write(string.getBytes());
}
}
use of javax.json.stream.JsonGenerator in project javaee7-firstcup by ecabrerar.
the class MovieWriter method writeTo.
@Override
public void writeTo(Movie t, Class<?> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, Object> mm, OutputStream out) throws IOException, WebApplicationException {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartObject().write("id", t.getId()).write("name", t.getName()).write("actors", t.getActors()).writeEnd();
gen.flush();
}
Aggregations