From 1a407204cc25728ce6bb6ff602fdd8d4d5a12119 Mon Sep 17 00:00:00 2001 From: Bucher Arnold Date: Wed, 2 Oct 2024 11:35:38 +0200 Subject: [PATCH] #557 add custom configuration to jsonb --- .../coffee/tool/utils/json/JsonbUtil.java | 43 ++++++++++++++++++- docs/en/common/core/coffee-tool.adoc | 4 ++ docs/en/migration/migration280to290.adoc | 1 + docs/hu/common/core/coffee-tool.adoc | 4 ++ docs/hu/migration/migration280to290.adoc | 1 + 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/coffee-tool/src/main/java/hu/icellmobilsoft/coffee/tool/utils/json/JsonbUtil.java b/coffee-tool/src/main/java/hu/icellmobilsoft/coffee/tool/utils/json/JsonbUtil.java index bf40e7c5e..21863fa3a 100644 --- a/coffee-tool/src/main/java/hu/icellmobilsoft/coffee/tool/utils/json/JsonbUtil.java +++ b/coffee-tool/src/main/java/hu/icellmobilsoft/coffee/tool/utils/json/JsonbUtil.java @@ -23,8 +23,10 @@ import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Optional; import jakarta.json.bind.Jsonb; @@ -38,6 +40,7 @@ import jakarta.json.bind.serializer.JsonbDeserializer; import jakarta.json.bind.serializer.JsonbSerializer; +import org.apache.commons.lang3.BooleanUtils; import org.eclipse.microprofile.config.Config; import hu.icellmobilsoft.coffee.tool.jsonb.adapter.ByteArrayJsonbAdapter; @@ -75,6 +78,9 @@ * {@value JsonbUtil#CUSTOM_DESERIALIZERS}: * - "your.custom.JsonbDeserializer1" * - "your.custom.JsonbDeserializer2" + * {@value JsonbUtil#CUSTOM_PROPERTIES}: + * - "jsonb.other-config-parameter1#value1" + * - "jsonb.other-config-parameter2#value2" * * * @@ -151,9 +157,13 @@ public class JsonbUtil { * Config property for custom deserializers classes */ private static final String CUSTOM_DESERIALIZERS = JSONB_CONFIG_PREFIX + "customDeserializers"; + /** + * Config property for custom properties values + */ + private static final String CUSTOM_PROPERTIES = JSONB_CONFIG_PREFIX + "customProperties"; /** - * Config property name + * Config property names */ private static final String JSONB_PROPERTY_NAME_FAIL_ON_UNKNOWN_PROPERTIES = "jsonb.fail-on-unknown-properties"; @@ -185,7 +195,10 @@ public static Jsonb getContext() { .withAdapters(getAdapters(config)) .withSerializers(getSerializers(config)) .withDeserializers(getDeserializers(config)); - jsonbConfig.setProperty(JSONB_PROPERTY_NAME_FAIL_ON_UNKNOWN_PROPERTIES, getFailOnUnknownProperties(config)); + + // add custom property configurations from config + getCustomProperties(config).forEach(jsonbConfig::setProperty); + return JsonbBuilder.newBuilder().withConfig(jsonbConfig).build(); } @@ -236,6 +249,31 @@ private static JsonbDeserializer[] getDeserializers(Config config) { return jsonbDeserializers.toArray(new JsonbDeserializer[0]); } + private static Map getCustomProperties(Config config) { + Map customProperties = new HashMap<>(); + + // add default custom property + customProperties.put(JSONB_PROPERTY_NAME_FAIL_ON_UNKNOWN_PROPERTIES, getFailOnUnknownProperties(config)); + + Optional> customPropertiesConfig = config.getOptionalValues(CUSTOM_PROPERTIES, String.class); + if (customPropertiesConfig.isPresent()) { + // add custom property from config + for (String customProperty : customPropertiesConfig.get()) { + String[] configValue = customProperty.split("#"); + String key = configValue[0]; + String value = configValue[1]; + if (BooleanUtils.toBooleanObject(value) != null) { + // jsonb requires to pass boolean as config value + customProperties.put(key, BooleanUtils.toBoolean(value)); + } else { + customProperties.put(key, value); + } + } + } + + return customProperties; + } + @SuppressWarnings("unchecked") private static T getCustomClassInstance(String customClassName) { try { @@ -292,4 +330,5 @@ private static String getDateFormat(Config config) { private static Locale getLocale(Config config) { return config.getOptionalValue(LOCALE, String.class).map(Locale::forLanguageTag).orElse(Locale.getDefault()); } + } diff --git a/docs/en/common/core/coffee-tool.adoc b/docs/en/common/core/coffee-tool.adoc index 8af7009fa..e99857bf2 100644 --- a/docs/en/common/core/coffee-tool.adoc +++ b/docs/en/common/core/coffee-tool.adoc @@ -64,6 +64,9 @@ coffee: customDeserializers: <14> - "your.custom.JsonbDeserializer1" - "your.custom.JsonbDeserializer2" + customProperties: <15> + - "jsonb.other-config-parameter1#value1" + - "jsonb.other-config-parameter2#value2" ---- In the above configuration, the following elements can be set: @@ -82,6 +85,7 @@ In the above configuration, the following elements can be set: <12> Custom JsonbAdapter classes for additional control over JSON processing. <13> Custom JsonbSerializer classes for serializing specific types. <14> Custom JsonbDeserializer classes for deserializing specific types. +<15> Custom jsonb config parameters, key-values are separated by # The values mention above in the example are the default values, these are used in case of missing configuration. diff --git a/docs/en/migration/migration280to290.adoc b/docs/en/migration/migration280to290.adoc index 4254053ec..de84c8e79 100644 --- a/docs/en/migration/migration280to290.adoc +++ b/docs/en/migration/migration280to290.adoc @@ -69,6 +69,7 @@ Gson library has been replaced with Eclipse Yasson JSONB implementation, due to - coffee.jsonb.config.customAdapters - coffee.jsonb.config.customSerializers - coffee.jsonb.config.customDeserializers +- coffee.jsonb.config.customProperties *JsonB related changes:* diff --git a/docs/hu/common/core/coffee-tool.adoc b/docs/hu/common/core/coffee-tool.adoc index 06cf3372f..c8cf9f49c 100644 --- a/docs/hu/common/core/coffee-tool.adoc +++ b/docs/hu/common/core/coffee-tool.adoc @@ -64,6 +64,9 @@ coffee: customDeserializers: <14> - "your.custom.JsonbDeserializer1" - "your.custom.JsonbDeserializer2" + customProperties: <15> + - "jsonb.other-config-parameter1#value1" + - "jsonb.other-config-parameter2#value2" ---- A fenti beállításokban a következő értékek adhatóak meg: @@ -82,6 +85,7 @@ A fenti beállításokban a következő értékek adhatóak meg: <12> Egyedi JsonbAdapter osztályok <13> Egyedi JsonbSerializer osztályok <14> Egyedi JsonbDeserializer osztályok +<15> Egyedi jsonb config paraméterek, az értékkészlet # jellel van elválaszva A példában megjelölt értékek az alapértelmezett értékei, amennyiben nincs külön konfiguráció megadva, ezeket az értékeket fogja használni. diff --git a/docs/hu/migration/migration280to290.adoc b/docs/hu/migration/migration280to290.adoc index 90e396156..3447fc25b 100644 --- a/docs/hu/migration/migration280to290.adoc +++ b/docs/hu/migration/migration280to290.adoc @@ -69,6 +69,7 @@ Gson library le lett cserélve Eclipse Yasson JSONB implementációra egy ismert - coffee.jsonb.config.customAdapters - coffee.jsonb.config.customSerializers - coffee.jsonb.config.customDeserializers +- coffee.jsonb.config.customProperties *JsonB-vel kapcsolatos változások:*