diff --git a/avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java b/avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java index 8f7d9d4c..ba4250bc 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java +++ b/avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java @@ -493,16 +493,6 @@ public URI uri() { return exchange.getRequestURI(); } - @Override - public void write(byte[] bytes) { - try (var os = exchange.getResponseBody()) { - exchange.sendResponseHeaders(statusCode(), bytes.length == 0 ? -1 : bytes.length); - os.write(bytes); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - @Override public void write(byte[] bufferBytes, int length) { try (var os = exchange.getResponseBody()) { diff --git a/avaje-jex/src/main/java/io/avaje/jex/http/Context.java b/avaje-jex/src/main/java/io/avaje/jex/http/Context.java index 6ce09f8f..c4ed3756 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/http/Context.java +++ b/avaje-jex/src/main/java/io/avaje/jex/http/Context.java @@ -21,8 +21,10 @@ import com.sun.net.httpserver.HttpExchange; import io.avaje.jex.core.Constants; +import io.avaje.jex.core.json.JsonbOutput; import io.avaje.jex.security.BasicAuthCredentials; import io.avaje.jex.security.Role; +import io.avaje.jsonb.JsonType; /** Provides access to functions for handling the request and response. */ public interface Context { @@ -245,6 +247,16 @@ default Context headers(Map headers) { */ void json(Object bean); + /** + * Optimized json write using avaje jsonb + * + * @param jsonType the serializer for the value. + * @param value the pojo to serialize + */ + default void jsonb(JsonType jsonType, T value) { + jsonType.toJson(value, JsonbOutput.of(this.contentType(ContentType.APPLICATION_JSON))); + } + /** * Write the stream as a JSON stream with new line delimiters {@literal * application/x-json-stream}. @@ -447,10 +459,12 @@ default String userAgent() { * * @param bytes The byte array to write. */ - void write(byte[] bytes); + default void write(byte[] bytes) { + write(bytes, bytes.length); + } /** - * Writes the first bytes from this buffer directly to the response. + * Writes the given length of bytes from this buffer directly to the response. * *

The bytes written will be from position 0 to length. *