[New] Allow for optional parameters in to flat json
This commit is contained in:
parent
47d56d6845
commit
6d65064117
|
@ -23,6 +23,10 @@ import li.strolch.utils.collections.MapOfSets;
|
||||||
* empty set with the bag id.
|
* empty set with the bag id.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
|
* <p>
|
||||||
|
* Optional values are handled similar, but only a parameter can be optional, not a whole bag
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
|
@ -30,13 +34,16 @@ import li.strolch.utils.collections.MapOfSets;
|
||||||
public class FromFlatJsonVisitor<T extends StrolchRootElement> {
|
public class FromFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
|
|
||||||
private MapOfSets<String, String> ignoredKeys;
|
private MapOfSets<String, String> ignoredKeys;
|
||||||
|
private MapOfSets<String, String> optionalKeys;
|
||||||
|
|
||||||
public FromFlatJsonVisitor() {
|
public FromFlatJsonVisitor() {
|
||||||
this.ignoredKeys = new MapOfSets<>();
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
this.optionalKeys = new MapOfSets<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FromFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
|
public FromFlatJsonVisitor(MapOfSets<String, String> ignoredParams) {
|
||||||
this.ignoredKeys = new MapOfSets<>();
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
this.optionalKeys = new MapOfSets<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ignoreBag(String bagId) {
|
public void ignoreBag(String bagId) {
|
||||||
|
@ -47,14 +54,18 @@ public class FromFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
this.ignoredKeys.addElement(bagId, paramId);
|
this.ignoredKeys.addElement(bagId, paramId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void optionalParameter(String bagId, String paramId) {
|
||||||
|
this.optionalKeys.addElement(bagId, paramId);
|
||||||
|
}
|
||||||
|
|
||||||
public void visit(T element, JsonObject jsonObject) {
|
public void visit(T element, JsonObject jsonObject) {
|
||||||
|
|
||||||
Set<String> bagKeySet = element.getParameterBagKeySet();
|
Set<String> bagKeySet = element.getParameterBagKeySet();
|
||||||
for (String bagId : bagKeySet) {
|
for (String bagId : bagKeySet) {
|
||||||
|
|
||||||
// see if we have to ignore this bag i.e. empty set existing
|
// see if we have to ignore this bag i.e. empty set existing
|
||||||
Set<String> paramIds = this.ignoredKeys.getSet(bagId);
|
Set<String> ignoredParamIds = this.ignoredKeys.getSet(bagId);
|
||||||
if (paramIds != null && paramIds.isEmpty())
|
if (ignoredParamIds != null && ignoredParamIds.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ParameterBag parameterBag = element.getParameterBag(bagId);
|
ParameterBag parameterBag = element.getParameterBag(bagId);
|
||||||
|
@ -63,12 +74,15 @@ public class FromFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
for (String paramId : parameterKeySet) {
|
for (String paramId : parameterKeySet) {
|
||||||
|
|
||||||
// see if this parameter must be ignored
|
// see if this parameter must be ignored
|
||||||
if (paramIds != null && paramIds.contains(paramId))
|
if (ignoredParamIds != null && ignoredParamIds.contains(paramId))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
JsonElement jsonElement = jsonObject.get(paramId);
|
JsonElement jsonElement = jsonObject.get(paramId);
|
||||||
if (jsonElement == null)
|
if (jsonElement == null) {
|
||||||
|
if (this.optionalKeys.containsElement(bagId, paramId))
|
||||||
|
continue;
|
||||||
throw new StrolchModelException("JsonObject is missing member with ID " + paramId);
|
throw new StrolchModelException("JsonObject is missing member with ID " + paramId);
|
||||||
|
}
|
||||||
|
|
||||||
if (!jsonElement.isJsonPrimitive()) {
|
if (!jsonElement.isJsonPrimitive()) {
|
||||||
throw new StrolchModelException("JsonElement " + paramId + " is not a json primitive but a "
|
throw new StrolchModelException("JsonElement " + paramId + " is not a json primitive but a "
|
||||||
|
|
|
@ -69,8 +69,8 @@ public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
for (String bagId : bagKeySet) {
|
for (String bagId : bagKeySet) {
|
||||||
|
|
||||||
// see if we have to ignore this bag i.e. empty set existing
|
// see if we have to ignore this bag i.e. empty set existing
|
||||||
Set<String> paramIds = this.ignoredKeys.getSet(bagId);
|
Set<String> ignoredParamIds = this.ignoredKeys.getSet(bagId);
|
||||||
if (paramIds != null && paramIds.isEmpty())
|
if (ignoredParamIds != null && ignoredParamIds.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ParameterBag parameterBag = element.getParameterBag(bagId);
|
ParameterBag parameterBag = element.getParameterBag(bagId);
|
||||||
|
@ -79,7 +79,7 @@ public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
for (String paramId : parameterKeySet) {
|
for (String paramId : parameterKeySet) {
|
||||||
|
|
||||||
// see if this parameter must be ignored
|
// see if this parameter must be ignored
|
||||||
if (paramIds != null && paramIds.contains(paramId))
|
if (ignoredParamIds != null && ignoredParamIds.contains(paramId))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (jsonObject.has(paramId)) {
|
if (jsonObject.has(paramId)) {
|
||||||
|
|
Loading…
Reference in New Issue