Skip to content

Commit

Permalink
Use JsonProvider holder (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
luneo7 authored Nov 15, 2024
1 parent 6f2ed9b commit 0d578de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.smallrye.jwt;

import jakarta.json.spi.JsonProvider;

public final class JsonProviderHolder {

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

private JsonProviderHolder() {
}

public static JsonProvider jsonProvider() {
return JSON_PROVIDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
Expand All @@ -59,6 +58,7 @@
import org.jose4j.jwk.OctetSequenceJsonWebKey;
import org.jose4j.jwk.PublicJsonWebKey;

import io.smallrye.jwt.JsonProviderHolder;
import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm;
import io.smallrye.jwt.algorithm.SignatureAlgorithm;

Expand Down Expand Up @@ -452,7 +452,7 @@ public static List<JsonWebKey> loadJsonWebKeys(String content) {
JWTUtilLogging.log.loadingJwks();

JsonObject jwks = null;
try (JsonReader reader = Json.createReader(new StringReader(content))) {
try (JsonReader reader = JsonProviderHolder.jsonProvider().createReader(new StringReader(content))) {
jwks = reader.readObject();
} catch (Exception ex) {
JWTUtilLogging.log.loadingJwksFailed(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
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_PROVIDER.createObjectBuilder();
JsonObjectBuilder builder = JsonProviderHolder.jsonProvider().createObjectBuilder();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object entryValue = entry.getValue();
if (entryValue instanceof Map) {
Expand All @@ -50,7 +47,7 @@ public static JsonObject replaceMap(Map<String, Object> map) {
}

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

for (Object element : collection) {
if (element instanceof String) {
Expand All @@ -73,11 +70,11 @@ public static JsonValue wrapValue(Object value) {
// This may already be a JsonValue
jsonValue = (JsonValue) value;
} else if (value instanceof String) {
jsonValue = JSON_PROVIDER.createValue(value.toString());
jsonValue = JsonProviderHolder.jsonProvider().createValue(value.toString());
} else if ((value instanceof Long) || (value instanceof Integer)) {
jsonValue = JSON_PROVIDER.createValue(((Number) value).longValue());
jsonValue = JsonProviderHolder.jsonProvider().createValue(((Number) value).longValue());
} else if (value instanceof Number) {
jsonValue = JSON_PROVIDER.createValue(((Number) value).doubleValue());
jsonValue = JsonProviderHolder.jsonProvider().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 0d578de

Please sign in to comment.