Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Cache JsonProvider.provider() result as per javadoc recommendation #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 36 additions & 35 deletions api/src/main/java/javax/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
* threads.
*/
public final class Json {

static final JsonProvider PROVIDER = JsonProvider.provider();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this would actually decrease performance with default implementation if JsonProvider is cached by client application (as it is supposed to). See

private final BufferPool bufferPool = new BufferPoolImpl();

When JsonProvider instance is cached like this buffer pool exists only once. Because it uses ConcurrentLinkedQueue it will decrease performance in multithreaded environment because of synchronization.

Copy link
Member

@keilw keilw Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I certainly see dangers making it final. That way for the lifetime of the Json class in a particular JVM it would remain the same even if the underlying provider may change (less likely without OSGi but possible for some implementations)
Keeping a static cache, maybe, as long as there's at least a protected or similar way to reset it, see how we did in JSR 363 with ServiceProvider or CDI.
I am not convinced we still need this in Java EE 8, maybe leave to a future (Jakarta EE) version of JSON-P?


private Json() {
}

Expand All @@ -92,7 +93,7 @@ private Json() {
* @return a JSON parser
*/
public static JsonParser createParser(Reader reader) {
return JsonProvider.provider().createParser(reader);
return PROVIDER.createParser(reader);
}

/**
Expand All @@ -106,7 +107,7 @@ public static JsonParser createParser(Reader reader) {
* @return a JSON parser
*/
public static JsonParser createParser(InputStream in) {
return JsonProvider.provider().createParser(in);
return PROVIDER.createParser(in);
}

/**
Expand All @@ -116,7 +117,7 @@ public static JsonParser createParser(InputStream in) {
* @return a JSON generator
*/
public static JsonGenerator createGenerator(Writer writer) {
return JsonProvider.provider().createGenerator(writer);
return PROVIDER.createGenerator(writer);
}

/**
Expand All @@ -126,7 +127,7 @@ public static JsonGenerator createGenerator(Writer writer) {
* @return a JSON generator
*/
public static JsonGenerator createGenerator(OutputStream out) {
return JsonProvider.provider().createGenerator(out);
return PROVIDER.createGenerator(out);
}

/**
Expand All @@ -135,7 +136,7 @@ public static JsonGenerator createGenerator(OutputStream out) {
* @return JSON parser factory.
*
public static JsonParserFactory createParserFactory() {
return JsonProvider.provider().createParserFactory();
return PROVIDER.createParserFactory();
}
*/

Expand All @@ -150,7 +151,7 @@ public static JsonParserFactory createParserFactory() {
* @return JSON parser factory
*/
public static JsonParserFactory createParserFactory(Map<String, ?> config) {
return JsonProvider.provider().createParserFactory(config);
return PROVIDER.createParserFactory(config);
}

/**
Expand All @@ -159,7 +160,7 @@ public static JsonParserFactory createParserFactory(Map<String, ?> config) {
* @return JSON generator factory
*
public static JsonGeneratorFactory createGeneratorFactory() {
return JsonProvider.provider().createGeneratorFactory();
return PROVIDER.createGeneratorFactory();
}
*/

Expand All @@ -175,7 +176,7 @@ public static JsonGeneratorFactory createGeneratorFactory() {
*/
public static JsonGeneratorFactory createGeneratorFactory(
Map<String, ?> config) {
return JsonProvider.provider().createGeneratorFactory(config);
return PROVIDER.createGeneratorFactory(config);
}

/**
Expand All @@ -187,7 +188,7 @@ public static JsonGeneratorFactory createGeneratorFactory(
* @return a JSON writer
*/
public static JsonWriter createWriter(Writer writer) {
return JsonProvider.provider().createWriter(writer);
return PROVIDER.createWriter(writer);
}

/**
Expand All @@ -200,7 +201,7 @@ public static JsonWriter createWriter(Writer writer) {
* @return a JSON writer
*/
public static JsonWriter createWriter(OutputStream out) {
return JsonProvider.provider().createWriter(out);
return PROVIDER.createWriter(out);
}

/**
Expand All @@ -210,7 +211,7 @@ public static JsonWriter createWriter(OutputStream out) {
* @return a JSON reader
*/
public static JsonReader createReader(Reader reader) {
return JsonProvider.provider().createReader(reader);
return PROVIDER.createReader(reader);
}

/**
Expand All @@ -222,7 +223,7 @@ public static JsonReader createReader(Reader reader) {
* @return a JSON reader
*/
public static JsonReader createReader(InputStream in) {
return JsonProvider.provider().createReader(in);
return PROVIDER.createReader(in);
}

/**
Expand All @@ -236,7 +237,7 @@ public static JsonReader createReader(InputStream in) {
* @return a JSON reader factory
*/
public static JsonReaderFactory createReaderFactory(Map<String, ?> config) {
return JsonProvider.provider().createReaderFactory(config);
return PROVIDER.createReaderFactory(config);
}

/**
Expand All @@ -250,7 +251,7 @@ public static JsonReaderFactory createReaderFactory(Map<String, ?> config) {
* @return a JSON writer factory
*/
public static JsonWriterFactory createWriterFactory(Map<String, ?> config) {
return JsonProvider.provider().createWriterFactory(config);
return PROVIDER.createWriterFactory(config);
}

/**
Expand All @@ -259,7 +260,7 @@ public static JsonWriterFactory createWriterFactory(Map<String, ?> config) {
* @return a JSON array builder
*/
public static JsonArrayBuilder createArrayBuilder() {
return JsonProvider.provider().createArrayBuilder();
return PROVIDER.createArrayBuilder();
}

/**
Expand All @@ -271,7 +272,7 @@ public static JsonArrayBuilder createArrayBuilder() {
* @since 1.1
*/
public static JsonArrayBuilder createArrayBuilder(JsonArray array) {
return JsonProvider.provider().createArrayBuilder(array);
return PROVIDER.createArrayBuilder(array);
}

/**
Expand All @@ -287,7 +288,7 @@ public static JsonArrayBuilder createArrayBuilder(JsonArray array) {
* @since 1.1
*/
public static JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
return JsonProvider.provider().createArrayBuilder(collection);
return PROVIDER.createArrayBuilder(collection);
}

/**
Expand All @@ -296,7 +297,7 @@ public static JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
* @return a JSON object builder
*/
public static JsonObjectBuilder createObjectBuilder() {
return JsonProvider.provider().createObjectBuilder();
return PROVIDER.createObjectBuilder();
}

/**
Expand All @@ -308,7 +309,7 @@ public static JsonObjectBuilder createObjectBuilder() {
* @since 1.1
*/
public static JsonObjectBuilder createObjectBuilder(JsonObject object) {
return JsonProvider.provider().createObjectBuilder(object);
return PROVIDER.createObjectBuilder(object);
}

/**
Expand All @@ -324,7 +325,7 @@ public static JsonObjectBuilder createObjectBuilder(JsonObject object) {
* @since 1.1
*/
public static JsonObjectBuilder createObjectBuilder(Map<String, Object> map) {
return JsonProvider.provider().createObjectBuilder(map);
return PROVIDER.createObjectBuilder(map);
}

/**
Expand All @@ -343,7 +344,7 @@ public static JsonObjectBuilder createObjectBuilder(Map<String, Object> map) {
* @since 1.1
*/
public static JsonPointer createPointer(String jsonPointer) {
return JsonProvider.provider().createPointer(jsonPointer);
return PROVIDER.createPointer(jsonPointer);
}

/**
Expand All @@ -354,7 +355,7 @@ public static JsonPointer createPointer(String jsonPointer) {
* @since 1.1
*/
public static JsonPatchBuilder createPatchBuilder() {
return JsonProvider.provider().createPatchBuilder();
return PROVIDER.createPatchBuilder();
}

/**
Expand All @@ -368,7 +369,7 @@ public static JsonPatchBuilder createPatchBuilder() {
* @since 1.1
*/
public static JsonPatchBuilder createPatchBuilder(JsonArray array) {
return JsonProvider.provider().createPatchBuilder(array);
return PROVIDER.createPatchBuilder(array);
}

/**
Expand All @@ -381,7 +382,7 @@ public static JsonPatchBuilder createPatchBuilder(JsonArray array) {
* @since 1.1
*/
public static JsonPatch createPatch(JsonArray array) {
return JsonProvider.provider().createPatch(array);
return PROVIDER.createPatch(array);
}

/**
Expand All @@ -396,7 +397,7 @@ public static JsonPatch createPatch(JsonArray array) {
* @since 1.1
*/
public static JsonPatch createDiff(JsonStructure source, JsonStructure target) {
return JsonProvider.provider().createDiff(source, target);
return PROVIDER.createDiff(source, target);
}

/**
Expand All @@ -409,7 +410,7 @@ public static JsonPatch createDiff(JsonStructure source, JsonStructure target) {
* @since 1.1
*/
public static JsonMergePatch createMergePatch(JsonValue patch) {
return JsonProvider.provider().createMergePatch(patch);
return PROVIDER.createMergePatch(patch);
}

/**
Expand All @@ -424,7 +425,7 @@ public static JsonMergePatch createMergePatch(JsonValue patch) {
* @since 1.1
*/
public static JsonMergePatch createMergeDiff(JsonValue source, JsonValue target) {
return JsonProvider.provider().createMergeDiff(source, target);
return PROVIDER.createMergeDiff(source, target);
}

/**
Expand All @@ -440,7 +441,7 @@ public static JsonMergePatch createMergeDiff(JsonValue source, JsonValue target)
*/
public static JsonBuilderFactory createBuilderFactory(
Map<String, ?> config) {
return JsonProvider.provider().createBuilderFactory(config);
return PROVIDER.createBuilderFactory(config);
}

/**
Expand All @@ -452,7 +453,7 @@ public static JsonBuilderFactory createBuilderFactory(
* @since 1.1
*/
public static JsonString createValue(String value) {
return JsonProvider.provider().createValue(value);
return PROVIDER.createValue(value);
}

/**
Expand All @@ -464,7 +465,7 @@ public static JsonString createValue(String value) {
* @since 1.1
*/
public static JsonNumber createValue(int value) {
return JsonProvider.provider().createValue(value);
return PROVIDER.createValue(value);
}

/**
Expand All @@ -476,7 +477,7 @@ public static JsonNumber createValue(int value) {
* @since 1.1
*/
public static JsonNumber createValue(long value) {
return JsonProvider.provider().createValue(value);
return PROVIDER.createValue(value);
}

/**
Expand All @@ -488,7 +489,7 @@ public static JsonNumber createValue(long value) {
* @since 1.1
*/
public static JsonNumber createValue(double value) {
return JsonProvider.provider().createValue(value);
return PROVIDER.createValue(value);
}

/**
Expand All @@ -500,7 +501,7 @@ public static JsonNumber createValue(double value) {
* @since 1.1
*/
public static JsonNumber createValue(BigDecimal value) {
return JsonProvider.provider().createValue(value);
return PROVIDER.createValue(value);
}

/**
Expand All @@ -512,7 +513,7 @@ public static JsonNumber createValue(BigDecimal value) {
* @since 1.1
*/
public static JsonNumber createValue(BigInteger value) {
return JsonProvider.provider().createValue(value);
return PROVIDER.createValue(value);
}

/**
Expand Down