Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#340 - [api] Add support for 'pre-encoded keys' #344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/src/main/java/jakarta/json/spi/JsonProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ private static void checkPackageAccess(String className) {
*/
public abstract JsonGeneratorFactory createGeneratorFactory(Map<String, ?> 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.
*
Expand Down
20 changes: 20 additions & 0 deletions api/src/main/java/jakarta/json/stream/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -540,4 +548,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();
}
}