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