Skip to content

Commit

Permalink
#557 add custom configuration to jsonb
Browse files Browse the repository at this point in the history
  • Loading branch information
bucherarnold committed Oct 2, 2024
1 parent 3e81532 commit 1a40720
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"
*
* </pre>
*
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -236,6 +249,31 @@ private static JsonbDeserializer[] getDeserializers(Config config) {
return jsonbDeserializers.toArray(new JsonbDeserializer[0]);
}

private static Map<String, Object> getCustomProperties(Config config) {
Map<String, Object> customProperties = new HashMap<>();

// add default custom property
customProperties.put(JSONB_PROPERTY_NAME_FAIL_ON_UNKNOWN_PROPERTIES, getFailOnUnknownProperties(config));

Optional<List<String>> 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> T getCustomClassInstance(String customClassName) {
try {
Expand Down Expand Up @@ -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());
}

}
4 changes: 4 additions & 0 deletions docs/en/common/core/coffee-tool.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down
1 change: 1 addition & 0 deletions docs/en/migration/migration280to290.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:*

Expand Down
4 changes: 4 additions & 0 deletions docs/hu/common/core/coffee-tool.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down
1 change: 1 addition & 0 deletions docs/hu/migration/migration280to290.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:*

Expand Down

0 comments on commit 1a40720

Please sign in to comment.