From b7a0d32d4eb9d5b47084ab1c9cb370ae95077e64 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Wed, 6 Nov 2024 12:20:02 +0100 Subject: [PATCH] Avoid instantiating too many JsonProviders See https://github.com/quarkusio/quarkus/issues/42748 for more details. --- .../src/main/java/io/smallrye/jwt/JsonUtils.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/implementation/jwt-auth/src/main/java/io/smallrye/jwt/JsonUtils.java b/implementation/jwt-auth/src/main/java/io/smallrye/jwt/JsonUtils.java index b112529c..2bd3469c 100644 --- a/implementation/jwt-auth/src/main/java/io/smallrye/jwt/JsonUtils.java +++ b/implementation/jwt-auth/src/main/java/io/smallrye/jwt/JsonUtils.java @@ -6,7 +6,6 @@ import java.util.Map; import java.util.Set; -import jakarta.json.Json; import jakarta.json.JsonArray; import jakarta.json.JsonArrayBuilder; import jakarta.json.JsonNumber; @@ -14,14 +13,17 @@ 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 map) { - JsonObjectBuilder builder = Json.createObjectBuilder(); + JsonObjectBuilder builder = JSON_PROVIDER.createObjectBuilder(); for (Map.Entry entry : map.entrySet()) { Object entryValue = entry.getValue(); if (entryValue instanceof Map) { @@ -48,7 +50,7 @@ public static JsonObject replaceMap(Map map) { } private static JsonArray toJsonArray(Collection collection) { - JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); + JsonArrayBuilder arrayBuilder = JSON_PROVIDER.createArrayBuilder(); for (Object element : collection) { if (element instanceof String) { @@ -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) {