1 /******************************************************************
\r
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 ******************************************************************/
\r
22 * @file JsonSchema.h
\r
24 * @brief This file provides data Model for Json Schema file.
\r
27 #ifndef JSON_SCHEMA_H_
\r
28 #define JSON_SCHEMA_H_
\r
33 #include "Properties.h"
\r
35 #include "Definitions.h"
\r
37 #include "Helpers.h"
\r
38 #include "AllowedValues.h"
\r
41 #include "IncludeResolver.h"
\r
47 * @brief This class provides data Model for Json Schema file.
\r
53 * Constructor of JsonSchema.
\r
55 JsonSchema() : m_cjson(NULL), m_includeResolver(NULL) {}
\r
58 * Constructor of JsonSchema.
\r
60 * @param includeResolver - Reference to IncludeResolver for reading external files
\r
62 JsonSchema(const IncludeResolverPtr &includeResolver) : m_cjson(NULL),
\r
63 m_includeResolver(includeResolver) {}
\r
66 * Constructor of JsonSchema.
\r
68 * @param cjson - pointer to cjson
\r
69 * @param includeResolver - Reference to IncludeResolver for reading external files
\r
71 JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_cjson(cjson),
\r
72 m_includeResolver(includeResolver) { readJson(); }
\r
76 * This method is for setting cJson pointer to JsonSchema.
\r
78 * @param cjson -pointer to cJson
\r
80 void setcJson(cJSON *cjson) {m_cjson = cjson; readJson(); }
\r
83 * This method is for getting size of Properties from JsonSchema.
\r
85 * @return size of Properties
\r
89 return m_properties.size();
\r
93 * This method is for getting Properties from JsonSchema.
\r
95 * @param name -name of property as string
\r
97 * @return pointer to Properties
\r
99 inline PropertiesPtr getProperty(const std::string &name)
\r
101 if (m_properties.end() != m_properties.find(name))
\r
103 return m_properties[name];
\r
109 * This method is for getting Properties from JsonSchema.
\r
111 * @return map of Properties name and pointer to Properties
\r
113 inline std::map<std::string, PropertiesPtr > const &getProperties()
\r
115 return m_properties;
\r
119 * This method is for getting Definitions from JsonSchema.
\r
121 * @return map of Definitions name and pointer to Definitions
\r
123 inline std::map<std::string, DefinitionsPtr > const &getDefinitions()
\r
125 return m_definition;
\r
129 * This method is for setting Properties to JsonSchema.
\r
131 * @param name -name of property as string
\r
132 * @param property -pointer to Properties
\r
134 void addProperty(const std::string &name, const PropertiesPtr &property)
\r
136 if (m_properties.end() == m_properties.find(name))
\r
138 m_properties[name] = property;
\r
143 * This method is for setting RequiredValue to JsonSchema.
\r
145 * @param reqValue -name of RequiredValue as string
\r
147 void setRequiredValue(const std::string &reqValue)
\r
149 if (m_required.end() == std::find(m_required.begin(), m_required.end(), reqValue))
\r
151 m_required.push_back(reqValue);
\r
156 * This method is for getting RequiredValues from JsonSchema.
\r
158 * @return vector of RequiredValues as string
\r
160 std::vector<std::string> const &getRequiredValues()
\r
166 * This method is for setting Definitions to JsonSchema.
\r
168 * @param defName -name of Definitions as string
\r
169 * @param definition -pointer to Definitions
\r
171 void addDefinition(const std::string &defName, const DefinitionsPtr &definition)
\r
173 if (m_definition.end() == m_definition.find(defName))
\r
175 m_definition[defName] = definition;
\r
180 * This method is for getting Definitions from JsonSchema.
\r
182 * @param defName -Definition name as string
\r
184 * @return pointer to Definitions
\r
186 DefinitionsPtr getDefinition(const std::string &defName)
\r
188 if (m_definition.end() != m_definition.find(defName))
\r
190 return m_definition[defName];
\r
196 * This method is for getting Type from JsonSchema.
\r
198 * @return JsonSchema Type as string
\r
200 std::string getType()
\r
206 * This method is for getting Id from JsonSchema.
\r
208 * @return JsonSchema Id as string
\r
210 std::string getId()
\r
216 * This method is for getting Schema from JsonSchema.
\r
218 * @return Schema as string
\r
220 std::string getSchema()
\r
226 * This method is for getting Description from JsonSchema.
\r
228 * @return JsonSchema Description as string
\r
230 std::string getDescription()
\r
232 return m_description;
\r
236 * This method is for getting Title from JsonSchema.
\r
238 * @return JsonSchema Title as string
\r
240 std::string getTitle()
\r
246 * This method is for getting AdditionalProperties from JsonSchema.
\r
248 * @return AdditionalProperties as bool
\r
250 bool getAdditionalProperties()
\r
252 return m_additionalProperties;
\r
256 * This method is for setting Items to JsonSchema.
\r
258 * @param item -pointer to Items
\r
260 void setItem(const ItemsPtr &item)
\r
262 m_items.push_back(item);
\r
266 * This method is for getting Items from JsonSchema.
\r
268 * @return vector of Items
\r
270 std::vector<ItemsPtr> const &getItems()
\r
277 DefinitionsPtr readDef(cJSON *childDefinitions, const std::string &defName);
\r
278 PropertiesPtr readProp(cJSON *childProperties, const std::string &attName );
\r
279 void readValues( cJSON *childProperties, PropertiesPtr property ,
\r
280 const std::string &attType);
\r
281 void readString( cJSON *childProperties, PropertiesPtr property);
\r
282 void readArray( cJSON *childProperties, PropertiesPtr property);
\r
283 void readInteger( cJSON *childProperties, PropertiesPtr property);
\r
284 void readDouble( cJSON *childProperties, PropertiesPtr property);
\r
285 DefinitionsPtr readRef(std::string m_ref);
\r
288 void readJsonRef(cJSON *jsonReference);
\r
289 void readDefRef(cJSON *defReference, DefinitionsPtr definition);
\r
290 void readAllOf(cJSON *allofValues);
\r
291 void readDefAllOf(cJSON *allofValues, DefinitionsPtr definition);
\r
292 ItemsPtr readItems(cJSON *item);
\r
293 void readItemRef(cJSON *itemReference, ItemsPtr item);
\r
294 void readItemAllOf(cJSON *allofValues, ItemsPtr item);
\r
297 std::map<std::string, PropertiesPtr > m_properties;
\r
298 std::map<std::string, DefinitionsPtr > m_definition;
\r
300 std::string m_schema;
\r
301 std::string m_title;
\r
302 std::string m_description;
\r
303 bool m_additionalProperties;
\r
304 std::string m_type;
\r
306 std::vector<std::string> m_required;
\r
307 std::vector<ItemsPtr > m_items;
\r
308 IncludeResolverPtr m_includeResolver;
\r
311 /** JsonSchemaPtr - shared Ptr to JsonSchema.*/
\r
312 typedef std::shared_ptr<JsonSchema> JsonSchemaPtr;
\r