From 18b2c1382d3d5710f2316f2492c4fcaeb44138cb Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Wed, 19 Feb 2025 18:48:05 -0500 Subject: [PATCH 1/3] add jsonType json method --- .../java/io/avaje/jex/core/JdkContext.java | 10 -------- .../main/java/io/avaje/jex/http/Context.java | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 14 deletions(-) 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..8873ed8f 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,17 @@ 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 json(JsonType jsonType, T value) { + contentType(ContentType.APPLICATION_JSON); + jsonType.toJson(value, JsonbOutput.of(this)); + } + /** * Write the stream as a JSON stream with new line delimiters {@literal * application/x-json-stream}. @@ -447,17 +460,19 @@ 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 bytes from this buffer directly to the response. * *

The bytes written will be from position 0 to length. * - * @param bufferBytes The byte array to write. + * @param bytes The byte array to write. * @param length The number of bytes to write from the buffer. */ - void write(byte[] bufferBytes, int length); + void write(byte[] bytes, int length); /** * Writes the content from the given InputStream directly to the response body. From 7b8c7a96810b23cc1351f25c7ef0aef77f51cd4c Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Wed, 19 Feb 2025 18:52:42 -0500 Subject: [PATCH 2/3] Update Context.java --- avaje-jex/src/main/java/io/avaje/jex/http/Context.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 8873ed8f..a8aa9dc7 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 @@ -253,7 +253,7 @@ default Context headers(Map headers) { * @param jsonType the serializer for the value. * @param value the pojo to serialize */ - default void json(JsonType jsonType, T value) { + default void jsonb(JsonType jsonType, T value) { contentType(ContentType.APPLICATION_JSON); jsonType.toJson(value, JsonbOutput.of(this)); } @@ -465,14 +465,14 @@ default void write(byte[] bytes) { } /** - * Writes the 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. * - * @param bytes The byte array to write. + * @param bufferBytes The byte array to write. * @param length The number of bytes to write from the buffer. */ - void write(byte[] bytes, int length); + void write(byte[] bufferBytes, int length); /** * Writes the content from the given InputStream directly to the response body. From 637007754688d9013ef0cf3aa75ca0f32064641e Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Wed, 19 Feb 2025 18:55:34 -0500 Subject: [PATCH 3/3] Update Context.java --- avaje-jex/src/main/java/io/avaje/jex/http/Context.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 a8aa9dc7..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 @@ -254,8 +254,7 @@ default Context headers(Map headers) { * @param value the pojo to serialize */ default void jsonb(JsonType jsonType, T value) { - contentType(ContentType.APPLICATION_JSON); - jsonType.toJson(value, JsonbOutput.of(this)); + jsonType.toJson(value, JsonbOutput.of(this.contentType(ContentType.APPLICATION_JSON))); } /**