From e83c5745757a3e5c1ba84a7dc53f7730c0416250 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 14 Dec 2021 21:39:40 +1300 Subject: [PATCH] #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(); + } }