Commit 87d69cdf authored by Jean Meurice's avatar Jean Meurice

Automated Json

parent 391e23d6
Pipeline #307348 passed with stage
in 43 seconds
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<groupId>montisim</groupId> <groupId>montisim</groupId>
<artifactId>commons</artifactId> <artifactId>commons</artifactId>
<version>2.0.2</version> <version>2.0.3</version>
<properties> <properties>
......
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.commons.boundingbox; package de.rwth.montisim.commons.boundingbox;
import de.rwth.montisim.commons.utils.JsonTraverser; import de.rwth.montisim.commons.utils.json.Typed;
import de.rwth.montisim.commons.utils.JsonTraverser.ObjectIterable;
import de.rwth.montisim.commons.utils.JsonWriter;
@Typed("aabb")
public class AABB implements BoundingBox { public class AABB implements BoundingBox {
public static final String TYPE_NAME = "aabb"; // public static final String TYPE_NAME = "aabb";
@Override // @Override
public void toJson(JsonWriter j) { // public void toJson(JsonWriter j) {
// TODO Auto-generated method stub // // TODO Auto-generated method stub
} // }
@Override // @Override
public void fromJson(JsonTraverser j) { // public void fromJson(JsonTraverser j) {
// TODO Auto-generated method stub // // TODO Auto-generated method stub
} // }
@Override // @Override
public void fromJson(JsonTraverser j, ObjectIterable it) { // public void fromJson(JsonTraverser j, ObjectIterable it) {
// TODO Auto-generated method stub // // TODO Auto-generated method stub
} // }
} }
\ No newline at end of file
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.commons.boundingbox; package de.rwth.montisim.commons.boundingbox;
import de.rwth.montisim.commons.utils.JsonSerializable; public interface BoundingBox {
import de.rwth.montisim.commons.utils.JsonTraverser; // void fromJson(JsonTraverser j, ObjectIterable it);
import de.rwth.montisim.commons.utils.Pair;
import de.rwth.montisim.commons.utils.JsonTraverser.ObjectIterable;
public interface BoundingBox extends JsonSerializable { // public static BoundingBox buildFromJson(JsonTraverser j){
void fromJson(JsonTraverser j, ObjectIterable it); // Pair<String, ObjectIterable> p = j.getStructureType();
// BoundingBox bbox = getBoundingBoxFromType(p.getKey());
// bbox.fromJson(j, p.getValue());
// return bbox;
// }
public static BoundingBox buildFromJson(JsonTraverser j){ // public static BoundingBox getBoundingBoxFromType(String type){
Pair<String, ObjectIterable> p = j.getStructureType(); // if (type.equals(OBB.TYPE_NAME)){
BoundingBox bbox = getBoundingBoxFromType(p.getKey()); // return new OBB();
bbox.fromJson(j, p.getValue()); // } else if (type.equals(AABB.TYPE_NAME)){
return bbox; // return new AABB();
} // } else if (type.equals(Sphere.TYPE_NAME)){
// return new Sphere();
public static BoundingBox getBoundingBoxFromType(String type){ // } else throw new IllegalArgumentException("Unknown BoundingBox type: "+type);
if (type.equals(OBB.TYPE_NAME)){ // }
return new OBB();
} else if (type.equals(AABB.TYPE_NAME)){
return new AABB();
} else if (type.equals(Sphere.TYPE_NAME)){
return new Sphere();
} else throw new IllegalArgumentException("Unknown BoundingBox type: "+type);
}
} }
\ No newline at end of file
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.commons.boundingbox; package de.rwth.montisim.commons.boundingbox;
import de.rwth.montisim.commons.utils.JsonTraverser;
import de.rwth.montisim.commons.utils.JsonWriter;
import de.rwth.montisim.commons.utils.Mat3; import de.rwth.montisim.commons.utils.Mat3;
import de.rwth.montisim.commons.utils.Vec3; import de.rwth.montisim.commons.utils.Vec3;
import de.rwth.montisim.commons.utils.JsonTraverser.Entry; import de.rwth.montisim.commons.utils.json.Typed;
import de.rwth.montisim.commons.utils.JsonTraverser.ObjectIterable;
//@JsonSubtype("obb") or @JsonTyped("obb") ? //@JsonSubtype("obb") or @JsonTyped("obb") ?
//@JsonObject //@JsonObject
@Typed("obb")
public class OBB implements BoundingBox { public class OBB implements BoundingBox {
public static final String TYPE_NAME = "obb"; //public static final String TYPE_NAME = "obb";
public Vec3 offset = new Vec3(); // Offset from center of mass public Vec3 offset = new Vec3(); // Offset from center of mass
public Vec3 half_extent = new Vec3(); public Vec3 half_extent = new Vec3();
public Mat3 axes = new Mat3(); public Mat3 axes = new Mat3();
@Override // @Override
public void toJson(JsonWriter j) { // public void toJson(JsonWriter j) {
j.startObject(); // j.startObject();
j.write("type", TYPE_NAME); // j.write("type", TYPE_NAME);
j.writeKey("offset"); // j.writeKey("offset");
offset.toJson(j); // offset.toJson(j);
j.writeKey("half_extent"); // j.writeKey("half_extent");
half_extent.toJson(j); // half_extent.toJson(j);
j.writeKey("axes"); // j.writeKey("axes");
axes.toJson(j); // axes.toJson(j);
j.endObject(); // j.endObject();
} // }
@Override // @Override
public void fromJson(JsonTraverser j) { // public void fromJson(JsonTraverser j) {
fromJson(j, j.streamObject()); // fromJson(j, j.streamObject());
} // }
@Override // @Override
public void fromJson(JsonTraverser j, ObjectIterable it) { // public void fromJson(JsonTraverser j, ObjectIterable it) {
boolean a = false, b = false, c = false; // boolean a = false, b = false, c = false;
for (Entry e : it) { // for (Entry e : it) {
if (e.key.equals("offset")) { // if (e.key.equals("offset")) {
offset.fromJson(j); // offset.fromJson(j);
a = true; // a = true;
} else if (e.key.equals("half_extent")) { // } else if (e.key.equals("half_extent")) {
half_extent.fromJson(j); // half_extent.fromJson(j);
b = true; // b = true;
} else if (e.key.equals("axes")) { // } else if (e.key.equals("axes")) {
axes.fromJson(j); // axes.fromJson(j);
c = true; // c = true;
} else // } else
j.unexpected(e); // j.unexpected(e);
} // }
if (!a) // if (!a)
j.expected("offset"); // j.expected("offset");
if (!b) // if (!b)
j.expected("half_extent"); // j.expected("half_extent");
if (!c) // if (!c)
j.expected("axes"); // j.expected("axes");
} // }
} }
\ No newline at end of file
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.commons.boundingbox; package de.rwth.montisim.commons.boundingbox;
import de.rwth.montisim.commons.utils.JsonTraverser; import de.rwth.montisim.commons.utils.json.Typed;
import de.rwth.montisim.commons.utils.JsonTraverser.ObjectIterable;
import de.rwth.montisim.commons.utils.JsonWriter;
@Typed("sphere")
public class Sphere implements BoundingBox { public class Sphere implements BoundingBox {
public static final String TYPE_NAME = "sphere"; public static final String TYPE_NAME = "sphere";
@Override
public void toJson(JsonWriter j) {
// TODO Auto-generated method stub
}
@Override
public void fromJson(JsonTraverser j) {
// TODO Auto-generated method stub
}
@Override
public void fromJson(JsonTraverser j, ObjectIterable it) {
// TODO Auto-generated method stub
}
} }
\ No newline at end of file
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.commons.dynamicinterface; package de.rwth.montisim.commons.dynamicinterface;
import java.util.Vector; import java.lang.reflect.InvocationTargetException;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.JsonTraverser; import de.rwth.montisim.commons.utils.json.JsonTraverser;
import de.rwth.montisim.commons.utils.JsonWriter; import de.rwth.montisim.commons.utils.json.SerializationContext;
import de.rwth.montisim.commons.utils.Vec2;
import de.rwth.montisim.commons.utils.Vec3;
import de.rwth.montisim.commons.utils.JsonTraverser.ValueType;
public class ArrayType extends DataType { public class ArrayType extends DataType {
public static enum Dimensionality { public static enum Dimensionality {
ARRAY, ARRAY, DYNAMIC
DYNAMIC
} }
public DataType baseType; public DataType baseType;
public Dimensionality dimension; public Dimensionality dimension;
/** Number of elems for "ARRAY", max size for "DYNAMIC". */ /** Number of elems for "ARRAY", max size for "DYNAMIC". */
public int sizeOrMaxSize; public int sizeOrMaxSize;
public ArrayType(DataType baseType, Dimensionality dim, int sizeOrMaxSize){ public ArrayType(DataType baseType, Dimensionality dim, int sizeOrMaxSize) {
//super(DataType.Type.ARRAY, baseType.getDataSize() * sizeOrMaxSize); //Must give dataSize manually since the default super() calls getDataSize() which is invalid before setting "baseType" & "sizeOrMaxSize". // super(DataType.Type.ARRAY, baseType.getDataSize() * sizeOrMaxSize); //Must
// give dataSize manually since the default super() calls getDataSize() which is
// invalid before setting "baseType" & "sizeOrMaxSize".
super(DataType.Type.ARRAY); super(DataType.Type.ARRAY);
this.baseType = baseType; this.baseType = baseType;
this.dimension = dim; this.dimension = dim;
...@@ -28,17 +27,20 @@ public class ArrayType extends DataType { ...@@ -28,17 +27,20 @@ public class ArrayType extends DataType {
} }
@Override @Override
public int getDataSize(){ public int getDataSize() {
return baseType.getDataSize() * sizeOrMaxSize; return baseType.getDataSize() * sizeOrMaxSize;
} }
@Override @Override
public String toString(){ public String toString() {
if (dimension == Dimensionality.ARRAY) return "["+baseType.toString()+"; "+sizeOrMaxSize+"]"; if (dimension == Dimensionality.ARRAY)
else return "<"+baseType.toString()+"; "+sizeOrMaxSize+">"; return "[" + baseType.toString() + "; " + sizeOrMaxSize + "]";
else
return "<" + baseType.toString() + "; " + sizeOrMaxSize + ">";
} }
// Implement hashCode & equals to be able to perform hashmap lookup by type & type comparison // Implement hashCode & equals to be able to perform hashmap lookup by type &
// type comparison
@Override @Override
public int hashCode() { public int hashCode() {
...@@ -51,118 +53,22 @@ public class ArrayType extends DataType { ...@@ -51,118 +53,22 @@ public class ArrayType extends DataType {
} }
@Override @Override
public boolean equals(Object o){ public boolean equals(Object o) {
if (o == null) if (o == null)
return false; return false;
if (o == this) if (o == this)
return true; return true;
if (this.getClass() != o.getClass()) if (this.getClass() != o.getClass())
return false; return false;
ArrayType a = ((ArrayType)o); ArrayType a = ((ArrayType) o);
return this.baseType.equals(a.baseType) && this.dimension.equals(a.dimension) && this.sizeOrMaxSize == a.sizeOrMaxSize; return this.baseType.equals(a.baseType) && this.dimension.equals(a.dimension)
} && this.sizeOrMaxSize == a.sizeOrMaxSize;
@Override
public void toJson(JsonWriter j, Object o){
j.startArray();
if (baseType.type == Type.DOUBLE) {
double[] a = (double[])o;
for (double d : a) j.writeValue(d);
} else if (baseType.type == Type.FLOAT) {
float[] a = (float[])o;
for (double f : a) j.writeValue(f);
} else if (baseType.type == Type.INT) {
int[] a = (int[])o;
for (int i : a) j.writeValue(i);
} else if (baseType.type == Type.BYTE) {
byte[] a = (byte[])o;
for (byte b : a) j.writeValue(b);
} else if (baseType.type == Type.BOOLEAN) {
boolean[] a = (boolean[])o;
for (boolean b : a) j.writeValue(b);
} else if (baseType.type == Type.EMPTY) {
} else if (baseType.type == Type.VEC2) {
Vec2[] a = (Vec2[])o;
for (Vec2 v : a) v.toJson(j);
} else if (baseType.type == Type.VEC3) {
Vec3[] a = (Vec3[])o;
for (Vec3 v : a) v.toJson(j);
} else throw new IllegalArgumentException("Missing array type implementation (type: "+type+")");
j.endArray();
} }
@Override @Override
public Object fromJson(JsonTraverser j){ public Object fromJson(JsonTraverser j, SerializationContext context) throws InstantiationException, IllegalAccessException,
if (baseType.type == Type.DOUBLE) { IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Vector<Double> v = new Vector<>(); if (baseType.type.arrayC == null) return null;
for (ValueType t : j.streamArray()){ return Json.instantiateFromJson(j, baseType.type.arrayC, context);
v.add(j.getDouble());
}
double[] a = new double[v.size()];
int i = 0;
for (double d : v) a[i++] = d;
return a;
} else if (baseType.type == Type.FLOAT) {
Vector<Float> v = new Vector<>();
for (ValueType t : j.streamArray()){
v.add((float)j.getDouble());
}
float[] a = new float[v.size()];
int i = 0;
for (float f : v) a[i++] = f;
return a;
} else if (baseType.type == Type.INT) {
Vector<Integer> v = new Vector<>();
for (ValueType t : j.streamArray()){
v.add((int)j.getLong());
}
int[] a = new int[v.size()];
int i = 0;
for (int f : v) a[i++] = f;
return a;
} else if (baseType.type == Type.BYTE) {
Vector<Byte> v = new Vector<>();
for (ValueType t : j.streamArray()){
v.add((byte)j.getLong());
}
byte[] a = new byte[v.size()];
int i = 0;
for (byte b : v) a[i++] = b;
return a;
} else if (baseType.type == Type.BOOLEAN) {
Vector<Boolean> v = new Vector<>();
for (ValueType t : j.streamArray()){
v.add(j.getBoolean());
}
boolean[] a = new boolean[v.size()];
int i = 0;
for (boolean b : v) a[i++] = b;
return a;
} else if (baseType.type == Type.EMPTY) {
return null;
} else if (baseType.type == Type.VEC2) {
Vector<Vec2> v = new Vector<>();
for (ValueType t : j.streamArray()){
Vec2 vec = new Vec2();
vec.fromJson(j);
v.add(vec);
}
Vec2[] a = new Vec2[v.size()];
int i = 0;
for (Vec2 b : v) a[i++] = b;
return a;
} else if (baseType.type == Type.VEC3) {
Vector<Vec3> v = new Vector<>();
for (ValueType t : j.streamArray()){
Vec3 vec = new Vec3();
vec.fromJson(j);
v.add(vec);
}
Vec3[] a = new Vec3[v.size()];
int i = 0;
for (Vec3 b : v) a[i++] = b;
return a;
} else throw new IllegalArgumentException("Missing array type implementation (type: "+type+")");
} }
} }
\ No newline at end of file
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.rwth.montisim.commons.dynamicinterface; package de.rwth.montisim.commons.dynamicinterface;
import java.lang.reflect.InvocationTargetException;
import de.rwth.montisim.commons.utils.*; import de.rwth.montisim.commons.utils.*;
import de.rwth.montisim.commons.utils.json.Json;
import de.rwth.montisim.commons.utils.json.JsonEntry; import de.rwth.montisim.commons.utils.json.JsonEntry;
import de.rwth.montisim.commons.utils.json.JsonTraverser;
import de.rwth.montisim.commons.utils.json.JsonWriter;
import de.rwth.montisim.commons.utils.json.SerializationContext;
/** /**
* Reflection class for the types of Messages sent in the simulation. * Reflection class for the types of Messages sent in the simulation.
*/ */
public class DataType { public class DataType {
public static final DataType DOUBLE = new DataType(Type.DOUBLE); public static final DataType DOUBLE = new DataType(Type.DOUBLE);
public static final DataType FLOAT = new DataType(Type.FLOAT); public static final DataType FLOAT = new DataType(Type.FLOAT);
public static final DataType INT = new DataType(Type.INT); public static final DataType INT = new DataType(Type.INT);
public static final DataType BOOLEAN = new DataType(Type.BOOLEAN); public static final DataType BOOLEAN = new DataType(Type.BOOLEAN);
public static final DataType BYTE = new DataType(Type.BYTE); public static final DataType BYTE = new DataType(Type.BYTE);
public static final DataType EMPTY = new DataType(Type.EMPTY); public static final DataType EMPTY = new DataType(Type.EMPTY);
public static final DataType VEC2 = new DataType(Type.VEC2);
public static final DataType VEC3 = new DataType(Type.VEC3);
public static final DataType VEC2 = new DataType(Type.VEC2);
public static final DataType VEC3 = new DataType(Type.VEC3);
public static enum Type { public static enum Type {
//@JsonEntry // @JsonEntry
DOUBLE, DOUBLE(Double.class, double[].class, "double", 8),
FLOAT, FLOAT(Float.class, float[].class, "float", 4),
INT, INT(Integer.class, int[].class, "int", 4),
BYTE, BYTE(Byte.class, byte[].class, "byte", 1),
BOOLEAN, BOOLEAN(Boolean.class, boolean[].class, "boolean", 1),
EMPTY, EMPTY(null, null, "void", 0),
VEC2, VEC2(Vec2.class, Vec2[].class, "Vec2", 16),
VEC3, VEC3(Vec3.class, Vec3[].class, "Vec3", 24),
STRUCT, STRUCT(null, null, "struct", -1),
ARRAY ARRAY(null, null, "array", -1);
}
@Override Type(Class<?> c, Class<?> arrayC, String name, int size) {
public String toString(){ this.c = c;
switch(type){ this.arrayC = arrayC;
case DOUBLE: return "double"; this.name = name;
case FLOAT: return "float"; this.size = size;
case INT: return "int";
case BYTE: return "byte";
case BOOLEAN: return "bool";
case EMPTY: return "void";