You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
currently config files are merged by overwriting null values and by overwriting array values.
in my use case however, array values need to be merged and null values should not override existing values.
example:
rational: configuration files are structured by module. a module may have a property which is not included in another, therefore null value should not override a non null value.
the values of arrays may need to be "collected" over multiple module configurations.
Contribution
JsonObject.java:
public JsonObject mergeIn(JsonObject other, int depth, boolean mergeArrays, boolean overwriteNull) {
if (depth < 1) {
return this;
}
if (depth == 1 && !mergeArrays && overwriteNull) {
map.putAll(other.map);
return this;
}
for (Map.Entry<String, Object> e : other.map.entrySet()) {
if (e.getValue() == null && overwriteNull) {
map.put(e.getKey(), null);
} else if (e.getValue() != null){
map.merge(e.getKey(), e.getValue(), (oldVal, newVal) -> {
if (oldVal instanceof Map) {
oldVal = new JsonObject((Map) oldVal);
}
if (newVal instanceof Map) {
newVal = new JsonObject((Map) newVal);
}
if (mergeArrays && oldVal instanceof JsonArray && newVal instanceof JsonArray)
return ((JsonArray)newVal).addAll((JsonArray)oldVal);
if (oldVal instanceof JsonObject && newVal instanceof JsonObject) {
return ((JsonObject) oldVal).mergeIn((JsonObject) newVal, depth - 1, mergeArrays, overwriteNull);
}
return newVal;
});
}
}
return this;
}
The text was updated successfully, but these errors were encountered:
tsegismont
changed the title
JsonObject and ConfigRetriever: add further options on how to mergeIn JsonObject
JsonObject : add further options on how to mergeIn JsonObject
Jan 21, 2025
Describe the feature
currently config files are merged by overwriting null values and by overwriting array values.
in my use case however, array values need to be merged and null values should not override existing values.
example:
should return:
Use cases
rational: configuration files are structured by module. a module may have a property which is not included in another, therefore null value should not override a non null value.
the values of arrays may need to be "collected" over multiple module configurations.
Contribution
JsonObject.java:
The text was updated successfully, but these errors were encountered: