step.core.dynamicbeans.DynamicJsonObjectResolver Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (C) 2020, exense GmbH
*
* This file is part of STEP
*
* STEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with STEP. If not, see .
******************************************************************************/
package step.core.dynamicbeans;
import java.util.Map;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonValue;
import step.core.json.JsonProviderCache;
public class DynamicJsonObjectResolver {
DynamicJsonValueResolver valueResolver;
public DynamicJsonObjectResolver(DynamicJsonValueResolver valueResolver) {
super();
this.valueResolver = valueResolver;
}
public JsonObject evaluate(JsonObject o, Map bindings) {
JsonObjectBuilder builder = JsonProviderCache.createObjectBuilder();
if(o!=null) {
for(String key:o.keySet()) {
JsonValue v = o.get(key);
Object result = evaluateJsonValue(v, bindings);
if(result instanceof JsonValue) {
builder.add(key,(JsonValue)result);
} else if(result instanceof Boolean) {
builder.add(key,(Boolean)result);
} else if(result instanceof Integer) {
builder.add(key,(Integer)result);
} else if(result instanceof String) {
builder.add(key,(String)result);
} else {
if(result != null)
builder.add(key,(String)result.toString());
else
builder.add(key, ""); // a more restrictive try-catch of the NPE + throw "Value null for key={key}" might be more useful
}
}
}
return builder.build();
}
private Object evaluateJsonValue(JsonValue v, Map bindings) {
if(v instanceof JsonObject) {
JsonObject jsonObject = (JsonObject) v;
if(jsonObject.containsKey("dynamic")) {
Object evaluate = valueResolver.evaluate(jsonObject, bindings);
if (evaluate instanceof JsonObject) {
return evaluate((JsonObject) evaluate, bindings);
} else if (evaluate instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) evaluate;
return evaluateJsonArray(jsonArray, bindings);
} else {
return evaluate;
}
} else {
return evaluate(jsonObject, bindings);
}
} else if (v instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) v;
return evaluateJsonArray(jsonArray, bindings);
} else {
// in this case we have a primitive, so nothing to do
return v;
}
}
private JsonArray evaluateJsonArray(JsonArray jsonArray, Map bindings) {
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
for(int i=0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy