use of org.bimserver.utils.ByteArrayDataSource in project BIMserver by opensourceBIM.
the class JsonConverter method fromJson.
public Object fromJson(SClass definedType, SClass genericType, Object object) throws ConvertException, IOException {
try {
if (object instanceof JsonObject) {
JsonObject jsonObject = (JsonObject) object;
if (jsonObject.has("__type")) {
String type = jsonObject.get("__type").getAsString();
SClass sClass = servicesMap.getType(type);
if (sClass == null) {
throw new ConvertException("Unknown type: " + type);
}
SBase newObject = sClass.newInstance();
for (SField field : newObject.getSClass().getAllFields()) {
if (jsonObject.has(field.getName())) {
newObject.sSet(field, fromJson(field.getType(), field.getGenericType(), jsonObject.get(field.getName())));
}
}
return newObject;
} else {
if (jsonObject.entrySet().size() != 0) {
throw new ConvertException("Missing __type field in " + jsonObject.toString());
} else {
return null;
}
}
} else if (object instanceof JsonArray) {
JsonArray array = (JsonArray) object;
if (definedType.isList()) {
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.size(); i++) {
list.add(fromJson(definedType, genericType, array.get(i)));
}
return list;
} else if (definedType.isSet()) {
Set<Object> set = new HashSet<Object>();
for (int i = 0; i < array.size(); i++) {
set.add(fromJson(definedType, genericType, array.get(i)));
}
return set;
}
} else if (object instanceof JsonNull) {
return null;
} else if (definedType.isByteArray()) {
if (object instanceof JsonPrimitive) {
JsonPrimitive jsonPrimitive = (JsonPrimitive) object;
return Base64.decodeBase64(jsonPrimitive.getAsString().getBytes(Charsets.UTF_8));
}
} else if (definedType.isDataHandler()) {
if (object instanceof JsonPrimitive) {
JsonPrimitive jsonPrimitive = (JsonPrimitive) object;
byte[] data = Base64.decodeBase64(jsonPrimitive.getAsString().getBytes(Charsets.UTF_8));
return new DataHandler(new ByteArrayDataSource(null, data));
}
} else if (definedType.isInteger()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsInt();
}
} else if (definedType.isLong()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsLong();
}
} else if (definedType.isEnum()) {
JsonPrimitive primitive = (JsonPrimitive) object;
for (Object enumConstantObject : definedType.getInstanceClass().getEnumConstants()) {
Enum<?> enumConstant = (Enum<?>) enumConstantObject;
if (enumConstant.name().equals(primitive.getAsString())) {
return enumConstant;
}
}
} else if (definedType.isDate()) {
if (object instanceof JsonPrimitive) {
return new Date(((JsonPrimitive) object).getAsLong());
}
} else if (definedType.isString()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsString();
} else if (object instanceof JsonNull) {
return null;
}
} else if (definedType.isBoolean()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsBoolean();
}
} else if (definedType.isList()) {
if (genericType.isLong()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsLong();
}
} else if (genericType.isInteger()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsInt();
}
} else if (genericType.isString()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsString();
}
} else if (genericType.isDouble()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsDouble();
}
}
} else if (definedType.isSet()) {
if (genericType.isLong()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsLong();
}
} else if (genericType.isInteger()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsInt();
}
} else if (genericType.isString()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsString();
}
}
} else if (definedType.isDouble()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsDouble();
}
} else if (definedType.isFloat()) {
if (object instanceof JsonPrimitive) {
return ((JsonPrimitive) object).getAsFloat();
}
} else if (definedType.isVoid()) {
return null;
}
} catch (NumberFormatException e) {
throw new ConvertException(e);
}
throw new UnsupportedOperationException(object.toString());
}
use of org.bimserver.utils.ByteArrayDataSource in project BIMserver by opensourceBIM.
the class ProtocolBuffersConverter method convertFieldValue.
private Object convertFieldValue(SField field, Object val) throws ConvertException {
if (val instanceof EnumValueDescriptor) {
EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) val;
Class<?> enumClass;
try {
enumClass = Class.forName("org.bimserver.interfaces.objects." + enumValueDescriptor.getType().getName());
for (Object v : enumClass.getEnumConstants()) {
Enum<?> e = (Enum<?>) v;
if (e.ordinal() == enumValueDescriptor.getNumber()) {
val = e;
break;
}
}
} catch (ClassNotFoundException e) {
LOGGER.error("", e);
}
return val;
} else if (field.getType().getInstanceClass() == Date.class) {
return new Date((Long) val);
} else if (field.getType().getInstanceClass() == DataHandler.class) {
ByteString byteString = (ByteString) val;
ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource("test", byteString.toByteArray());
return new DataHandler(byteArrayDataSource);
} else if (val instanceof DynamicMessage) {
return convertProtocolBuffersMessageToSObject((DynamicMessage) val, null, field.isAggregate() ? field.getGenericType() : field.getType());
} else {
return val;
}
}
Aggregations