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