[New] Added StrolchElementToJsonVisitor.withListParametersAsArray()

This commit is contained in:
Robert von Burg 2022-03-06 20:01:42 +01:00
parent 7bece7451e
commit 04e5167f60
2 changed files with 28 additions and 0 deletions

View File

@ -55,6 +55,7 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
private boolean withoutPolicies;
private boolean withoutStateVariables;
private boolean withoutValueChanges;
private boolean withListParametersAsArray;
private int activityDepth = Integer.MAX_VALUE;
public StrolchElementToJsonVisitor() {
@ -101,6 +102,10 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
return this.withoutValueChanges;
}
public boolean isWithListParametersAsArray() {
return this.withListParametersAsArray;
}
public StrolchElementToJsonVisitor withLocator() {
this.withLocator = true;
return this;
@ -156,6 +161,11 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
return this;
}
public StrolchElementToJsonVisitor withListParametersAsArray() {
this.withListParametersAsArray = true;
return this;
}
public StrolchElementToJsonVisitor activityDepth(int depth) {
this.activityDepth = depth;
return this;
@ -641,6 +651,19 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
rootJ.addProperty(paramId, (Boolean) param.getValue());
} else if (type.isNumber()) {
rootJ.addProperty(paramId, (Number) param.getValue());
} else if (this.withListParametersAsArray && type.isList()) {
JsonArray valuesJ = switch (type) {
case FLOAT_LIST -> ((FloatListParameter) param).streamValues()
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
case INTEGER_LIST -> ((IntegerListParameter) param).streamValues()
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
case STRING_LIST -> ((StringListParameter) param).streamValues()
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
case LONG_LIST -> ((LongListParameter) param).streamValues()
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
default -> throw new IllegalStateException("Unhandle list type " + type);
};
rootJ.add(paramId, valuesJ);
} else {
rootJ.addProperty(paramId, param.getValueAsString());
}

View File

@ -104,6 +104,11 @@ public class StrolchRootElementToJsonVisitor implements StrolchRootElementVisito
return this;
}
public StrolchRootElementToJsonVisitor withListParametersAsArray() {
this.visitor.withListParametersAsArray();
return this;
}
public StrolchRootElementToJsonVisitor withPolicies() {
this.visitor.withPolicies();
return this;