Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonPrimitive.

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:org.openbaton.plugin.utils.PluginCaller.java

License:Apache License

public Serializable executeRPC(String methodName, Collection<Serializable> args, Type returnType)
        throws IOException, InterruptedException, PluginException {

    Channel channel = connection.createChannel();
    String replyQueueName = channel.queueDeclare().getQueue();
    String exchange = "plugin-exchange";
    channel.queueBind(replyQueueName, exchange, replyQueueName);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    String consumerTag = channel.basicConsume(replyQueueName, true, consumer);

    //Check if plugin is still up
    if (!RabbitManager.getQueues(brokerIp, username, password, managementPort).contains(pluginId))
        throw new PluginException("Plugin with id: " + pluginId + " not existing anymore...");

    String response;/*from  ww w .j av a 2s  .  co m*/
    String corrId = UUID.randomUUID().toString();
    PluginMessage pluginMessage = new PluginMessage();
    pluginMessage.setMethodName(methodName);
    pluginMessage.setParameters(args);
    String message = gson.toJson(pluginMessage);

    BasicProperties props = new Builder().correlationId(corrId).replyTo(replyQueueName).build();

    channel.basicPublish(exchange, pluginId, props, message.getBytes());

    if (returnType != null) {

        while (true) {
            Delivery delivery = consumer.nextDelivery();
            if (delivery.getProperties().getCorrelationId().equals(corrId)) {
                response = new String(delivery.getBody());
                log.trace("received: " + response);
                break;
            } else {
                log.error("Received Message with wrong correlation id");
                throw new PluginException(
                        "Received Message with wrong correlation id. This should not happen, if it does please call us.");
            }
        }

        channel.queueDelete(replyQueueName);
        try {
            channel.close();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = gson.fromJson(response, JsonObject.class);

        JsonElement exceptionJson = jsonObject.get("exception");
        if (exceptionJson == null) {
            JsonElement answerJson = jsonObject.get("answer");

            Serializable ret = null;

            if (answerJson.isJsonPrimitive()) {
                ret = gson.fromJson(answerJson.getAsJsonPrimitive(), returnType);
            } else if (answerJson.isJsonArray()) {
                ret = gson.fromJson(answerJson.getAsJsonArray(), returnType);
            } else
                ret = gson.fromJson(answerJson.getAsJsonObject(), returnType);

            log.trace("answer is: " + ret);
            return ret;
        } else {
            PluginException pluginException;
            try {
                pluginException = new PluginException(
                        gson.fromJson(exceptionJson.getAsJsonObject(), VimDriverException.class));
                log.debug("Got Vim Driver Exception with server: "
                        + ((VimDriverException) pluginException.getCause()).getServer());
            } catch (Exception ignored) {
                pluginException = new PluginException(
                        gson.fromJson(exceptionJson.getAsJsonObject(), Throwable.class));
            }
            throw pluginException;
        }
    } else
        return null;
}

From source file:org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeReader.java

License:Open Source License

private static void addChildToParent(final String childName, final JsonElement childType,
        final CompositeNodeWrapper parent) {
    if (childType.isJsonObject()) {
        CompositeNodeWrapper child = new CompositeNodeWrapper(getNamespaceFor(childName),
                getLocalNameFor(childName));
        parent.addValue(child);//from ww w.j a  va2  s  . c om
        for (Entry<String, JsonElement> childOfChild : childType.getAsJsonObject().entrySet()) {
            addChildToParent(childOfChild.getKey(), childOfChild.getValue(), child);
        }
    } else if (childType.isJsonArray()) {
        if (childType.getAsJsonArray().size() == 1 && childType.getAsJsonArray().get(0).isJsonNull()) {
            parent.addValue(new EmptyNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName)));

        } else {
            for (JsonElement childOfChildType : childType.getAsJsonArray()) {
                addChildToParent(childName, childOfChildType, parent);
            }
        }
    } else if (childType.isJsonPrimitive()) {
        JsonPrimitive childPrimitive = childType.getAsJsonPrimitive();
        String value = childPrimitive.getAsString().trim();
        parent.addValue(new SimpleNodeWrapper(getNamespaceFor(childName), getLocalNameFor(childName),
                resolveValueOfElement(value)));
    } else {
        LOG.debug("Ignoring unhandled child type {}", childType);
    }
}

From source file:org.opendaylight.vtn.javaapi.openstack.validation.PortResourceValidator.java

License:Open Source License

/**
 * Validates the parameters of POST request body
 * /*from  w ww. j av a2 s. co m*/
 * @param requestBody
 *            - JSON request body corresponding to POST operation
 * @return
 */
public boolean validatePost(final JsonObject requestBody) {
    LOG.trace("Start PortResourceValidator#validatePost()");
    boolean isValid = true;
    // validation of mandatory parameters
    if (requestBody == null || !requestBody.has(VtnServiceOpenStackConsts.PORT)
            || !requestBody.has(VtnServiceOpenStackConsts.DATAPATH_ID)
            || !requestBody.has(VtnServiceOpenStackConsts.VID)) {
        isValid = false;
        setInvalidParameter(UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
    } else {
        // validation of id
        if (requestBody.has(VtnServiceOpenStackConsts.ID)) {
            final JsonElement id = requestBody.get(VtnServiceOpenStackConsts.ID);
            if (id.isJsonNull() || id.getAsString().isEmpty()
                    || !validator.isValidMaxLengthAlphaNum(id.getAsString(), VtnServiceJsonConsts.LEN_24)) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.ID + VtnServiceConsts.COLON
                        + (id.isJsonNull() ? id : id.getAsString()));
            }
        }

        // validation of datapath_id
        if (isValid) {
            final JsonElement datapathId = requestBody.get(VtnServiceOpenStackConsts.DATAPATH_ID);
            setInvalidParameter(VtnServiceOpenStackConsts.DATAPATH_ID);
            if (datapathId.isJsonNull() || !isValidDataPathId(datapathId.getAsString())) {
                isValid = false;
                setInvalidParameter(VtnServiceOpenStackConsts.DATAPATH_ID + VtnServiceConsts.COLON
                        + (datapathId.isJsonNull() ? datapathId : datapathId.getAsString()));
            }
        }

        // validation of port
        if (isValid) {
            final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);
            if (!port.isJsonNull() && !port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL)) {
                if (port.getAsString().isEmpty()) {
                    isValid = false;
                } else {
                    try {
                        isValid = validator.isValidBigIntegerRangeString(new BigInteger(port.getAsString()),
                                VtnServiceJsonConsts.BIG_VAL0, VtnServiceJsonConsts.BIG_VAL_4294967040);
                    } catch (Exception e) {
                        isValid = false;
                    }
                }

                // set message as per above checks
                if (!isValid) {
                    setInvalidParameter(VtnServiceOpenStackConsts.PORT + VtnServiceConsts.COLON
                            + (port.isJsonNull() ? port : port.getAsString()));
                }
            }
        }

        // validation of vid
        if (isValid) {
            final JsonElement vid = requestBody.get(VtnServiceOpenStackConsts.VID);
            setInvalidParameter(VtnServiceOpenStackConsts.VID);
            if (vid.isJsonNull() || vid.getAsString().isEmpty()) {
                isValid = false;
            } else {
                try {
                    isValid = validator.isValidRange(vid.getAsString(), VtnServiceJsonConsts.VAL_1,
                            VtnServiceJsonConsts.VAL_4095);
                    if (Integer.parseInt(vid.getAsString()) == VtnServiceJsonConsts.VAL_65535) {
                        isValid = true;
                    }
                } catch (Exception e) {
                    isValid = false;
                }
            }

            // set message as per above checks
            if (!isValid) {
                setInvalidParameter(VtnServiceOpenStackConsts.VID + VtnServiceConsts.COLON
                        + (vid.isJsonNull() ? vid : vid.getAsString()));
            }
        }

        // validation of filters.
        if (isValid) {
            setInvalidParameter(VtnServiceOpenStackConsts.FILTERS);
            if (requestBody.has(VtnServiceOpenStackConsts.FILTERS)) {
                if (requestBody.get(VtnServiceOpenStackConsts.FILTERS).isJsonArray()) {
                    if (requestBody.getAsJsonArray(VtnServiceOpenStackConsts.FILTERS).size() > 0) {
                        JsonArray filters = requestBody.getAsJsonArray(VtnServiceOpenStackConsts.FILTERS);
                        Iterator<JsonElement> iterator = filters.iterator();
                        while (iterator.hasNext()) {
                            JsonElement filterID = iterator.next();
                            if (filterID.isJsonPrimitive()) {
                                isValid = isValidFilterId(filterID.getAsString());
                                // Set message as per above checks
                                if (!isValid) {
                                    setInvalidParameter(VtnServiceOpenStackConsts.FILTER_RES_ID
                                            + VtnServiceConsts.COLON + filterID.getAsString());
                                    LOG.debug("Invalid flow filter id: %s", filterID.getAsString());
                                    break;
                                }
                            } else {
                                setInvalidParameter(
                                        UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
                                isValid = false;
                                break;
                            }
                        }
                    }
                } else {
                    setInvalidParameter(UncCommonEnum.UncResultCode.UNC_INVALID_FORMAT.getMessage());
                    isValid = false;
                }
            }
            // filters is not specified, The isValid is true.
        }
    }

    if (isValid) {
        final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);
        if (port.isJsonNull() || port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL)) {
            if (requestBody.has(VtnServiceOpenStackConsts.FILTERS)
                    && requestBody.getAsJsonArray(VtnServiceOpenStackConsts.FILTERS).size() > 0) {
                isValid = false;
                setInvalidParameter("filters,but no port");
            }
        }
    }

    // validation of port and datapath_id combination
    if (isValid) {
        final JsonElement port = requestBody.get(VtnServiceOpenStackConsts.PORT);
        final JsonElement datapathid = requestBody.get(VtnServiceOpenStackConsts.DATAPATH_ID);
        if (datapathid.getAsString().isEmpty() && (!port.isJsonNull()
                && !port.getAsString().equalsIgnoreCase(VtnServiceOpenStackConsts.NULL))) {
            isValid = false;
            setInvalidParameter("port specified, but datapath_id not " + "specified");
        }
    }
    LOG.trace("Complete PortResourceValidator#validatePost()");
    return isValid;
}

From source file:org.opendolphin.core.comm.JsonCodec.java

License:Apache License

private Object toValidValue(JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return null;
    } else if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive primitive = jsonElement.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isString()) {
            return primitive.getAsString();
        } else {/*from  w  ww.  j av  a2  s  .  co m*/
            return primitive.getAsNumber();
        }
    } else if (jsonElement.isJsonObject()) {
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        if (jsonObject.has(Date.class.toString())) {
            try {
                return new SimpleDateFormat(ISO8601_FORMAT)
                        .parse(jsonObject.getAsJsonPrimitive(Date.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(BigDecimal.class.toString())) {
            try {
                return new BigDecimal(jsonObject.getAsJsonPrimitive(BigDecimal.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(Float.class.toString())) {
            try {
                return Float.valueOf(jsonObject.getAsJsonPrimitive(Float.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(Double.class.toString())) {
            try {
                return Double.valueOf(jsonObject.getAsJsonPrimitive(Double.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        }
    }
    throw new RuntimeException("Can not converte!");
}

From source file:org.openecomp.sdc.be.model.tosca.converters.JsonConverter.java

License:Open Source License

@Override
public String convert(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
    StringReader reader = new StringReader(value);
    JsonReader jsonReader = new JsonReader(reader);
    jsonReader.setLenient(true);//from  w w w  .j a  va2 s . c o  m
    JsonElement jsonElement = jsonParser.parse(jsonReader);
    if (jsonElement.isJsonPrimitive()) {
        return value;
    }
    return gson.toJson(jsonElement);
}

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaJsonValueConverter.java

License:Open Source License

@Override
public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
    StringReader reader = new StringReader(value);
    JsonReader jsonReader = new JsonReader(reader);
    jsonReader.setLenient(true);// ww w.  ja  v a2s .c  o m
    JsonElement jsonElement = jsonParser.parse(jsonReader);
    if (jsonElement.isJsonPrimitive()) {
        return value;
    }
    return handleComplexJsonValue(jsonElement);
}

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaListValueConverter.java

License:Open Source License

@Override
public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
    if (value == null) {
        return null;
    }//from   w  ww.j  av a 2 s  .co m
    try {
        ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
        ToscaValueConverter innerConverter = null;
        boolean isScalar = true;
        if (innerToscaType != null) {
            innerConverter = innerToscaType.getValueConverter();
        } else {
            DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);

            if (dataTypeDefinition != null) {
                ToscaPropertyType toscaPropertyType = null;
                if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
                    innerConverter = toscaPropertyType.getValueConverter();
                } else {
                    isScalar = false;
                    innerConverter = ToscaMapValueConverter.getInstance();
                }
            } else {
                log.debug("inner Tosca Type is null");
                return value;
            }
        }
        JsonElement jsonElement = null;
        try {
            StringReader reader = new StringReader(value);
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);

            jsonElement = jsonParser.parse(jsonReader);
        } catch (JsonSyntaxException e) {
            log.debug("convertToToscaValue failed to parse json value :", e);
            return null;
        }
        if (jsonElement == null || true == jsonElement.isJsonNull()) {
            log.debug("convertToToscaValue json element is null");
            return null;
        }
        if (jsonElement.isJsonArray() == false) {
            // get_input all array like get_input: qrouter_names
            return handleComplexJsonValue(jsonElement);
        }
        JsonArray asJsonArray = jsonElement.getAsJsonArray();

        ArrayList<Object> toscaList = new ArrayList<Object>();
        final boolean isScalarF = isScalar;
        final ToscaValueConverter innerConverterFinal = innerConverter;
        asJsonArray.forEach(e -> {
            Object convertedValue = null;
            if (isScalarF) {
                log.debug("try to convert scalar value {}", e.getAsString());
                if (e.getAsString() == null) {
                    convertedValue = null;
                } else {
                    JsonElement singleElement = jsonParser.parse(e.getAsString());
                    if (singleElement.isJsonPrimitive()) {
                        convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType,
                                dataTypes);
                    } else {
                        convertedValue = handleComplexJsonValue(singleElement);
                    }
                }
            } else {
                JsonObject asJsonObject = e.getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

                DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
                Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
                Map<String, Object> toscaObjectPresentation = new HashMap<>();
                // log.debug("try to convert datatype value {}",
                // e.getAsString());

                for (Entry<String, JsonElement> entry : entrySet) {
                    String propName = entry.getKey();

                    JsonElement elementValue = entry.getValue();
                    PropertyDefinition propertyDefinition = allProperties.get(propName);
                    if (propertyDefinition == null) {
                        log.debug("The property {} was not found under data type {}", propName,
                                dataTypeDefinition.getName());
                        continue;
                        // return null;
                    }
                    String type = propertyDefinition.getType();
                    ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
                    Object convValue;
                    if (propertyType != null) {
                        if (elementValue.isJsonPrimitive()) {
                            ToscaValueConverter valueConverter = propertyType.getValueConverter();
                            convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
                                    dataTypes);
                        } else {
                            if (ToscaPropertyType.MAP.equals(type)
                                    || ToscaPropertyType.LIST.equals(propertyType)) {
                                ToscaValueConverter valueConverter = propertyType.getValueConverter();
                                String json = gson.toJson(elementValue);
                                String innerTypeRecursive = propertyDefinition.getSchema().getProperty()
                                        .getType();
                                convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive,
                                        dataTypes);
                            } else {
                                convValue = handleComplexJsonValue(elementValue);
                            }
                        }
                    } else {
                        String json = gson.toJson(elementValue);
                        convValue = convertToToscaValue(json, type, dataTypes);
                    }
                    toscaObjectPresentation.put(propName, convValue);
                }
                convertedValue = toscaObjectPresentation;
            }
            toscaList.add(convertedValue);
        });
        return toscaList;
    } catch (

    JsonParseException e) {
        log.debug("Failed to parse json : {}. {}", value, e);
        BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
        return null;
    }
}

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter.java

License:Open Source License

public Object convertDataTypeToToscaMap(String innerType, Map<String, DataTypeDefinition> dataTypes,
        ToscaValueConverter innerConverter, final boolean isScalarF, JsonElement entryValue) {
    Object convertedValue = null;
    if (isScalarF && entryValue.isJsonPrimitive()) {
        log.debug("try convert scalar value {}", entryValue.getAsString());
        if (entryValue.getAsString() == null) {
            convertedValue = null;/*from w  w  w. jav a  2s .co  m*/
        } else {
            convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
        }
    } else {
        JsonObject asJsonObjectIn = entryValue.getAsJsonObject();
        Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet();

        DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
        Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
        Map<String, Object> toscaObjectPresentation = new HashMap<>();

        for (Entry<String, JsonElement> entry : entrySetIn) {
            String propName = entry.getKey();

            JsonElement elementValue = entry.getValue();
            Object convValue;
            if (isScalarF == false) {
                PropertyDefinition propertyDefinition = allProperties.get(propName);
                if (propertyDefinition == null && isScalarF) {
                    log.debug("The property {} was not found under data type {}", propName,
                            dataTypeDefinition.getName());
                    continue;
                }

                String type = propertyDefinition.getType();
                ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
                if (propertyType != null) {
                    if (elementValue.isJsonPrimitive()) {
                        ToscaValueConverter valueConverter = propertyType.getValueConverter();
                        convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
                                dataTypes);
                    } else {
                        if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
                            ToscaValueConverter valueConverter = propertyType.getValueConverter();
                            String json = gson.toJson(elementValue);
                            String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
                            convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
                        } else {
                            convValue = handleComplexJsonValue(elementValue);
                        }
                    }
                } else {
                    convValue = convertToToscaValue(elementValue.getAsString(), type, dataTypes);
                }
            } else {
                if (elementValue.isJsonPrimitive()) {
                    convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
                } else {
                    convValue = handleComplexJsonValue(elementValue);
                }
            }
            toscaObjectPresentation.put(propName, convValue);
        }
        convertedValue = toscaObjectPresentation;
    }
    return convertedValue;
}

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter.java

License:Open Source License

private List<Object> handleJsonArray(JsonElement entry) {
    List<Object> array = new ArrayList<>();
    JsonArray jsonArray = entry.getAsJsonArray();
    Iterator<JsonElement> iterator = jsonArray.iterator();
    while (iterator.hasNext()) {
        Object object;/*from   w  w  w  . j  ava2 s  .c o  m*/
        JsonElement element = iterator.next();
        if (element.isJsonPrimitive()) {
            object = json2JavaPrimitive(element.getAsJsonPrimitive());
        } else {
            object = handleComplexJsonValue(element);
        }
        array.add(object);
    }
    return array;
}

From source file:org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter.java

License:Open Source License

private ImmutablePair<JsonElement, Boolean> validateAndUpdate(JsonElement jsonElement,
        DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) {

    Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);

    ToscaPropertyType toscaPropertyType = null;
    if ((toscaPropertyType = isDataTypeDerviedFromScalarType(dataTypeDefinition)) != null) {

        PropertyTypeValidator validator = toscaPropertyType.getValidator();
        PropertyValueConverter converter = toscaPropertyType.getConverter();
        if (jsonElement == null || true == jsonElement.isJsonNull()) {
            boolean valid = validator.isValid(null, null, allDataTypes);
            if (false == valid) {
                log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(),
                        dataTypeDefinition.getName());
                return falseResult;
            }/*ww  w .  ja  v  a2  s.  co m*/
            return new ImmutablePair<JsonElement, Boolean>(jsonElement, true);

        } else {
            if (true == jsonElement.isJsonPrimitive()) {
                String value = null;
                if (jsonElement != null) {
                    if (jsonElement.toString().isEmpty()) {
                        value = "";
                    } else {
                        value = jsonElement.toString();
                    }
                }
                boolean valid = validator.isValid(value, null, null);
                if (false == valid) {
                    log.trace("Failed in validation of property {} from type {}. Json primitive value is {}",
                            dataTypeDefinition.getName(), dataTypeDefinition.getName(), value);
                    return falseResult;
                }

                String convertedValue = converter.convert(value, null, allDataTypes);
                JsonElement element = null;
                try {
                    element = jsonParser.parse(convertedValue);
                } catch (JsonSyntaxException e) {
                    log.debug("Failed to parse value {} of property {}. {}", convertedValue,
                            dataTypeDefinition.getName(), e);
                    return falseResult;
                }

                return new ImmutablePair<JsonElement, Boolean>(element, true);

            } else {
                // MAP, LIST, OTHER types cannot be applied data type
                // definition scalar type. We currently cannot derived from
                // map/list. (cannot add the entry schema to it)
                log.debug(
                        "We cannot derive from list/map. Thus, the value cannot be not primitive since the data type {} is scalar one",
                        dataTypeDefinition.getName());

                return falseResult;
            }
        }
    } else {

        if (jsonElement == null || jsonElement.isJsonNull()) {

            return new ImmutablePair<JsonElement, Boolean>(jsonElement, true);

        } else {

            if (jsonElement.isJsonObject()) {

                JsonObject buildJsonObject = new JsonObject();

                JsonObject asJsonObject = jsonElement.getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

                for (Entry<String, JsonElement> entry : entrySet) {
                    String propName = entry.getKey();

                    JsonElement elementValue = entry.getValue();

                    PropertyDefinition propertyDefinition = allProperties.get(propName);
                    if (propertyDefinition == null) {
                        log.debug("The property {} was not found under data type {}", propName,
                                dataTypeDefinition.getName());
                        return falseResult;
                    }
                    String type = propertyDefinition.getType();
                    boolean isScalarType = ToscaPropertyType.isScalarType(type);

                    if (true == isScalarType) {
                        ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
                        if (propertyType == null) {
                            log.debug("cannot find the {} under default tosca property types", type);
                            return falseResult;
                        }
                        PropertyTypeValidator validator = propertyType.getValidator();
                        String innerType = null;
                        if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) {
                            if (propertyDefinition.getSchema() != null
                                    && propertyDefinition.getSchema().getProperty() != null) {
                                innerType = propertyDefinition.getSchema().getProperty().getType();
                                if (innerType == null) {
                                    log.debug("Property type {} must have inner type in its declaration.",
                                            propertyType);
                                    return falseResult;
                                }
                            }
                        }

                        String value = null;
                        if (elementValue != null) {
                            if (elementValue.isJsonPrimitive() && elementValue.getAsString().isEmpty()) {
                                value = "";
                            } else {
                                value = elementValue.toString();
                            }
                        }

                        boolean isValid = validator.isValid(value, innerType, allDataTypes);
                        if (false == isValid) {
                            log.debug("Failed to validate the value {} from type {}", value, propertyType);
                            return falseResult;
                        }

                        PropertyValueConverter converter = propertyType.getConverter();
                        String convertedValue = converter.convert(value, innerType, allDataTypes);

                        JsonElement element = null;
                        if (convertedValue != null) {
                            if (convertedValue.isEmpty()) {
                                element = new JsonPrimitive("");
                            } else {
                                try {
                                    element = jsonParser.parse(convertedValue);
                                } catch (JsonSyntaxException e) {
                                    log.debug("Failed to parse value {} of type {}. {}", convertedValue,
                                            propertyType, e);
                                    return falseResult;
                                }
                            }
                        }
                        buildJsonObject.add(propName, element);

                    } else {

                        DataTypeDefinition typeDefinition = allDataTypes.get(type);
                        if (typeDefinition == null) {
                            log.debug("The data type {] cannot be found in the given data type list.", type);
                            return falseResult;
                        }

                        ImmutablePair<JsonElement, Boolean> isValid = validateAndUpdate(elementValue,
                                typeDefinition, allDataTypes);

                        if (false == isValid.getRight().booleanValue()) {
                            log.debug("Failed in validation of value {} from type {}",
                                    (elementValue != null ? elementValue.toString() : null),
                                    typeDefinition.getName());
                            return falseResult;
                        }

                        buildJsonObject.add(propName, isValid.getLeft());
                    }

                }

                return new ImmutablePair<JsonElement, Boolean>(buildJsonObject, true);
            } else {
                log.debug("The value {} of type {} should be json object",
                        (jsonElement != null ? jsonElement.toString() : null), dataTypeDefinition.getName());
                return falseResult;
            }

        }
    }

}