[New] Allow to not map name on ToFlatJsonVisitor
This commit is contained in:
parent
e27a0969ee
commit
1779bcc99f
|
@ -1,5 +1,7 @@
|
||||||
package li.strolch.model.json;
|
package li.strolch.model.json;
|
||||||
|
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import li.strolch.model.activity.Activity;
|
import li.strolch.model.activity.Activity;
|
||||||
|
@ -11,4 +13,28 @@ public class ActivityToFlatJsonVisitor extends ToFlatJsonVisitor<Activity> imple
|
||||||
public JsonObject visit(Activity element) {
|
public JsonObject visit(Activity element) {
|
||||||
return toJson(element);
|
return toJson(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActivityToFlatJsonVisitor withoutElementName() {
|
||||||
|
super.withoutElementName();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActivityToFlatJsonVisitor setHook(BiConsumer<Activity, JsonObject> hook) {
|
||||||
|
super.setHook(hook);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActivityToFlatJsonVisitor ignoreBag(String bagId) {
|
||||||
|
super.ignoreBag(bagId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActivityToFlatJsonVisitor ignoreParameter(String bagId, String paramId) {
|
||||||
|
super.ignoreParameter(bagId, paramId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package li.strolch.model.json;
|
package li.strolch.model.json;
|
||||||
|
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import li.strolch.model.Order;
|
import li.strolch.model.Order;
|
||||||
|
@ -11,4 +13,28 @@ public class OrderToFlatJsonVisitor extends ToFlatJsonVisitor<Order> implements
|
||||||
public JsonObject visit(Order element) {
|
public JsonObject visit(Order element) {
|
||||||
return toJson(element);
|
return toJson(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrderToFlatJsonVisitor withoutElementName() {
|
||||||
|
super.withoutElementName();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrderToFlatJsonVisitor setHook(BiConsumer<Order, JsonObject> hook) {
|
||||||
|
super.setHook(hook);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrderToFlatJsonVisitor ignoreBag(String bagId) {
|
||||||
|
super.ignoreBag(bagId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrderToFlatJsonVisitor ignoreParameter(String bagId, String paramId) {
|
||||||
|
super.ignoreParameter(bagId, paramId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package li.strolch.model.json;
|
package li.strolch.model.json;
|
||||||
|
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import li.strolch.model.Resource;
|
import li.strolch.model.Resource;
|
||||||
|
@ -11,4 +13,28 @@ public class ResourceToFlatJsonVisitor extends ToFlatJsonVisitor<Resource> imple
|
||||||
public JsonObject visit(Resource element) {
|
public JsonObject visit(Resource element) {
|
||||||
return toJson(element);
|
return toJson(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResourceToFlatJsonVisitor withoutElementName() {
|
||||||
|
super.withoutElementName();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResourceToFlatJsonVisitor setHook(BiConsumer<Resource, JsonObject> hook) {
|
||||||
|
super.setHook(hook);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResourceToFlatJsonVisitor ignoreBag(String bagId) {
|
||||||
|
super.ignoreBag(bagId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResourceToFlatJsonVisitor ignoreParameter(String bagId, String paramId) {
|
||||||
|
super.ignoreParameter(bagId, paramId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,7 @@ public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
|
|
||||||
private MapOfSets<String, String> ignoredKeys;
|
private MapOfSets<String, String> ignoredKeys;
|
||||||
private BiConsumer<T, JsonObject> hook;
|
private BiConsumer<T, JsonObject> hook;
|
||||||
|
private boolean withoutElementName;
|
||||||
|
|
||||||
public ToFlatJsonVisitor() {
|
public ToFlatJsonVisitor() {
|
||||||
this.ignoredKeys = new MapOfSets<>();
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
|
@ -54,16 +55,24 @@ public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
this.ignoredKeys = new MapOfSets<>();
|
this.ignoredKeys = new MapOfSets<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHook(BiConsumer<T, JsonObject> hook) {
|
public ToFlatJsonVisitor<T> withoutElementName() {
|
||||||
|
this.withoutElementName = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ToFlatJsonVisitor<T> setHook(BiConsumer<T, JsonObject> hook) {
|
||||||
this.hook = hook;
|
this.hook = hook;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ignoreBag(String bagId) {
|
public ToFlatJsonVisitor<T> ignoreBag(String bagId) {
|
||||||
this.ignoredKeys.addSet(bagId, Collections.emptySet());
|
this.ignoredKeys.addSet(bagId, Collections.emptySet());
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ignoreParameter(String bagId, String paramId) {
|
public ToFlatJsonVisitor<T> ignoreParameter(String bagId, String paramId) {
|
||||||
this.ignoredKeys.addElement(bagId, paramId);
|
this.ignoredKeys.addElement(bagId, paramId);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject toJson(T element) {
|
public JsonObject toJson(T element) {
|
||||||
|
@ -71,6 +80,7 @@ public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
JsonObject jsonObject = new JsonObject();
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
|
||||||
jsonObject.addProperty(ID, element.getId());
|
jsonObject.addProperty(ID, element.getId());
|
||||||
|
if (!this.withoutElementName)
|
||||||
jsonObject.addProperty(NAME, element.getName());
|
jsonObject.addProperty(NAME, element.getName());
|
||||||
jsonObject.addProperty(TYPE, element.getType());
|
jsonObject.addProperty(TYPE, element.getType());
|
||||||
jsonObject.addProperty(OBJECT_TYPE, element.getClass().getSimpleName());
|
jsonObject.addProperty(OBJECT_TYPE, element.getClass().getSimpleName());
|
||||||
|
@ -93,7 +103,8 @@ public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (jsonObject.has(paramId)) {
|
if (jsonObject.has(paramId)) {
|
||||||
throw new StrolchModelException("JsonObject already has a member with ID " + paramId);
|
throw new StrolchModelException(
|
||||||
|
"JsonObject already has a member with ID " + paramId + ": " + parameterBag.getLocator());
|
||||||
}
|
}
|
||||||
|
|
||||||
Parameter<?> param = parameterBag.getParameter(paramId);
|
Parameter<?> param = parameterBag.getParameter(paramId);
|
||||||
|
|
Loading…
Reference in New Issue