+/******************************************************************\r
+ *\r
+ * Copyright 2015 Samsung Electronics All Rights Reserved.\r
+ *\r
+ *\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ ******************************************************************/\r
+\r
+#include "JsonSchema.h"\r
+using namespace std;\r
+\r
+namespace RAML\r
+{\r
+\r
+ void JsonSchema::readJson()\r
+ {\r
+ if (! m_cjson)\r
+ return;\r
+\r
+ cJSON *jsonId = cJSON_GetObjectItem(m_cjson, "id");\r
+ if (jsonId)\r
+ {\r
+ m_id = jsonId->valuestring;\r
+ }\r
+ cJSON *jsonSchema = cJSON_GetObjectItem(m_cjson, "$schema");\r
+ if (jsonSchema)\r
+ {\r
+ m_schema = jsonSchema->valuestring;\r
+ }\r
+ cJSON *jsonTitle = cJSON_GetObjectItem(m_cjson, "title");\r
+ if (jsonTitle)\r
+ {\r
+ m_title = jsonTitle->valuestring;\r
+ }\r
+ cJSON *jsonType = cJSON_GetObjectItem(m_cjson, "type");\r
+ if (jsonType)\r
+ {\r
+ m_type = jsonType->valuestring;\r
+ }\r
+ cJSON *jsonDescription = cJSON_GetObjectItem(m_cjson, "description");\r
+ if (jsonDescription)\r
+ {\r
+ m_description = jsonDescription->valuestring;\r
+ }\r
+ cJSON *jsonDefinitions = cJSON_GetObjectItem(m_cjson, "definitions");\r
+ if (jsonDefinitions)\r
+ {\r
+ cJSON *childDefinitions = jsonDefinitions->child;\r
+ while (childDefinitions)\r
+ {\r
+ std::string defName = childDefinitions->string;\r
+ addDefinition(defName, readDef(childDefinitions, defName));\r
+ childDefinitions = childDefinitions->next;\r
+ }\r
+ }\r
+ cJSON *jsonProperties = cJSON_GetObjectItem(m_cjson, "properties");\r
+ if (jsonProperties)\r
+ {\r
+ cJSON *childProperties = jsonProperties->child;\r
+ while (childProperties)\r
+ {\r
+ std::string attName = childProperties->string;\r
+ addProperty(attName, readProp(childProperties, attName));\r
+ childProperties = childProperties->next;\r
+ }\r
+ }\r
+ if (m_type == "array")\r
+ {\r
+ cJSON *jsonItems = cJSON_GetObjectItem(m_cjson, "items");\r
+ if (jsonItems)\r
+ {\r
+ if (jsonItems->type == 5)\r
+ {\r
+ int item_size = cJSON_GetArraySize(jsonItems);\r
+ int item_index = 0;\r
+ do\r
+ {\r
+ cJSON *item = cJSON_GetArrayItem(jsonItems, item_index);\r
+ Items *newItem = readItems(item);\r
+ setItem(newItem);\r
+ }\r
+ while ( ++item_index < item_size);\r
+ }\r
+ else\r
+ {\r
+ Items *newItem = readItems(jsonItems);\r
+ setItem(newItem);\r
+ }\r
+ }\r
+ }\r
+ cJSON *jsonAdditionalProperties = cJSON_GetObjectItem(m_cjson, "additionalProperties");\r
+ if (jsonAdditionalProperties)\r
+ m_additionalProperties = jsonAdditionalProperties->type;\r
+ else\r
+ m_additionalProperties = cJSON_True;\r
+\r
+ cJSON *jsonReference = cJSON_GetObjectItem(m_cjson, "$ref");\r
+ if (jsonReference)\r
+ {\r
+ readJsonRef(jsonReference);\r
+ }\r
+ cJSON *jsonAllOf = cJSON_GetObjectItem(m_cjson, "allOf");\r
+ if (jsonAllOf)\r
+ {\r
+ readAllOf(jsonAllOf);\r
+ }\r
+ cJSON *jsonRequiredValues = cJSON_GetObjectItem(m_cjson, "required");\r
+ if (jsonRequiredValues)\r
+ {\r
+ int size = cJSON_GetArraySize(jsonRequiredValues);\r
+ int index = 0;\r
+ do\r
+ {\r
+ setRequiredValue(cJSON_GetArrayItem(jsonRequiredValues, index)->valuestring);\r
+ }\r
+ while ( ++index < size);\r
+ }\r
+ }\r
+\r
+ Definitions *JsonSchema::readDef(cJSON *childDefinitions, const std::string &defName)\r
+ {\r
+ Definitions *definition = new Definitions(defName);\r
+\r
+ cJSON *defType = cJSON_GetObjectItem(childDefinitions, "type");\r
+ if (defType)\r
+ {\r
+ std::string type = defType->valuestring;\r
+ definition->setType(type);\r
+ }\r
+ cJSON *defProperties = cJSON_GetObjectItem(childDefinitions, "properties");\r
+ if (defProperties)\r
+ {\r
+ cJSON *childProperties = defProperties->child;\r
+ while (childProperties)\r
+ {\r
+ std::string attName = childProperties->string;\r
+ definition->addProperty(attName, readProp(childProperties, attName));\r
+ childProperties = childProperties->next;\r
+ }\r
+ }\r
+ cJSON *defRequiredValues = cJSON_GetObjectItem(childDefinitions, "required");\r
+ if (defRequiredValues)\r
+ {\r
+ int size = cJSON_GetArraySize(defRequiredValues);\r
+ int index = 0;\r
+ do\r
+ {\r
+ definition->setRequiredValue(cJSON_GetArrayItem(defRequiredValues, index)->valuestring);\r
+ }\r
+ while ( ++index < size);\r
+ }\r
+ cJSON *defReference = cJSON_GetObjectItem(childDefinitions, "$ref");\r
+ if (defReference)\r
+ {\r
+ readDefRef(defReference, definition);\r
+ }\r
+ cJSON *defAllOf = cJSON_GetObjectItem(childDefinitions, "allOf");\r
+ if (defAllOf)\r
+ {\r
+ readDefAllOf(defAllOf, definition);\r
+ }\r
+ return definition;\r
+ }\r
+\r
+ Properties *JsonSchema::readProp(cJSON *childProperties, const std::string &attName )\r
+ {\r
+ Properties *property = new Properties(attName);\r
+\r
+ cJSON *propertyUpdateFrequency = cJSON_GetObjectItem(childProperties, "update_frequency");\r
+ if (propertyUpdateFrequency)\r
+ {\r
+ property->setUpdateFrequencyTime(propertyUpdateFrequency->valueint);\r
+ }\r
+ cJSON *propertyDescription = cJSON_GetObjectItem(childProperties, "description");\r
+ if (propertyDescription)\r
+ {\r
+ property->setDescription(propertyDescription->valuestring);\r
+ }\r
+ cJSON *propertyType = cJSON_GetObjectItem(childProperties, "type");\r
+ if (propertyType)\r
+ {\r
+ std::string attType;\r
+ if (propertyType->type == 4)\r
+ {\r
+ attType = propertyType->valuestring;\r
+ property->setType(attType);\r
+ }\r
+ else if (propertyType->type == 5)\r
+ {\r
+ attType = cJSON_GetArrayItem(propertyType, 0)->valuestring;\r
+ property->setType(attType);\r
+ }\r
+ readValues(childProperties, property, attType);\r
+ }\r
+ cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default");\r
+ if (defaultValue)\r
+ {\r
+ if (defaultValue->type == 4)\r
+ {\r
+ property->setValue((std::string)defaultValue->valuestring);\r
+ }\r
+ else if (defaultValue->type == 3)\r
+ {\r
+ if (property->getType() == "number")\r
+ property->setValue((double)defaultValue->valuedouble);\r
+ else\r
+ property->setValue((int)defaultValue->valueint );\r
+ }\r
+ else if (defaultValue->type == 1)\r
+ {\r
+ property->setValue((bool)true);\r
+ }\r
+ else if (defaultValue->type == 0)\r
+ {\r
+ property->setValue((bool)false);\r
+ }\r
+\r
+ }\r
+ cJSON *allowedvalues = cJSON_GetObjectItem(childProperties, "enum");\r
+ if (allowedvalues)\r
+ {\r
+ if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4)\r
+ {\r
+ int size = cJSON_GetArraySize(allowedvalues);\r
+ int idx = 0;\r
+ std::vector<std::string> allwdValues;\r
+ do\r
+ {\r
+ allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring);\r
+ }\r
+ while ( ++idx < size);\r
+ property->setAllowedValues(allwdValues);\r
+ }\r
+ else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3)\r
+ {\r
+ int size = cJSON_GetArraySize(allowedvalues);\r
+ int idx = 0;\r
+ if (property->getType() == "number")\r
+ {\r
+ std::vector<double> allwdValues;\r
+ do\r
+ {\r
+ allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble);\r
+ }\r
+ while ( ++idx < size);\r
+ property->setAllowedValues(allwdValues);\r
+ }\r
+ else\r
+ {\r
+ std::vector<int> allwdValues;\r
+ do\r
+ {\r
+ allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint);\r
+ }\r
+ while ( ++idx < size);\r
+ property->setAllowedValues(allwdValues);\r
+ }\r
+ }\r
+ else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1)\r
+ || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0))\r
+ {\r
+ int size = cJSON_GetArraySize(allowedvalues);\r
+ int idx = 0;\r
+ std::vector<bool> allwdValues;\r
+ do\r
+ {\r
+ if (cJSON_GetArrayItem(allowedvalues, idx)->type)\r
+ allwdValues.push_back(true);\r
+ else\r
+ allwdValues.push_back(false);\r
+ }\r
+ while ( ++idx < size);\r
+ property->setAllowedValues(allwdValues);\r
+ }\r
+ }\r
+ return property;\r
+ }\r
+\r
+ void JsonSchema::readValues(cJSON *childProperties, Properties *property ,\r
+ const std::string &attType)\r
+ {\r
+ if (attType == "string")\r
+ {\r
+ readString(childProperties, property);\r
+ }\r
+ else if (attType == "integer")\r
+ {\r
+ readNumber(childProperties, property);\r
+ }\r
+ else if (attType == "array")\r
+ {\r
+ readArray(childProperties, property);\r
+ }\r
+ else if (attType == "number")\r
+ {\r
+ readNumber(childProperties, property);\r
+ }\r
+ }\r
+\r
+ void JsonSchema::readString(cJSON *childProperties, Properties *property)\r
+ {\r
+ cJSON *stringMax = cJSON_GetObjectItem(childProperties, "maxLength");\r
+ if (stringMax)\r
+ {\r
+ cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
+ if (exclusiveMax)\r
+ {\r
+ if (exclusiveMax->type == cJSON_True)\r
+ property->setMax (--(stringMax->valueint));\r
+ else\r
+ property->setMax(stringMax->valueint);\r
+ }\r
+ else\r
+ property->setMax(stringMax->valueint);\r
+ }\r
+ cJSON *stringMin = cJSON_GetObjectItem(childProperties, "minLength");\r
+ if (stringMin)\r
+ {\r
+ cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
+ if (exclusiveMin)\r
+ {\r
+ if (exclusiveMin->type == cJSON_True)\r
+ property->setMin( ++(stringMin->valueint));\r
+ else\r
+ property->setMin(stringMin->valueint);\r
+ }\r
+ else\r
+ property->setMin(stringMin->valueint);\r
+ }\r
+ cJSON *stringFormat = cJSON_GetObjectItem(childProperties, "format");\r
+ if (stringFormat)\r
+ {\r
+ property->setFormat(stringFormat->valuestring);\r
+ }\r
+ cJSON *stringPattern = cJSON_GetObjectItem(childProperties, "pattern");\r
+ if (stringPattern)\r
+ {\r
+ property->setPattern(stringPattern->valuestring);\r
+ }\r
+ }\r
+\r
+ void JsonSchema::readArray(cJSON *childProperties, Properties *property)\r
+ {\r
+ cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items");\r
+ if (itemValues)\r
+ {\r
+ if (itemValues->type == 5)\r
+ {\r
+ int item_size = cJSON_GetArraySize(itemValues);\r
+ int item_index = 0;\r
+ do\r
+ {\r
+ cJSON *item = cJSON_GetArrayItem(itemValues, item_index);\r
+ Items *newItem = readItems(item);\r
+ property->setItem(newItem);\r
+ }\r
+ while ( ++item_index < item_size);\r
+ }\r
+ else\r
+ {\r
+ Items *newItem = readItems(itemValues);\r
+ property->setItem(newItem);\r
+ }\r
+ }\r
+ cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems");\r
+ if (itemsMax)\r
+ {\r
+ cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
+ if (exclusiveMax)\r
+ {\r
+ if (exclusiveMax->type == cJSON_True)\r
+ property->setMax( --(itemsMax->valueint));\r
+ else\r
+ property->setMax(itemsMax->valueint);\r
+ }\r
+ else\r
+ property->setMax(itemsMax->valueint);\r
+ }\r
+ cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minLength");\r
+ if (itemsMin)\r
+ {\r
+ cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
+ if (exclusiveMin)\r
+ {\r
+ if (exclusiveMin->type == cJSON_True)\r
+ property->setMin( ++(itemsMin->valueint));\r
+ else\r
+ property->setMin(itemsMin->valueint);\r
+ }\r
+ else\r
+ property->setMin(itemsMin->valueint);\r
+ }\r
+ cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems");\r
+ if (uniqueItems)\r
+ {\r
+ property->setUnique(uniqueItems->type);\r
+ }\r
+ else\r
+ {\r
+ property->setUnique(cJSON_True);\r
+ }\r
+ cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems");\r
+ if (additionalItems)\r
+ {\r
+ property->setAdditionalItems(additionalItems->type);\r
+ }\r
+ else\r
+ {\r
+ property->setAdditionalItems(cJSON_True);\r
+ }\r
+ }\r
+\r
+ void JsonSchema::readNumber(cJSON *childProperties, Properties *property)\r
+ {\r
+ cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum");\r
+ if (Max)\r
+ {\r
+ cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
+ if (exclusiveMax)\r
+ {\r
+ if (exclusiveMax->type == cJSON_True)\r
+ property->setMax( --(Max->valueint));\r
+ else\r
+ property->setMax(Max->valueint);\r
+ }\r
+ else\r
+ property->setMax(Max->valueint);\r
+ }\r
+ cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum");\r
+ if (Min)\r
+ {\r
+ cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
+ if (exclusiveMin)\r
+ {\r
+ if (exclusiveMin->type == cJSON_True)\r
+ property->setMin( ++(Min->valueint));\r
+ else\r
+ property->setMin(Min->valueint);\r
+ }\r
+ else\r
+ property->setMin(Min->valueint);\r
+ }\r
+ cJSON *multipleOf = cJSON_GetObjectItem(childProperties, "multipleOf");\r
+ if (multipleOf)\r
+ {\r
+ property->setMultipleOf(multipleOf->valueint);\r
+ }\r
+\r
+ }\r
+ Definitions *JsonSchema::readRef(std::string m_ref)\r
+ {\r
+ std::string delimiter1 = "#";\r
+ std::string delimiter2 = "/";\r
+ std::string fileName;\r
+ if (! m_ref.empty())\r
+ {\r
+ std::size_t pos = m_ref.find(delimiter1);\r
+ if ( (pos = m_ref.find(delimiter1)) != std::string::npos)\r
+ {\r
+ fileName = m_ref.substr(0, pos);\r
+ m_ref.erase(0, pos);\r
+ }\r
+ m_ref.erase(0, delimiter1 .length());\r
+ std::string defName;\r
+\r
+ if (! m_ref.empty())\r
+ {\r
+ m_ref.erase(0, delimiter2 .length());\r
+ std::string keyName;\r
+ if ( (pos = m_ref.find(delimiter2)) != std::string::npos)\r
+ {\r
+ keyName = m_ref.substr(0, pos);\r
+ m_ref.erase(0, pos + delimiter2.length());\r
+ if (keyName == "definitions")\r
+ {\r
+ if ( (pos = m_ref.find(delimiter2)) != std::string::npos)\r
+ {\r
+ defName = m_ref.substr(0, pos);\r
+ }\r
+ else if (! m_ref.empty())\r
+ {\r
+ defName = m_ref;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ if (!fileName.empty())\r
+ {\r
+ if (!(defName.empty()))\r
+ {\r
+ cJSON *m_json = m_includeResolver->readToJson(fileName);\r
+ JsonSchema *Refparser = new JsonSchema(m_json, m_includeResolver);\r
+ Definitions *definition = Refparser->getDefinition(defName);\r
+ if (definition == nullptr)\r
+ throw JsonException("Definition Name Incorrect");\r
+ return definition;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (!(defName.empty()))\r
+ {\r
+ if (getDefinition(defName) == nullptr)\r
+ throw JsonException("Definition Name Incorrect");\r
+ return getDefinition(defName);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ void JsonSchema::readAllOf(cJSON *allofValues)\r
+ {\r
+ int size = cJSON_GetArraySize(allofValues);\r
+ int index = 0;\r
+ do\r
+ {\r
+ cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index);\r
+ cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref");\r
+ if (jsonReference)\r
+ {\r
+ readJsonRef(jsonReference );\r
+ }\r
+ cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required");\r
+ if (jsonRequiredValues)\r
+ {\r
+ int len = cJSON_GetArraySize(jsonRequiredValues);\r
+ int idx = 0;\r
+ do\r
+ {\r
+ setRequiredValue(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring);\r
+ }\r
+ while ( ++idx < len);\r
+ }\r
+ }\r
+ while ( ++index < size);\r
+ }\r
+ void JsonSchema::readJsonRef(cJSON *jsonReference)\r
+ {\r
+ std::string m_ref = jsonReference->valuestring;\r
+ std::map<std::string, Properties *> properties;\r
+ std::vector<std::string> required;\r
+\r
+ std::string web = "http://";\r
+ std::string delimiter = "#";\r
+ std::size_t pos = m_ref.find(web);\r
+\r
+ if (pos == std::string::npos) // If Web Link Is GIVEN TO READ\r
+ {\r
+ pos = m_ref.find(delimiter);\r
+ if ( pos == (m_ref.length() - 1) )\r
+ {\r
+ std::string fileName = m_ref.substr(0, pos);\r
+ cJSON *m_json = m_includeResolver->readToJson(fileName);\r
+ JsonSchema *Refparser = new JsonSchema(m_json, m_includeResolver);\r
+\r
+ properties = Refparser->getProperties();\r
+ required = Refparser->getRequiredValues();\r
+ }\r
+ else\r
+ {\r
+ Definitions *definition = readRef(m_ref);\r
+ properties = definition->getProperties();\r
+ required = definition->getRequiredValues();\r
+ }\r
+ for ( auto it : properties)\r
+ {\r
+ std:: string name = it.first;\r
+ addProperty(name, it.second);\r
+ }\r
+ for (auto it : required )\r
+ {\r
+ setRequiredValue(it);\r
+ }\r
+\r
+ }\r
+ }\r
+ void JsonSchema::readDefAllOf(cJSON *allofValues, Definitions *definition)\r
+ {\r
+ int size = cJSON_GetArraySize(allofValues);\r
+ int index = 0;\r
+ do\r
+ {\r
+ cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index);\r
+ cJSON *defReference = cJSON_GetObjectItem(childAllOf, "$ref");\r
+ if (defReference)\r
+ {\r
+ readDefRef(defReference , definition);\r
+ }\r
+ cJSON *defRequiredValues = cJSON_GetObjectItem(allofValues, "required");\r
+ if (defRequiredValues)\r
+ {\r
+ int len = cJSON_GetArraySize(defRequiredValues);\r
+ int idx = 0;\r
+ do\r
+ {\r
+ definition->setRequiredValue(cJSON_GetArrayItem(defRequiredValues, idx)->valuestring);\r
+ }\r
+ while ( ++idx < len);\r
+ }\r
+ }\r
+ while ( ++index < size);\r
+ }\r
+ void JsonSchema::readDefRef(cJSON *defReference, Definitions *definition)\r
+ {\r
+ std::string m_ref = defReference->valuestring;\r
+ std::map<std::string, Properties *> properties;\r
+ std::vector<std::string> required;\r
+ std::string type;\r
+\r
+ std::string web = "http://";\r
+ std::string delimiter = "#";\r
+ std::size_t pos = m_ref.find(web);\r
+\r
+ if (pos == std::string::npos) // If Web Link Is GIVEN TO READ\r
+ {\r
+ pos = m_ref.find(delimiter);\r
+ if ( pos == (m_ref.length() - 1) )\r
+ {\r
+ std::string fileName = m_ref.substr(0, pos);\r
+ cJSON *m_json = m_includeResolver->readToJson(fileName);\r
+ JsonSchema *Refparser = new JsonSchema(m_json, m_includeResolver);\r
+\r
+ properties = Refparser->getProperties();\r
+ required = Refparser->getRequiredValues();\r
+ type = Refparser->getType();\r
+ }\r
+ else\r
+ {\r
+ Definitions *definitionRef = readRef(m_ref);\r
+ properties = definitionRef->getProperties();\r
+ required = definitionRef->getRequiredValues();\r
+ type = definitionRef->getType();\r
+ }\r
+ for (auto it : properties)\r
+ {\r
+ definition->addProperty(it.first, it.second);\r
+ }\r
+ for ( auto it : required)\r
+ {\r
+ definition->setRequiredValue(it);\r
+ }\r
+ definition->setType(type);\r
+ }\r
+ }\r
+ Items *JsonSchema::readItems(cJSON *item)\r
+ {\r
+ Items *newItem = new Items();\r
+ cJSON *itemType = cJSON_GetObjectItem(item, "type");\r
+ if (itemType)\r
+ {\r
+ std::string type = itemType->valuestring;\r
+ newItem->setType(type);\r
+ }\r
+\r
+ cJSON *itemProperties = cJSON_GetObjectItem(item, "properties");\r
+ if (itemProperties)\r
+ {\r
+ cJSON *childProperties = itemProperties->child;\r
+ while (childProperties)\r
+ {\r
+ std::string attName = childProperties->string;\r
+ Properties *property = readProp(childProperties, attName);\r
+\r
+ newItem->addProperty(attName, property);\r
+ childProperties = childProperties->next;\r
+ }\r
+ }\r
+\r
+ cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum");\r
+ if (allowedvalues)\r
+ {\r
+ if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4)\r
+ {\r
+ int size = cJSON_GetArraySize(allowedvalues);\r
+ int idx = 0;\r
+ std::vector<std::string> allwdValues;\r
+ do\r
+ {\r
+ allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring);\r
+ }\r
+ while ( ++idx < size);\r
+ newItem->setAllowedValues(allwdValues);\r
+ }\r
+ else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3)\r
+ {\r
+ int size = cJSON_GetArraySize(allowedvalues);\r
+ int idx = 0;\r
+ if (newItem->getType() == "number")\r
+ {\r
+ std::vector<double> allwdValues;\r
+ do\r
+ {\r
+ allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble);\r
+ }\r
+ while ( ++idx < size);\r
+ newItem->setAllowedValues(allwdValues);\r
+ }\r
+ else\r
+ {\r
+ std::vector<int> allwdValues;\r
+ do\r
+ {\r
+ allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint);\r
+ }\r
+ while ( ++idx < size);\r
+ newItem->setAllowedValues(allwdValues);\r
+ }\r
+ }\r
+ else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1)\r
+ || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0))\r
+ {\r
+ int size = cJSON_GetArraySize(allowedvalues);\r
+ int idx = 0;\r
+ std::vector<bool> allwdValues;\r
+ do\r
+ {\r
+ if (cJSON_GetArrayItem(allowedvalues, idx)->type)\r
+ allwdValues.push_back(true);\r
+ else\r
+ allwdValues.push_back(false);\r
+ }\r
+ while ( ++idx < size);\r
+ newItem->setAllowedValues(allwdValues);\r
+ }\r
+ }\r
+ cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required");\r
+ if (itemRequiredValues)\r
+ {\r
+ int size = cJSON_GetArraySize(itemRequiredValues);\r
+ int index = 0;\r
+ do\r
+ {\r
+ newItem->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
+ }\r
+ while ( ++index < size);\r
+ }\r
+ cJSON *itemReference = cJSON_GetObjectItem(item, "$ref");\r
+ if (itemReference)\r
+ {\r
+ readItemRef(itemReference , newItem);\r
+ }\r
+ cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf");\r
+ if (itemAllOf)\r
+ {\r
+ readItemAllOf(itemAllOf , newItem);\r
+ }\r
+ return (newItem);\r
+ }\r
+\r
+ void JsonSchema::readItemRef(cJSON *itemReference, Items *item)\r
+ {\r
+ std::string m_ref = itemReference->valuestring;\r
+ std::map<std::string, Properties *> properties;\r
+ std::vector<std::string> required;\r
+ std::string type;\r
+\r
+ std::string web = "http://";\r
+ std::string delimiter = "#";\r
+ std::size_t pos = m_ref.find(web);\r
+\r
+ if (pos == std::string::npos) // If Web Link Is GIVEN TO READ\r
+ {\r
+ pos = m_ref.find(delimiter);\r
+ if ( pos == (m_ref.length() - 1 ) )\r
+ {\r
+ std::string fileName = m_ref.substr(0, pos);\r
+ cJSON *m_json = m_includeResolver->readToJson(fileName);\r
+ JsonSchema *Refparser = new JsonSchema(m_json, m_includeResolver);\r
+\r
+ properties = Refparser->getProperties();\r
+ required = Refparser->getRequiredValues();\r
+ type = Refparser->getType();\r
+ }\r
+ else\r
+ {\r
+ Definitions *definitionRef = readRef(m_ref);\r
+ properties = definitionRef->getProperties();\r
+ required = definitionRef->getRequiredValues();\r
+ type = definitionRef->getType();\r
+ }\r
+ for ( auto it : properties)\r
+ {\r
+ std:: string name = it.first;\r
+ item->addProperty(name, it.second);\r
+ }\r
+ for ( auto it : required)\r
+ {\r
+ item->setRequiredValue(it);\r
+ }\r
+ item->setType(type);\r
+ }\r
+ }\r
+\r
+ void JsonSchema::readItemAllOf(cJSON *allofValues, Items *item)\r
+ {\r
+ int size = cJSON_GetArraySize(allofValues);\r
+ int index = 0;\r
+ do\r
+ {\r
+ cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index);\r
+ cJSON *itemReference = cJSON_GetObjectItem(childAllOf, "$ref");\r
+ if (itemReference)\r
+ {\r
+ readItemRef(itemReference, item);\r
+ }\r
+ cJSON *itemRequiredValues = cJSON_GetObjectItem(allofValues, "required");\r
+ if (itemRequiredValues)\r
+ {\r
+ int len = cJSON_GetArraySize(itemRequiredValues);\r
+ int idx = 0;\r
+ do\r
+ {\r
+ item->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, idx)->valuestring);\r
+ }\r
+ while ( ++idx < len);\r
+ }\r
+ }\r
+ while ( ++index < size);\r
+ }\r
+}\r
+\r