Search in sources :

Example 1 with ByteArrayDataSource

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());
}
Also used : SClass(org.bimserver.shared.meta.SClass) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) SField(org.bimserver.shared.meta.SField) DataHandler(javax.activation.DataHandler) Date(java.util.Date) JsonNull(com.google.gson.JsonNull) JsonArray(com.google.gson.JsonArray) SBase(org.bimserver.shared.meta.SBase) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.google.gson.JsonObject) ByteArrayDataSource(org.bimserver.utils.ByteArrayDataSource) HashSet(java.util.HashSet)

Example 2 with ByteArrayDataSource

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;
    }
}
Also used : ByteString(com.google.protobuf.ByteString) DataHandler(javax.activation.DataHandler) DynamicMessage(com.google.protobuf.DynamicMessage) ByteArrayDataSource(org.bimserver.utils.ByteArrayDataSource) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) Date(java.util.Date)

Aggregations

Date (java.util.Date)2 DataHandler (javax.activation.DataHandler)2 ByteArrayDataSource (org.bimserver.utils.ByteArrayDataSource)2 JsonArray (com.google.gson.JsonArray)1 JsonNull (com.google.gson.JsonNull)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ByteString (com.google.protobuf.ByteString)1 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 SBase (org.bimserver.shared.meta.SBase)1 SClass (org.bimserver.shared.meta.SClass)1 SField (org.bimserver.shared.meta.SField)1