Esempio n. 1
0
    PropertiesPtr JsonSchema::readProp(cJSON *childProperties, const std::string &attName )
    {
        PropertiesPtr property = std::make_shared<Properties>(attName);

        cJSON *propertyDescription = cJSON_GetObjectItem(childProperties, "description");
        if (propertyDescription)
        {
            property->setDescription(propertyDescription->valuestring);
        }
        cJSON *propertyType = cJSON_GetObjectItem(childProperties, "type");
        if (propertyType)
        {
            std::string attType;
            if (propertyType->type == 4)
            {
                attType = propertyType->valuestring;
                property->setType(attType);
            }
            else if (propertyType->type == 5)
            {
                attType = cJSON_GetArrayItem(propertyType, 0)->valuestring;
                property->setType(attType);
            }
            readValues(childProperties, property, attType);
        }
        cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default");
        if (defaultValue)
        {
            if (defaultValue->type == 4)
            {
                property->setValue((std::string)defaultValue->valuestring);
            }
            else if (defaultValue->type == 3)
            {
                if (property->getType() == "number")
                    property->setValue((double)defaultValue->valuedouble);
                else
                    property->setValue((int)defaultValue->valueint );
            }
            else if (defaultValue->type == 1)
            {
                property->setValue((bool)true);
            }
            else if (defaultValue->type == 0)
            {
                property->setValue((bool)false);
            }

        }
        cJSON *allowedvalues = cJSON_GetObjectItem(childProperties, "enum");
        if (allowedvalues)
        {
            if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4)
            {
                int size = cJSON_GetArraySize(allowedvalues);
                int idx = 0;
                std::vector<std::string> allwdValues;
                do
                {
                    allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring);
                }
                while ( ++idx < size);
                property->setAllowedValues(allwdValues);
            }
            else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3)
            {
                int size = cJSON_GetArraySize(allowedvalues);
                int idx = 0;
                if (property->getType() == "number")
                {
                    std::vector<double> allwdValues;
                    do
                    {
                        allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble);
                    }
                    while ( ++idx < size);
                    property->setAllowedValues(allwdValues);
                }
                else
                {
                    std::vector<int> allwdValues;
                    do
                    {
                        allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint);
                    }
                    while ( ++idx < size);
                    property->setAllowedValues(allwdValues);
                }
            }
            else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1)
                     || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0))
            {
                int size = cJSON_GetArraySize(allowedvalues);
                int idx = 0;
                std::vector<bool> allwdValues;
                do
                {
                    if (cJSON_GetArrayItem(allowedvalues, idx)->type)
                        allwdValues.push_back(true);
                    else
                        allwdValues.push_back(false);
                }
                while ( ++idx < size);
                property->setAllowedValues(allwdValues);
            }
        }
        return property;
    }