Skip to content

Commit

Permalink
Avoid instantiating too many JsonProviders
Browse files Browse the repository at this point in the history
See quarkusio/quarkus#42748 for more details.
  • Loading branch information
gsmet committed Nov 6, 2024
1 parent 74638c4 commit b7a0d32
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
import java.util.Map;
import java.util.Set;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonNumber;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;

public class JsonUtils {

private static final JsonProvider JSON_PROVIDER = JsonProvider.provider();

private JsonUtils() {
}

public static JsonObject replaceMap(Map<String, Object> map) {
JsonObjectBuilder builder = Json.createObjectBuilder();
JsonObjectBuilder builder = JSON_PROVIDER.createObjectBuilder();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object entryValue = entry.getValue();
if (entryValue instanceof Map) {
Expand All @@ -48,7 +50,7 @@ public static JsonObject replaceMap(Map<String, Object> map) {
}

private static JsonArray toJsonArray(Collection<?> collection) {
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
JsonArrayBuilder arrayBuilder = JSON_PROVIDER.createArrayBuilder();

for (Object element : collection) {
if (element instanceof String) {
Expand All @@ -71,11 +73,11 @@ public static JsonValue wrapValue(Object value) {
// This may already be a JsonValue
jsonValue = (JsonValue) value;
} else if (value instanceof String) {
jsonValue = Json.createValue(value.toString());
jsonValue = JSON_PROVIDER.createValue(value.toString());
} else if ((value instanceof Long) || (value instanceof Integer)) {
jsonValue = Json.createValue(((Number) value).longValue());
jsonValue = JSON_PROVIDER.createValue(((Number) value).longValue());
} else if (value instanceof Number) {
jsonValue = Json.createValue(((Number) value).doubleValue());
jsonValue = JSON_PROVIDER.createValue(((Number) value).doubleValue());
} else if (value instanceof Boolean) {
jsonValue = (Boolean) value ? JsonValue.TRUE : JsonValue.FALSE;
} else if (value instanceof Collection) {
Expand Down

0 comments on commit b7a0d32

Please sign in to comment.