X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fsimulator%2Framlparser%2Framl%2FjsonSchemaParser%2FJsonSchema.cpp;h=826e67da46e63da00971d701b1e1f7fd7017117c;hb=710b8664d2dd595a649518f144c40850af7364aa;hp=67968e264b44984e70adf3357e07f477880f8594;hpb=6b6a0398741ca2be52a8cb8eb1bcaa56207a4ae6;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp index 67968e2..826e67d 100755 --- a/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp +++ b/service/simulator/ramlparser/raml/jsonSchemaParser/JsonSchema.cpp @@ -18,9 +18,14 @@ * ******************************************************************/ -#include "JsonSchema.h" -using namespace std; +/** + * @file JsonSchema.cpp + * + * @brief This file reads data from Json Schema. + */ +#include "JsonSchema.h" +#include namespace RAML { @@ -78,26 +83,11 @@ namespace RAML } if (m_type == "array") { - cJSON *jsonItems = cJSON_GetObjectItem(m_cjson, "items"); - if (jsonItems) - { - if (jsonItems->type == 5) - { - int item_size = cJSON_GetArraySize(jsonItems); - int item_index = 0; - do - { - cJSON *item = cJSON_GetArrayItem(jsonItems, item_index); - setItem(readItems(item)); - } - while ( ++item_index < item_size); - } - else - { - setItem(readItems(jsonItems)); - } - } + PropertiesPtr property = std::make_shared("array"); + readArray(m_cjson, property); + addProperty("array" , property); } + cJSON *jsonAdditionalProperties = cJSON_GetObjectItem(m_cjson, "additionalProperties"); if (jsonAdditionalProperties) m_additionalProperties = jsonAdditionalProperties->type; @@ -107,12 +97,37 @@ namespace RAML cJSON *jsonReference = cJSON_GetObjectItem(m_cjson, "$ref"); if (jsonReference) { - readJsonRef(jsonReference); + JsonParameters param; + readJsonRef(jsonReference, param); + + for (auto it : param.getProperties()) + { + addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + setRequiredValue(it); + } + if (m_type.empty()) + m_type = param.getType(); } cJSON *jsonAllOf = cJSON_GetObjectItem(m_cjson, "allOf"); if (jsonAllOf) { - readAllOf(jsonAllOf); + JsonParameters param; + + readAllOf(jsonAllOf, param); + + for (auto it : param.getProperties()) + { + addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + setRequiredValue(it); + } + if (m_type.empty()) + m_type = param.getType(); } cJSON *jsonRequiredValues = cJSON_GetObjectItem(m_cjson, "required"); if (jsonRequiredValues) @@ -136,6 +151,12 @@ namespace RAML { std::string type = defType->valuestring; definition->setType(type); + if (type == "array") + { + PropertiesPtr property = std::make_shared("array"); + readArray(childDefinitions, property); + definition->addProperty("array" , property); + } } cJSON *defProperties = cJSON_GetObjectItem(childDefinitions, "properties"); if (defProperties) @@ -162,12 +183,36 @@ namespace RAML cJSON *defReference = cJSON_GetObjectItem(childDefinitions, "$ref"); if (defReference) { - readDefRef(defReference, definition); + JsonParameters param; + readJsonRef(defReference, param); + + for (auto it : param.getProperties()) + { + definition->addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + definition->setRequiredValue(it); + } + if (definition->getType().empty()) + definition->setType(param.getType()); } cJSON *defAllOf = cJSON_GetObjectItem(childDefinitions, "allOf"); if (defAllOf) { - readDefAllOf(defAllOf, definition); + JsonParameters param; + readAllOf(defAllOf, param); + + for (auto it : param.getProperties()) + { + definition->addProperty(it.first, it.second); + } + for ( auto it : param.getRequired()) + { + definition->setRequiredValue(it); + } + if (definition->getType().empty()) + definition->setType(param.getType()); } return definition; } @@ -182,106 +227,125 @@ namespace RAML property->setDescription(propertyDescription->valuestring); } cJSON *propertyType = cJSON_GetObjectItem(childProperties, "type"); + std::string attType; 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 (!(attType == "array") && !(attType == "object")) { - if (defaultValue->type == 4) + cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default"); + if (defaultValue) { - property->setValue((std::string)defaultValue->valuestring); + readDefaultValue(defaultValue, property, attType); } - 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); - } - } + readValues(childProperties, property, attType); cJSON *allowedvalues = cJSON_GetObjectItem(childProperties, "enum"); if (allowedvalues) { - if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4) + readAllowedValues(allowedvalues, property, attType); + } + property->setTypeString(attType); + return property; + } + + void JsonSchema::readDefaultValue(cJSON *defaultValue, PropertiesPtr &property, + const std::string &attType) + { + if (defaultValue->type == 4) + { + property->setValue((std::string)defaultValue->valuestring); + } + else if (defaultValue->type == 3) + { + if (attType == "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); + } + } + + void JsonSchema::readAllowedValues(cJSON *allowedvalues, PropertiesPtr &property, + std::string &attType) + { + if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4) + { + int size = cJSON_GetArraySize(allowedvalues); + int idx = 0; + std::vector allwdValues; + do { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring); - } - while ( ++idx < size); - property->setAllowedValues(allwdValues); + allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring); } - else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3) + while ( ++idx < size); + property->setValueProperty(std::make_shared(allwdValues)); + if (attType.empty()) + attType = "string"; + } + else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3) + { + int size = cJSON_GetArraySize(allowedvalues); + int idx = 0; + if (attType == "number") { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - if (property->getType() == "number") - { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble); - } - while ( ++idx < size); - property->setAllowedValues(allwdValues); - } - else + std::vector allwdValues; + do { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint); - } - while ( ++idx < size); - property->setAllowedValues(allwdValues); + allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble); } + while ( ++idx < size); + property->setValueProperty(std::make_shared(allwdValues)); } - else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1) - || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0)) + else { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; + std::vector allwdValues; do { - if (cJSON_GetArrayItem(allowedvalues, idx)->type) - allwdValues.push_back(true); - else - allwdValues.push_back(false); + allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint); } while ( ++idx < size); - property->setAllowedValues(allwdValues); + property->setValueProperty(std::make_shared(allwdValues)); + if (attType.empty()) + attType = "integer"; } } - return property; + else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1) + || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0)) + { + int size = cJSON_GetArraySize(allowedvalues); + int idx = 0; + std::vector allwdValues; + do + { + if (cJSON_GetArrayItem(allowedvalues, idx)->type) + allwdValues.push_back(true); + else + allwdValues.push_back(false); + } + while ( ++idx < size); + property->setValueProperty(std::make_shared(allwdValues)); + if (attType.empty()) + attType = "boolean"; + } } - void JsonSchema::readValues(cJSON *childProperties, PropertiesPtr property , + void JsonSchema::readValues(cJSON *childProperties, PropertiesPtr &property , const std::string &attType) { if (attType == "string") @@ -292,31 +356,36 @@ namespace RAML { readInteger(childProperties, property); } + else if (attType == "number") + { + readDouble(childProperties, property); + } else if (attType == "array") { readArray(childProperties, property); } - else if (attType == "number") + else if (attType == "object") { - readDouble(childProperties, property); + readObject(childProperties, property); } } - void JsonSchema::readString(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readString(cJSON *childProperties, PropertiesPtr &property) { cJSON *stringMax = cJSON_GetObjectItem(childProperties, "maxLength"); + int min = INT_MIN, max = INT_MAX; if (stringMax) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax (--(stringMax->valueint)); + max = --(stringMax->valueint); else - property->setMax(stringMax->valueint); + max = stringMax->valueint; } else - property->setMax(stringMax->valueint); + max = stringMax->valueint; } cJSON *stringMin = cJSON_GetObjectItem(childProperties, "minLength"); if (stringMin) @@ -325,109 +394,89 @@ namespace RAML if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(stringMin->valueint)); + min = ++(stringMin->valueint); else - property->setMin(stringMin->valueint); + min = stringMin->valueint; } else - property->setMin(stringMin->valueint); + min = stringMin->valueint; } + if (min != INT_MIN || max != INT_MAX) + property->setValueProperty(std::make_shared(min, max, 0)); + cJSON *stringFormat = cJSON_GetObjectItem(childProperties, "format"); if (stringFormat) { - property->setFormat(stringFormat->valuestring); + property->setValueProperty(std::make_shared + (ValueProperty::Type::FORMAT, (stringFormat->valuestring))); } + cJSON *stringPattern = cJSON_GetObjectItem(childProperties, "pattern"); if (stringPattern) { - property->setPattern(stringPattern->valuestring); + property->setValueProperty(std::make_shared + (ValueProperty::Type::PATTERN, (stringPattern->valuestring))); } } - void JsonSchema::readArray(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readInteger(cJSON *childProperties, PropertiesPtr &property) { - cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items"); - if (itemValues) - { - if (itemValues->type == 5) - { - int item_size = cJSON_GetArraySize(itemValues); - int item_index = 0; - do - { - cJSON *item = cJSON_GetArrayItem(itemValues, item_index); - property->setItem(readItems(item)); - } - while ( ++item_index < item_size); - } - else - { - property->setItem(readItems(itemValues)); - } - } - cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems"); - if (itemsMax) + cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); + int min = INT_MIN, max = INT_MAX, multipleOf = INT_MAX; + if (Max) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax( --(itemsMax->valueint)); + max = --(Max->valueint); else - property->setMax(itemsMax->valueint); + max = Max->valueint; } else - property->setMax(itemsMax->valueint); + max = Max->valueint; } - cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minLength"); - if (itemsMin) + cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); + if (Min) { cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum"); if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(itemsMin->valueint)); + min = ++(Min->valueint); else - property->setMin(itemsMin->valueint); + min = Min->valueint; } else - property->setMin(itemsMin->valueint); - } - cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems"); - if (uniqueItems) - { - property->setUnique(uniqueItems->type); - } - else - { - property->setUnique(cJSON_True); + min = Min->valueint; } - cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems"); - if (additionalItems) - { - property->setAdditionalItems(additionalItems->type); - } - else + cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf"); + if (MultipleOff) { - property->setAdditionalItems(cJSON_True); + multipleOf = MultipleOff->valueint; } + if (min != INT_MIN || max != INT_MAX) + property->setValueProperty(std::make_shared(min, max, multipleOf)); + } - void JsonSchema::readInteger(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readDouble(cJSON *childProperties, PropertiesPtr &property) { cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); + double min = INT_MIN, max = INT_MAX; + int multipleOf = INT_MAX; if (Max) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMax( --(Max->valueint)); + max = --(Max->valuedouble); else - property->setMax(Max->valueint); + max = Max->valuedouble; } else - property->setMax(Max->valueint); + max = Max->valuedouble; } cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); if (Min) @@ -436,423 +485,395 @@ namespace RAML if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMin( ++(Min->valueint)); + min = ++(Min->valuedouble); else - property->setMin(Min->valueint); + min = Min->valuedouble; } else - property->setMin(Min->valueint); + min = Min->valuedouble; } - cJSON *multipleOf = cJSON_GetObjectItem(childProperties, "multipleOf"); - if (multipleOf) + + cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf"); + if (MultipleOff) { - property->setMultipleOf(multipleOf->valueint); + multipleOf = MultipleOff->valueint; } + if (min != INT_MIN || max != INT_MAX) + property->setValueProperty(std::make_shared(min, max, multipleOf)); } - void JsonSchema::readDouble(cJSON *childProperties, PropertiesPtr property) + void JsonSchema::readArray(cJSON *childProperties, PropertiesPtr &property) { - cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum"); - if (Max) + cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items"); + if (itemValues) + { + if (itemValues->type == 5) + { + //int item_size = cJSON_GetArraySize(itemValues); + int item_index = 0; + //do + //{ + cJSON *item = cJSON_GetArrayItem(itemValues, item_index); + readItems(item, property); + //break; + //} + //while ( ++item_index < item_size); + } + else + { + readItems(itemValues, property); + } + } + cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems"); + int min = INT_MIN, max = INT_MAX; + bool unique = cJSON_False, addItems = cJSON_False; + if (itemsMax) { cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum"); if (exclusiveMax) { if (exclusiveMax->type == cJSON_True) - property->setMaxDouble( --(Max->valuedouble)); + max = --(itemsMax->valueint); else - property->setMaxDouble(Max->valuedouble); + max = itemsMax->valueint; } else - property->setMaxDouble(Max->valuedouble); + max = itemsMax->valueint; } - cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum"); - if (Min) + cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minItems"); + if (itemsMin) { cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum"); if (exclusiveMin) { if (exclusiveMin->type == cJSON_True) - property->setMinDouble( ++(Min->valuedouble)); + min = ++(itemsMin->valueint); else - property->setMinDouble(Min->valuedouble); + min = itemsMin->valueint; } else - property->setMinDouble(Min->valuedouble); + min = itemsMin->valueint; } - cJSON *multipleOf = cJSON_GetObjectItem(childProperties, "multipleOf"); - if (multipleOf) + cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems"); + if (uniqueItems) { - property->setMultipleOf(multipleOf->valueint); + unique = uniqueItems->type; } - + cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems"); + if (additionalItems) + { + addItems = additionalItems->type; + } + property->setValueProperty(std::make_shared + (ValueProperty::Type::ARRAY, min, max, unique, addItems)); } - DefinitionsPtr JsonSchema::readRef(std::string m_ref) + void JsonSchema::readItems(cJSON *item, PropertiesPtr &property) { - std::string delimiter1 = "#"; - std::string delimiter2 = "/"; - std::string fileName; - if (! m_ref.empty()) + std::string type; + JsonParameters param; + cJSON *itemType = cJSON_GetObjectItem(item, "type"); + if (itemType) { - std::size_t pos = m_ref.find(delimiter1); - if ( (pos = m_ref.find(delimiter1)) != std::string::npos) - { - fileName = m_ref.substr(0, pos); - m_ref.erase(0, pos); - } - m_ref.erase(0, delimiter1 .length()); - std::string defName; + type = itemType->valuestring; + } - if (! m_ref.empty()) + cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf"); + if (itemAllOf) + { + readAllOf(itemAllOf , param); + } + else + { + cJSON *itemOneOf = cJSON_GetObjectItem(item, "oneOf"); + if (itemOneOf) { - m_ref.erase(0, delimiter2 .length()); - std::string keyName; - if ( (pos = m_ref.find(delimiter2)) != std::string::npos) - { - keyName = m_ref.substr(0, pos); - m_ref.erase(0, pos + delimiter2.length()); - if (keyName == "definitions") - { - if ( (pos = m_ref.find(delimiter2)) != std::string::npos) - { - defName = m_ref.substr(0, pos); - } - else if (! m_ref.empty()) - { - defName = m_ref; - } - } - } + readAllOf(itemOneOf , param); } - if (!fileName.empty()) + } + + cJSON *itemReference = cJSON_GetObjectItem(item, "$ref"); + if (itemReference) + { + readJsonRef(itemReference , param); + } + + if (type == "object") + { + cJSON *itemProperties = cJSON_GetObjectItem(item, "properties"); + if (itemProperties) { - if (!(defName.empty())) + cJSON *childProperties = itemProperties->child; + std::vector propertyVector; + while (childProperties) { - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); - DefinitionsPtr definition = Refparser->getDefinition(defName); - if (definition == nullptr) - throw JsonException("Definition Name Incorrect"); - return definition; + std::string attName = childProperties->string; + PropertiesPtr prop = std::make_shared(attName); + readProp(childProperties, attName); + propertyVector.push_back(*prop); + childProperties = childProperties->next; } + property->setValue(propertyVector); } - else + + cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required"); + if (itemRequiredValues) { - if (!(defName.empty())) + int size = cJSON_GetArraySize(itemRequiredValues); + int index = 0; + do { - if (getDefinition(defName) == nullptr) - throw JsonException("Definition Name Incorrect"); - return getDefinition(defName); + property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring); } + while ( ++index < size); } } - throw JsonException("Definition Name Empty"); - return nullptr; - } - void JsonSchema::readAllOf(cJSON *allofValues) - { - int size = cJSON_GetArraySize(allofValues); - int index = 0; - do + + else if (param.getType() == "object") { - cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index); - cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref"); - if (jsonReference) + std::vector propertyVector; + for (auto prop : param.getProperties()) { - readJsonRef(jsonReference ); + propertyVector.push_back(*(prop.second)); } - cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required"); - if (jsonRequiredValues) + property->setValue(propertyVector); + + for (auto req : param.getRequired()) { - int len = cJSON_GetArraySize(jsonRequiredValues); - int idx = 0; - do - { - setRequiredValue(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring); - } - while ( ++idx < len); + property->setRequiredValue(req); } } - while ( ++index < size); - } - void JsonSchema::readJsonRef(cJSON *jsonReference) - { - std::string m_ref = jsonReference->valuestring; - std::map properties; - std::vector required; - - std::string web = "http://"; - std::string delimiter = "#"; - std::size_t pos = m_ref.find(web); - - if (pos == std::string::npos) // If Web Link Is GIVEN TO READ + else { - pos = m_ref.find(delimiter); - if ( pos == (m_ref.length() - 1) ) - { - std::string fileName = m_ref.substr(0, pos); - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); + PropertiesPtr prop = std::make_shared("property"); - properties = Refparser->getProperties(); - required = Refparser->getRequiredValues(); - } - else + cJSON *defaultValue = cJSON_GetObjectItem(item, "default"); + if (defaultValue) { - DefinitionsPtr definition = readRef(m_ref); - properties = definition->getProperties(); - required = definition->getRequiredValues(); + readDefaultValue(defaultValue, prop, type); } - for ( auto it : properties) + cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum"); + if (allowedvalues) { - std:: string name = it.first; - addProperty(name, it.second); + readAllowedValues(allowedvalues, prop, type); } - for (auto it : required ) - { - setRequiredValue(it); - } - + readValues(item, prop, type); + prop->setTypeString(type); + property->setValue(*prop); } } - void JsonSchema::readDefAllOf(cJSON *allofValues, DefinitionsPtr definition) + + void JsonSchema::readObject(cJSON *childProperties, PropertiesPtr &property) { - int size = cJSON_GetArraySize(allofValues); - int index = 0; - do + property->setTypeString("object"); + + cJSON *subProperties = cJSON_GetObjectItem(childProperties, "properties"); + cJSON *itemRequiredValues = cJSON_GetObjectItem(childProperties, "required"); + if (subProperties) { - cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index); - cJSON *defReference = cJSON_GetObjectItem(childAllOf, "$ref"); - if (defReference) + cJSON *childProperties = subProperties->child; + std::vector propertyVector; + while (childProperties) { - readDefRef(defReference , definition); + std::string attName = childProperties->string; + PropertiesPtr prop = std::make_shared(attName); + readProp(childProperties, attName); + propertyVector.push_back(*prop); + childProperties = childProperties->next; } - cJSON *defRequiredValues = cJSON_GetObjectItem(allofValues, "required"); - if (defRequiredValues) + property->setValue(propertyVector); + if (itemRequiredValues) { - int len = cJSON_GetArraySize(defRequiredValues); - int idx = 0; + int size = cJSON_GetArraySize(itemRequiredValues); + int index = 0; do { - definition->setRequiredValue(cJSON_GetArrayItem(defRequiredValues, idx)->valuestring); + property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring); } - while ( ++idx < len); + while ( ++index < size); } } - while ( ++index < size); - } - void JsonSchema::readDefRef(cJSON *defReference, DefinitionsPtr definition) - { - std::string m_ref = defReference->valuestring; - std::map properties; - std::vector required; - std::string type; - - std::string web = "http://"; - std::string delimiter = "#"; - std::size_t pos = m_ref.find(web); - - if (pos == std::string::npos) // If Web Link Is GIVEN TO READ + else { - pos = m_ref.find(delimiter); - if ( pos == (m_ref.length() - 1) ) - { - std::string fileName = m_ref.substr(0, pos); - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); + JsonParameters param; - properties = Refparser->getProperties(); - required = Refparser->getRequiredValues(); - type = Refparser->getType(); - } - else + cJSON *itemAllOf = cJSON_GetObjectItem(childProperties, "allOf"); + if (itemAllOf) { - DefinitionsPtr definitionRef = readRef(m_ref); - properties = definitionRef->getProperties(); - required = definitionRef->getRequiredValues(); - type = definitionRef->getType(); + readAllOf(itemAllOf , param); } - for (auto it : properties) + cJSON *itemReference = cJSON_GetObjectItem(childProperties, "$ref"); + if (itemReference) { - definition->addProperty(it.first, it.second); + readJsonRef(itemReference , param); } - for ( auto it : required) + + if (param.getType() == "object") { - definition->setRequiredValue(it); + std::vector propertyVector; + for (auto prop : param.getProperties()) + { + propertyVector.push_back(*(prop.second)); + } + property->setValue(propertyVector); + + for (auto req : param.getRequired()) + { + property->setRequiredValue(req); + } } - definition->setType(type); } } - ItemsPtr JsonSchema::readItems(cJSON *item) + + void JsonSchema::readFile(std::string &fileName , JsonParameters ¶m) { - ItemsPtr newItem = std::make_shared(); - cJSON *itemType = cJSON_GetObjectItem(item, "type"); - if (itemType) - { - std::string type = itemType->valuestring; - newItem->setType(type); - } + std::string name = fileName; + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + if (name.compare("oic.baseresource.json") == 0) + return; + + cJSON *json = m_includeResolver->readToJson(fileName); + JsonSchemaPtr Refparser = std::make_shared(json, m_includeResolver); - cJSON *itemProperties = cJSON_GetObjectItem(item, "properties"); - if (itemProperties) + param.addProperties(Refparser->getProperties()); + param.addRequired(Refparser->getRequiredValues()); + param.setType(Refparser->getType()); + } + + void JsonSchema::readFile(std::string &fileName , std::string &defName , JsonParameters ¶m) + { + std::string name = fileName; + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + if (name.compare("oic.baseresource.json") == 0) + return; + + cJSON *json = m_includeResolver->readToJson(fileName); + JsonSchemaPtr Refparser = std::make_shared(json, m_includeResolver); + + DefinitionsPtr definition = Refparser->getDefinition(defName); + if (definition == nullptr) + throw JsonException("Definition Name Incorrect"); + + param.addProperties(definition->getProperties()); + param.addRequired(definition->getRequiredValues()); + param.setType(definition->getType()); + } + + void JsonSchema::readRef(std::string ref , JsonParameters ¶m) + { + std::string delimiter1 = "#"; + std::string delimiter2 = "/"; + std::string fileName; + if (! ref.empty()) { - cJSON *childProperties = itemProperties->child; - while (childProperties) + std::size_t pos = std::string::npos; + if ( (pos = ref.find(delimiter1)) != std::string::npos) { - std::string attName = childProperties->string; - - newItem->addProperty(attName, readProp(childProperties, attName)); - childProperties = childProperties->next; + fileName = ref.substr(0, pos); + ref.erase(0, pos); } - } + ref.erase(0, delimiter1 .length()); + std::string defName; - cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum"); - if (allowedvalues) - { - if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4) + if (! ref.empty()) { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; - do + ref.erase(0, delimiter2 .length()); + std::string keyName; + if ( (pos = ref.find(delimiter2)) != std::string::npos) { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring); + keyName = ref.substr(0, pos); + ref.erase(0, pos + delimiter2.length()); + if (keyName == "definitions") + { + if ( (pos = ref.find(delimiter2)) != std::string::npos) + { + defName = ref.substr(0, pos); + } + else if (! ref.empty()) + { + defName = ref; + } + } } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); } - else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3) + if (!fileName.empty()) { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - if (newItem->getType() == "number") + if (!(defName.empty())) { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble); - } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); + readFile(fileName, defName, param); } else { - std::vector allwdValues; - do - { - allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint); - } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); + throw JsonException("Definition Name Empty"); } } - else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1) - || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0)) + else { - int size = cJSON_GetArraySize(allowedvalues); - int idx = 0; - std::vector allwdValues; - do + if (!(defName.empty())) { - if (cJSON_GetArrayItem(allowedvalues, idx)->type) - allwdValues.push_back(true); - else - allwdValues.push_back(false); + if (getDefinition(defName) == nullptr) + throw JsonException("Definition Name Incorrect"); + param.addProperties(getDefinition(defName)->getProperties()); + param.addRequired(getDefinition(defName)->getRequiredValues()); + param.setType(getDefinition(defName)->getType()); + } + else + { + throw JsonException("Definition Name Empty"); } - while ( ++idx < size); - newItem->setAllowedValues(allwdValues); - } - } - cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required"); - if (itemRequiredValues) - { - int size = cJSON_GetArraySize(itemRequiredValues); - int index = 0; - do - { - newItem->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring); } - while ( ++index < size); } - cJSON *itemReference = cJSON_GetObjectItem(item, "$ref"); - if (itemReference) - { - readItemRef(itemReference , newItem); - } - cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf"); - if (itemAllOf) - { - readItemAllOf(itemAllOf , newItem); - } - return newItem; } - void JsonSchema::readItemRef(cJSON *itemReference, ItemsPtr item) + void JsonSchema::readJsonRef(cJSON *jsonReference , JsonParameters ¶m) { - std::string m_ref = itemReference->valuestring; - std::map properties; - std::vector required; - std::string type; + std::string ref = jsonReference->valuestring; std::string web = "http://"; std::string delimiter = "#"; - std::size_t pos = m_ref.find(web); + std::size_t pos = ref.find(web); if (pos == std::string::npos) // If Web Link Is GIVEN TO READ { - pos = m_ref.find(delimiter); - if ( pos == (m_ref.length() - 1 ) ) + pos = ref.find(delimiter); + if ( pos == (ref.length() - 1) ) { - std::string fileName = m_ref.substr(0, pos); - cJSON *m_json = m_includeResolver->readToJson(fileName); - JsonSchemaPtr Refparser = std::make_shared(m_json, m_includeResolver); - - properties = Refparser->getProperties(); - required = Refparser->getRequiredValues(); - type = Refparser->getType(); + std::string fileName = ref.substr(0, pos); + readFile(fileName, param); } else { - DefinitionsPtr definitionRef = readRef(m_ref); - properties = definitionRef->getProperties(); - required = definitionRef->getRequiredValues(); - type = definitionRef->getType(); - } - for ( auto it : properties) - { - std:: string name = it.first; - item->addProperty(name, it.second); - } - for ( auto it : required) - { - item->setRequiredValue(it); + readRef(ref, param); } - item->setType(type); } } - void JsonSchema::readItemAllOf(cJSON *allofValues, ItemsPtr item) + void JsonSchema::readAllOf(cJSON *allofValues , JsonParameters &allParams) { int size = cJSON_GetArraySize(allofValues); int index = 0; do { + JsonParameters param; + cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index); - cJSON *itemReference = cJSON_GetObjectItem(childAllOf, "$ref"); - if (itemReference) + cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref"); + if (jsonReference) { - readItemRef(itemReference, item); + readJsonRef(jsonReference, param); + allParams.addProperties(param.getProperties()); + allParams.addRequired(param.getRequired()); + allParams.setType(param.getType()); } - cJSON *itemRequiredValues = cJSON_GetObjectItem(allofValues, "required"); - if (itemRequiredValues) + cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required"); + if (jsonRequiredValues) { - int len = cJSON_GetArraySize(itemRequiredValues); + int len = cJSON_GetArraySize(jsonRequiredValues); int idx = 0; do { - item->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, idx)->valuestring); + allParams.addRequired(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring); } while ( ++idx < len); }