From e83c5745757a3e5c1ba84a7dc53f7730c0416250 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 14 Dec 2021 21:39:40 +1300 Subject: [PATCH 1/2] #340 - [api] Add support for 'pre-encoded keys' --- api/src/main/java/jakarta/json/spi/JsonProvider.java | 11 +++++++++++ .../main/java/jakarta/json/stream/JsonGenerator.java | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/api/src/main/java/jakarta/json/spi/JsonProvider.java b/api/src/main/java/jakarta/json/spi/JsonProvider.java index 3cfbee65..2b6cfd30 100644 --- a/api/src/main/java/jakarta/json/spi/JsonProvider.java +++ b/api/src/main/java/jakarta/json/spi/JsonProvider.java @@ -237,6 +237,17 @@ private static void checkPackageAccess(String className) { */ public abstract JsonGeneratorFactory createGeneratorFactory(Map config); + /** + * Creates a generator key for the given JSON name. + * The {@code JsonGenerator.Key} is immutable and can be held and reused. + * This generator key is optimised for use to writing keys via {@code JsonGenerator} + * by being escaped and encoded when compared to using {@code String} keys. + * + * @param key The JSON name + * @return The JSON generator key + */ + public abstract JsonGenerator.Key createGeneratorKey(String key); + /** * Creates a JSON reader from a character stream. * diff --git a/api/src/main/java/jakarta/json/stream/JsonGenerator.java b/api/src/main/java/jakarta/json/stream/JsonGenerator.java index 3fbc4b7a..ba7a2c1e 100644 --- a/api/src/main/java/jakarta/json/stream/JsonGenerator.java +++ b/api/src/main/java/jakarta/json/stream/JsonGenerator.java @@ -540,4 +540,16 @@ public interface JsonGenerator extends Flushable, /*Auto*/Closeable { @Override void flush(); + /** + * A JSON name that can be optimised for writing by a {@code JsonGenerator}. + */ + interface Key { + + /** + * Return the key as escaped character array. + * + * @return the key in escaped character array. + */ + char[] toCharArray(); + } } From 9e82fe2eee03178e2a7ae73e65ee9ecf7b133a01 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 14 Dec 2021 22:21:22 +1300 Subject: [PATCH 2/2] #344 Add JsonGenerator.writeKey() method --- api/src/main/java/jakarta/json/stream/JsonGenerator.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/src/main/java/jakarta/json/stream/JsonGenerator.java b/api/src/main/java/jakarta/json/stream/JsonGenerator.java index ba7a2c1e..46615730 100644 --- a/api/src/main/java/jakarta/json/stream/JsonGenerator.java +++ b/api/src/main/java/jakarta/json/stream/JsonGenerator.java @@ -192,6 +192,14 @@ public interface JsonGenerator extends Flushable, /*Auto*/Closeable { */ JsonGenerator writeKey(String name); + /** + * Write the JSON name with a colon. + * + * @param name name of json field + * @return this generator + */ + JsonGenerator writeKey(Key name); + /** * Writes the JSON start array character. It starts a new child array * context within which JSON values can be written to the array. This