Search in sources :

Example 16 with JsonGenerator

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());
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Book(org.ecabrerar.examples.libraryapp.domain.Book) JsonGenerator(javax.json.stream.JsonGenerator)

Example 17 with JsonGenerator

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());
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) JsonGenerator(javax.json.stream.JsonGenerator)

Example 18 with JsonGenerator

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();
}
Also used : JsonGenerator(javax.json.stream.JsonGenerator)

Aggregations

JsonGenerator (javax.json.stream.JsonGenerator)18 StringWriter (java.io.StringWriter)9 HashMap (java.util.HashMap)4 JsonGeneratorFactory (javax.json.stream.JsonGeneratorFactory)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 Map (java.util.Map)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 AuthOpException (digilib.auth.AuthOpException)1 DigilibServletRequest (digilib.conf.DigilibServletRequest)1 ImageOpException (digilib.image.ImageOpException)1 DocuDirectory (digilib.io.DocuDirectory)1 FileOpException (digilib.io.FileOpException)1 ImageInput (digilib.io.ImageInput)1 ImageSet (digilib.io.ImageSet)1 ImageSize (digilib.util.ImageSize)1 FileOutputStream (java.io.FileOutputStream)1 Writer (java.io.Writer)1