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

Use JsonProvider holder #837

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
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