Design Changes to JsonSchema Parser
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / jsonSchemaParser / JsonSchema.cpp
1 /******************************************************************\r
2  *\r
3  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 /**\r
22  * @file   JsonSchema.cpp\r
23  *\r
24  * @brief   This file reads data from Json Schema.\r
25  */\r
26 \r
27 #include "JsonSchema.h"\r
28 #include <iostream>\r
29 namespace RAML\r
30 {\r
31 \r
32     void JsonSchema::readJson()\r
33     {\r
34         if (! m_cjson)\r
35             return;\r
36 \r
37         cJSON *jsonId = cJSON_GetObjectItem(m_cjson, "id");\r
38         if (jsonId)\r
39         {\r
40             m_id = jsonId->valuestring;\r
41         }\r
42         cJSON *jsonSchema = cJSON_GetObjectItem(m_cjson, "$schema");\r
43         if (jsonSchema)\r
44         {\r
45             m_schema = jsonSchema->valuestring;\r
46         }\r
47         cJSON *jsonTitle = cJSON_GetObjectItem(m_cjson, "title");\r
48         if (jsonTitle)\r
49         {\r
50             m_title = jsonTitle->valuestring;\r
51         }\r
52         cJSON *jsonType = cJSON_GetObjectItem(m_cjson, "type");\r
53         if (jsonType)\r
54         {\r
55             m_type = jsonType->valuestring;\r
56         }\r
57         cJSON *jsonDescription = cJSON_GetObjectItem(m_cjson, "description");\r
58         if (jsonDescription)\r
59         {\r
60             m_description = jsonDescription->valuestring;\r
61         }\r
62         cJSON *jsonDefinitions = cJSON_GetObjectItem(m_cjson, "definitions");\r
63         if (jsonDefinitions)\r
64         {\r
65             cJSON *childDefinitions = jsonDefinitions->child;\r
66             while (childDefinitions)\r
67             {\r
68                 std::string defName = childDefinitions->string;\r
69                 addDefinition(defName, readDef(childDefinitions, defName));\r
70                 childDefinitions = childDefinitions->next;\r
71             }\r
72         }\r
73         cJSON *jsonProperties = cJSON_GetObjectItem(m_cjson, "properties");\r
74         if (jsonProperties)\r
75         {\r
76             cJSON *childProperties = jsonProperties->child;\r
77             while (childProperties)\r
78             {\r
79                 std::string attName = childProperties->string;\r
80                 addProperty(attName, readProp(childProperties, attName));\r
81                 childProperties = childProperties->next;\r
82             }\r
83         }\r
84         if (m_type == "array")\r
85         {\r
86             PropertiesPtr property = std::make_shared<Properties>("array");\r
87             readArray(m_cjson, property);\r
88             addProperty("array" , property);\r
89         }\r
90 \r
91         cJSON *jsonAdditionalProperties = cJSON_GetObjectItem(m_cjson, "additionalProperties");\r
92         if (jsonAdditionalProperties)\r
93             m_additionalProperties = jsonAdditionalProperties->type;\r
94         else\r
95             m_additionalProperties = cJSON_True;\r
96 \r
97         cJSON *jsonReference = cJSON_GetObjectItem(m_cjson, "$ref");\r
98         if (jsonReference)\r
99         {\r
100             JsonParameters param;\r
101             readJsonRef(jsonReference, param);\r
102 \r
103             for (auto it : param.getProperties())\r
104             {\r
105                 addProperty(it.first, it.second);\r
106             }\r
107             for ( auto it : param.getRequired())\r
108             {\r
109                 setRequiredValue(it);\r
110             }\r
111             if (m_type.empty())\r
112                 m_type = param.getType();\r
113         }\r
114         cJSON *jsonAllOf = cJSON_GetObjectItem(m_cjson, "allOf");\r
115         if (jsonAllOf)\r
116         {\r
117             JsonParameters param;\r
118 \r
119             readAllOf(jsonAllOf, param);\r
120 \r
121             for (auto it : param.getProperties())\r
122             {\r
123                 addProperty(it.first, it.second);\r
124             }\r
125             for ( auto it : param.getRequired())\r
126             {\r
127                 setRequiredValue(it);\r
128             }\r
129             if (m_type.empty())\r
130                 m_type = param.getType();\r
131         }\r
132         cJSON *jsonRequiredValues = cJSON_GetObjectItem(m_cjson, "required");\r
133         if (jsonRequiredValues)\r
134         {\r
135             int size = cJSON_GetArraySize(jsonRequiredValues);\r
136             int index = 0;\r
137             do\r
138             {\r
139                 setRequiredValue(cJSON_GetArrayItem(jsonRequiredValues, index)->valuestring);\r
140             }\r
141             while ( ++index < size);\r
142         }\r
143     }\r
144 \r
145     DefinitionsPtr JsonSchema::readDef(cJSON *childDefinitions, const std::string &defName)\r
146     {\r
147         DefinitionsPtr definition = std::make_shared<Definitions>(defName);\r
148 \r
149         cJSON *defType = cJSON_GetObjectItem(childDefinitions, "type");\r
150         if (defType)\r
151         {\r
152             std::string type = defType->valuestring;\r
153             definition->setType(type);\r
154             if (type == "array")\r
155             {\r
156                 PropertiesPtr property = std::make_shared<Properties>("array");\r
157                 readArray(childDefinitions, property);\r
158                 definition->addProperty("array" , property);\r
159             }\r
160         }\r
161         cJSON *defProperties = cJSON_GetObjectItem(childDefinitions, "properties");\r
162         if (defProperties)\r
163         {\r
164             cJSON *childProperties = defProperties->child;\r
165             while (childProperties)\r
166             {\r
167                 std::string attName = childProperties->string;\r
168                 definition->addProperty(attName, readProp(childProperties, attName));\r
169                 childProperties = childProperties->next;\r
170             }\r
171         }\r
172         cJSON *defRequiredValues = cJSON_GetObjectItem(childDefinitions, "required");\r
173         if (defRequiredValues)\r
174         {\r
175             int size = cJSON_GetArraySize(defRequiredValues);\r
176             int index = 0;\r
177             do\r
178             {\r
179                 definition->setRequiredValue(cJSON_GetArrayItem(defRequiredValues, index)->valuestring);\r
180             }\r
181             while ( ++index < size);\r
182         }\r
183         cJSON *defReference = cJSON_GetObjectItem(childDefinitions, "$ref");\r
184         if (defReference)\r
185         {\r
186             JsonParameters param;\r
187             readJsonRef(defReference, param);\r
188 \r
189             for (auto it : param.getProperties())\r
190             {\r
191                 definition->addProperty(it.first, it.second);\r
192             }\r
193             for ( auto it : param.getRequired())\r
194             {\r
195                 definition->setRequiredValue(it);\r
196             }\r
197             if (definition->getType().empty())\r
198                 definition->setType(param.getType());\r
199         }\r
200         cJSON *defAllOf = cJSON_GetObjectItem(childDefinitions, "allOf");\r
201         if (defAllOf)\r
202         {\r
203             JsonParameters param;\r
204             readAllOf(defAllOf, param);\r
205 \r
206             for (auto it : param.getProperties())\r
207             {\r
208                 definition->addProperty(it.first, it.second);\r
209             }\r
210             for ( auto it : param.getRequired())\r
211             {\r
212                 definition->setRequiredValue(it);\r
213             }\r
214             if (definition->getType().empty())\r
215                 definition->setType(param.getType());\r
216         }\r
217         return definition;\r
218     }\r
219 \r
220     PropertiesPtr JsonSchema::readProp(cJSON *childProperties, const std::string &attName )\r
221     {\r
222         PropertiesPtr property = std::make_shared<Properties>(attName);\r
223 \r
224         cJSON *propertyDescription = cJSON_GetObjectItem(childProperties, "description");\r
225         if (propertyDescription)\r
226         {\r
227             property->setDescription(propertyDescription->valuestring);\r
228         }\r
229         cJSON *propertyType = cJSON_GetObjectItem(childProperties, "type");\r
230         std::string attType;\r
231         if (propertyType)\r
232         {\r
233             if (propertyType->type == 4)\r
234             {\r
235                 attType = propertyType->valuestring;\r
236             }\r
237             else if (propertyType->type == 5)\r
238             {\r
239                 attType = cJSON_GetArrayItem(propertyType, 0)->valuestring;\r
240             }\r
241         }\r
242         if (!(attType == "array") && !(attType == "object"))\r
243         {\r
244             cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default");\r
245             if (defaultValue)\r
246             {\r
247                 readDefaultValue(defaultValue, property, attType);\r
248             }\r
249         }\r
250         readValues(childProperties, property, attType);\r
251         cJSON *allowedvalues = cJSON_GetObjectItem(childProperties, "enum");\r
252         if (allowedvalues)\r
253         {\r
254             readAllowedValues(allowedvalues, property, attType);\r
255         }\r
256         property->setTypeString(attType);\r
257         return property;\r
258     }\r
259 \r
260     void JsonSchema::readDefaultValue(cJSON *defaultValue,  PropertiesPtr &property,\r
261                                       const std::string &attType)\r
262     {\r
263         if (defaultValue->type == 4)\r
264         {\r
265             property->setValue((std::string)defaultValue->valuestring);\r
266         }\r
267         else if (defaultValue->type == 3)\r
268         {\r
269             if (attType == "number")\r
270                 property->setValue((double)defaultValue->valuedouble);\r
271             else\r
272                 property->setValue((int)defaultValue->valueint );\r
273         }\r
274         else if (defaultValue->type == 1)\r
275         {\r
276             property->setValue((bool)true);\r
277         }\r
278         else if (defaultValue->type == 0)\r
279         {\r
280             property->setValue((bool)false);\r
281         }\r
282     }\r
283 \r
284     void JsonSchema::readAllowedValues(cJSON *allowedvalues,  PropertiesPtr &property,\r
285                                        std::string &attType)\r
286     {\r
287         if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4)\r
288         {\r
289             int size = cJSON_GetArraySize(allowedvalues);\r
290             int idx = 0;\r
291             std::vector<std::string> allwdValues;\r
292             do\r
293             {\r
294                 allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring);\r
295             }\r
296             while ( ++idx < size);\r
297             property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
298             if (attType.empty())\r
299                 attType = "string";\r
300         }\r
301         else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3)\r
302         {\r
303             int size = cJSON_GetArraySize(allowedvalues);\r
304             int idx = 0;\r
305             if (attType == "number")\r
306             {\r
307                 std::vector<double> allwdValues;\r
308                 do\r
309                 {\r
310                     allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble);\r
311                 }\r
312                 while ( ++idx < size);\r
313                 property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
314             }\r
315             else\r
316             {\r
317                 std::vector<int> allwdValues;\r
318                 do\r
319                 {\r
320                     allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint);\r
321                 }\r
322                 while ( ++idx < size);\r
323                 property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
324                 if (attType.empty())\r
325                     attType = "integer";\r
326             }\r
327         }\r
328         else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1)\r
329                  || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0))\r
330         {\r
331             int size = cJSON_GetArraySize(allowedvalues);\r
332             int idx = 0;\r
333             std::vector<bool> allwdValues;\r
334             do\r
335             {\r
336                 if (cJSON_GetArrayItem(allowedvalues, idx)->type)\r
337                     allwdValues.push_back(true);\r
338                 else\r
339                     allwdValues.push_back(false);\r
340             }\r
341             while ( ++idx < size);\r
342             property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
343             if (attType.empty())\r
344                 attType = "boolean";\r
345         }\r
346     }\r
347 \r
348     void JsonSchema::readValues(cJSON *childProperties,  PropertiesPtr &property ,\r
349                                 const std::string &attType)\r
350     {\r
351         if (attType == "string")\r
352         {\r
353             readString(childProperties, property);\r
354         }\r
355         else if (attType == "integer")\r
356         {\r
357             readInteger(childProperties, property);\r
358         }\r
359         else if (attType == "number")\r
360         {\r
361             readDouble(childProperties, property);\r
362         }\r
363         else if (attType == "array")\r
364         {\r
365             readArray(childProperties, property);\r
366         }\r
367     }\r
368 \r
369     void JsonSchema::readString(cJSON *childProperties, PropertiesPtr &property)\r
370     {\r
371         cJSON *stringMax = cJSON_GetObjectItem(childProperties, "maxLength");\r
372         int min = INT_MIN, max = INT_MAX;\r
373         if (stringMax)\r
374         {\r
375             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
376             if (exclusiveMax)\r
377             {\r
378                 if (exclusiveMax->type == cJSON_True)\r
379                     max = --(stringMax->valueint);\r
380                 else\r
381                     max = stringMax->valueint;\r
382             }\r
383             else\r
384                 max = stringMax->valueint;\r
385         }\r
386         cJSON *stringMin = cJSON_GetObjectItem(childProperties, "minLength");\r
387         if (stringMin)\r
388         {\r
389             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
390             if (exclusiveMin)\r
391             {\r
392                 if (exclusiveMin->type == cJSON_True)\r
393                     min = ++(stringMin->valueint);\r
394                 else\r
395                     min = stringMin->valueint;\r
396             }\r
397             else\r
398                 min = stringMin->valueint;\r
399         }\r
400         if (min != INT_MIN || max != INT_MAX)\r
401             property->setValueProperty(std::make_shared<ValueProperty>(min, max, 0));\r
402 \r
403         cJSON *stringFormat = cJSON_GetObjectItem(childProperties, "format");\r
404         if (stringFormat)\r
405         {\r
406             property->setValueProperty(std::make_shared<ValueProperty>\r
407                                        (ValueProperty::Type::FORMAT, (stringFormat->valuestring)));\r
408         }\r
409 \r
410         cJSON *stringPattern = cJSON_GetObjectItem(childProperties, "pattern");\r
411         if (stringPattern)\r
412         {\r
413             property->setValueProperty(std::make_shared<ValueProperty>\r
414                                        (ValueProperty::Type::PATTERN, (stringPattern->valuestring)));\r
415         }\r
416     }\r
417 \r
418     void JsonSchema::readInteger(cJSON *childProperties,  PropertiesPtr &property)\r
419     {\r
420         cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum");\r
421         int min = INT_MIN, max = INT_MAX, multipleOf = INT_MAX;\r
422         if (Max)\r
423         {\r
424             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
425             if (exclusiveMax)\r
426             {\r
427                 if (exclusiveMax->type == cJSON_True)\r
428                     max = --(Max->valueint);\r
429                 else\r
430                     max = Max->valueint;\r
431             }\r
432             else\r
433                 max = Max->valueint;\r
434         }\r
435         cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum");\r
436         if (Min)\r
437         {\r
438             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
439             if (exclusiveMin)\r
440             {\r
441                 if (exclusiveMin->type == cJSON_True)\r
442                     min = ++(Min->valueint);\r
443                 else\r
444                     min = Min->valueint;\r
445             }\r
446             else\r
447                 min = Min->valueint;\r
448         }\r
449         cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf");\r
450         if (MultipleOff)\r
451         {\r
452             multipleOf = MultipleOff->valueint;\r
453         }\r
454         if (min != INT_MIN || max != INT_MAX)\r
455             property->setValueProperty(std::make_shared<ValueProperty>(min, max, multipleOf));\r
456 \r
457     }\r
458 \r
459     void JsonSchema::readDouble(cJSON *childProperties,  PropertiesPtr &property)\r
460     {\r
461         cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum");\r
462         double min = INT_MIN, max = INT_MAX;\r
463         int multipleOf = INT_MAX;\r
464         if (Max)\r
465         {\r
466             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
467             if (exclusiveMax)\r
468             {\r
469                 if (exclusiveMax->type == cJSON_True)\r
470                     max = --(Max->valuedouble);\r
471                 else\r
472                     max = Max->valuedouble;\r
473             }\r
474             else\r
475                 max = Max->valuedouble;\r
476         }\r
477         cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum");\r
478         if (Min)\r
479         {\r
480             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
481             if (exclusiveMin)\r
482             {\r
483                 if (exclusiveMin->type == cJSON_True)\r
484                     min = ++(Min->valuedouble);\r
485                 else\r
486                     min = Min->valuedouble;\r
487             }\r
488             else\r
489                 min = Min->valuedouble;\r
490         }\r
491 \r
492         cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf");\r
493         if (MultipleOff)\r
494         {\r
495             multipleOf = MultipleOff->valueint;\r
496         }\r
497         if (min != INT_MIN || max != INT_MAX)\r
498             property->setValueProperty(std::make_shared<ValueProperty>(min, max, multipleOf));\r
499 \r
500     }\r
501 \r
502     void JsonSchema::readArray(cJSON *childProperties,  PropertiesPtr &property)\r
503     {\r
504         cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items");\r
505         if (itemValues)\r
506         {\r
507             if (itemValues->type == 5)\r
508             {\r
509                 int item_size = cJSON_GetArraySize(itemValues);\r
510                 int item_index = 0;\r
511                 do\r
512                 {\r
513                     cJSON *item = cJSON_GetArrayItem(itemValues, item_index);\r
514                     readItems(item, property);\r
515                     break; \r
516                 }\r
517                 while ( ++item_index < item_size);\r
518             }\r
519             else\r
520             {\r
521                 readItems(itemValues, property);\r
522             }\r
523         }\r
524         cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems");\r
525         int min = INT_MIN, max = INT_MAX;\r
526         bool unique = cJSON_True, addItems = cJSON_True;\r
527         if (itemsMax)\r
528         {\r
529             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
530             if (exclusiveMax)\r
531             {\r
532                 if (exclusiveMax->type == cJSON_True)\r
533                     max = --(itemsMax->valueint);\r
534                 else\r
535                     max = itemsMax->valueint;\r
536             }\r
537             else\r
538                 max = itemsMax->valueint;\r
539         }\r
540         cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minItems");\r
541         if (itemsMin)\r
542         {\r
543             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
544             if (exclusiveMin)\r
545             {\r
546                 if (exclusiveMin->type == cJSON_True)\r
547                     min = ++(itemsMin->valueint);\r
548                 else\r
549                     min = itemsMin->valueint;\r
550             }\r
551             else\r
552                 min = itemsMin->valueint;\r
553         }\r
554         cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems");\r
555         if (uniqueItems)\r
556         {\r
557             unique = uniqueItems->type;\r
558         }\r
559         cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems");\r
560         if (additionalItems)\r
561         {\r
562             addItems = additionalItems->type;\r
563         }\r
564         property->setValueProperty(std::make_shared<ValueProperty>\r
565                                    (ValueProperty::Type::ARRAY, min, max, unique, addItems));\r
566     }\r
567 \r
568     void JsonSchema::readItems(cJSON *item, PropertiesPtr &property)\r
569     {\r
570         std::string type;\r
571         JsonParameters param;\r
572         cJSON *itemType = cJSON_GetObjectItem(item, "type");\r
573         if (itemType)\r
574         {\r
575             type = itemType->valuestring;\r
576         }\r
577 \r
578         cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf");\r
579         if (itemAllOf)\r
580         {\r
581             readAllOf(itemAllOf , param);\r
582         }\r
583         cJSON *itemReference = cJSON_GetObjectItem(item, "$ref");\r
584         if (itemReference)\r
585         {\r
586             readJsonRef(itemReference , param);\r
587         }\r
588 \r
589         if (type == "object")\r
590         {\r
591             cJSON *itemProperties = cJSON_GetObjectItem(item, "properties");\r
592             if (itemProperties)\r
593             {\r
594                 cJSON *childProperties = itemProperties->child;\r
595                 std::vector<Properties> propertyVector;\r
596                 while (childProperties)\r
597                 {\r
598                     std::string attName = childProperties->string;\r
599                     PropertiesPtr prop = std::make_shared<Properties>(attName);\r
600                     readProp(childProperties, attName);\r
601                     propertyVector.push_back(*prop);\r
602                     childProperties = childProperties->next;\r
603                 }\r
604                 property->setValue(propertyVector);\r
605             }\r
606 \r
607             cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required");\r
608             if (itemRequiredValues)\r
609             {\r
610                 int size = cJSON_GetArraySize(itemRequiredValues);\r
611                 int index = 0;\r
612                 do\r
613                 {\r
614                     property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
615                 }\r
616                 while ( ++index < size);\r
617             }\r
618         }\r
619 \r
620         else if (param.getType() == "object")\r
621         {\r
622             std::vector<Properties> propertyVector;\r
623             for (auto prop : param.getProperties())\r
624             {\r
625                 propertyVector.push_back(*(prop.second));\r
626             }\r
627             property->setValue(propertyVector);\r
628 \r
629             for (auto req : param.getRequired())\r
630             {\r
631                 property->setRequiredValue(req);\r
632             }\r
633         }\r
634         else\r
635         {\r
636             PropertiesPtr prop = std::make_shared<Properties>("property");\r
637 \r
638             cJSON *defaultValue = cJSON_GetObjectItem(item, "default");\r
639             if (defaultValue)\r
640             {\r
641                 readDefaultValue(defaultValue, prop, type);\r
642             }\r
643             cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum");\r
644             if (allowedvalues)\r
645             {\r
646                 readAllowedValues(allowedvalues, prop, type);\r
647             }\r
648             prop->setTypeString(type);\r
649             property->setValue(*prop);\r
650         }\r
651     }\r
652 \r
653     void JsonSchema::readFile(std::string &fileName ,  JsonParameters &param)\r
654     {\r
655         cJSON *json = m_includeResolver->readToJson(fileName);\r
656         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
657 \r
658         param.addProperties(Refparser->getProperties());\r
659         param.addRequired(Refparser->getRequiredValues());\r
660         param.setType(Refparser->getType());\r
661     }\r
662 \r
663     void JsonSchema::readFile(std::string &fileName , std::string &defName ,  JsonParameters &param)\r
664     {\r
665         cJSON *json = m_includeResolver->readToJson(fileName);\r
666         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
667 \r
668         DefinitionsPtr definition = Refparser->getDefinition(defName);\r
669         if (definition == nullptr)\r
670             throw JsonException("Definition Name Incorrect");\r
671 \r
672         param.addProperties(definition->getProperties());\r
673         param.addRequired(definition->getRequiredValues());\r
674         param.setType(definition->getType());\r
675     }\r
676 \r
677     void JsonSchema::readRef(std::string ref ,  JsonParameters &param)\r
678     {\r
679         std::string delimiter1 = "#";\r
680         std::string delimiter2 = "/";\r
681         std::string fileName;\r
682         if (! ref.empty())\r
683         {\r
684             std::size_t pos = ref.find(delimiter1);\r
685             if ( (pos = ref.find(delimiter1)) != std::string::npos)\r
686             {\r
687                 fileName = ref.substr(0, pos);\r
688                 ref.erase(0, pos);\r
689             }\r
690             ref.erase(0, delimiter1 .length());\r
691             std::string defName;\r
692 \r
693             if (! ref.empty())\r
694             {\r
695                 ref.erase(0, delimiter2 .length());\r
696                 std::string keyName;\r
697                 if ( (pos = ref.find(delimiter2)) != std::string::npos)\r
698                 {\r
699                     keyName = ref.substr(0, pos);\r
700                     ref.erase(0, pos + delimiter2.length());\r
701                     if (keyName == "definitions")\r
702                     {\r
703                         if ( (pos = ref.find(delimiter2)) != std::string::npos)\r
704                         {\r
705                             defName = ref.substr(0, pos);\r
706                         }\r
707                         else if (! ref.empty())\r
708                         {\r
709                             defName = ref;\r
710                         }\r
711                     }\r
712                 }\r
713             }\r
714             if (!fileName.empty())\r
715             {\r
716                 if (!(defName.empty()))\r
717                 {\r
718                     readFile(fileName, defName, param);\r
719                 }\r
720                 else\r
721                 {\r
722                     throw JsonException("Definition Name Empty");\r
723                 }\r
724             }\r
725             else\r
726             {\r
727                 if (!(defName.empty()))\r
728                 {\r
729                     if (getDefinition(defName) == nullptr)\r
730                         throw JsonException("Definition Name Incorrect");\r
731                     param.addProperties(getDefinition(defName)->getProperties());\r
732                     param.addRequired(getDefinition(defName)->getRequiredValues());\r
733                     param.setType(getDefinition(defName)->getType());\r
734                 }\r
735                 else\r
736                 {\r
737                     throw JsonException("Definition Name Empty");\r
738                 }\r
739             }\r
740         }\r
741     }\r
742 \r
743     void JsonSchema::readJsonRef(cJSON *jsonReference , JsonParameters &param)\r
744     {\r
745         std::string ref = jsonReference->valuestring;\r
746 \r
747         std::string web = "http://";\r
748         std::string delimiter = "#";\r
749         std::size_t pos = ref.find(web);\r
750 \r
751         if (pos == std::string::npos)   // If Web Link Is GIVEN TO READ\r
752         {\r
753             pos = ref.find(delimiter);\r
754             if ( pos ==  (ref.length() - 1) )\r
755             {\r
756                 std::string fileName = ref.substr(0, pos);\r
757                 readFile(fileName, param);\r
758             }\r
759             else\r
760             {\r
761                 readRef(ref, param);\r
762             }\r
763         }\r
764     }\r
765 \r
766     void JsonSchema::readAllOf(cJSON *allofValues ,  JsonParameters &allParams)\r
767     {\r
768         int size = cJSON_GetArraySize(allofValues);\r
769         int index = 0;\r
770         do\r
771         {\r
772             JsonParameters param;\r
773 \r
774             cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index);\r
775             cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref");\r
776             if (jsonReference)\r
777             {\r
778                 readJsonRef(jsonReference, param);\r
779                 allParams.addProperties(param.getProperties());\r
780                 allParams.addRequired(param.getRequired());\r
781                 allParams.setType(param.getType());\r
782             }\r
783             cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required");\r
784             if (jsonRequiredValues)\r
785             {\r
786                 int len = cJSON_GetArraySize(jsonRequiredValues);\r
787                 int idx = 0;\r
788                 do\r
789                 {\r
790                     allParams.addRequired(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring);\r
791                 }\r
792                 while ( ++idx < len);\r
793             }\r
794         }\r
795         while ( ++index < size);\r
796     }\r
797 }\r
798 \r