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
34 #include "Definitions.h"
\r
38 #include "IncludeResolver.h"
\r
44 * @brief This class provides data Model for Json Schema file.
\r
50 * Constructor of JsonSchema.
\r
52 JsonSchema() : m_additionalProperties(cJSON_True), m_cjson(NULL), m_includeResolver(NULL) {}
\r
55 * Constructor of JsonSchema.
\r
57 * @param includeResolver - Reference to IncludeResolver for reading external files
\r
59 JsonSchema(const IncludeResolverPtr &includeResolver) : m_additionalProperties(cJSON_True),
\r
61 m_includeResolver(includeResolver) {}
\r
64 * Constructor of JsonSchema.
\r
66 * @param cjson - pointer to cjson
\r
67 * @param includeResolver - Reference to IncludeResolver for reading external files
\r
69 JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_additionalProperties(
\r
70 cJSON_True), m_cjson(cjson),
\r
71 m_includeResolver(includeResolver) { readJson(); }
\r
75 * This method is for setting cJson pointer to JsonSchema.
\r
77 * @param cjson -pointer to cJson
\r
79 void setcJson(cJSON *cjson) {m_cjson = cjson; readJson(); }
\r
82 * This method is for getting size of Properties from JsonSchema.
\r
84 * @return size of Properties
\r
88 return m_properties.size();
\r
92 * This method is for getting Properties from JsonSchema.
\r
94 * @param name -name of property as string
\r
96 * @return pointer to Properties
\r
98 PropertiesPtr getProperty(const std::string &name)
\r
100 if (m_properties.end() != m_properties.find(name))
\r
102 return m_properties[name];
\r
108 * This method is for getting Properties from JsonSchema.
\r
110 * @return map of Properties name and pointer to Properties
\r
112 std::map<std::string, PropertiesPtr > const &getProperties()
\r
114 return m_properties;
\r
118 * This method is for getting Definitions from JsonSchema.
\r
120 * @return map of Definitions name and pointer to Definitions
\r
122 std::map<std::string, DefinitionsPtr > const &getDefinitions()
\r
124 return m_definition;
\r
128 * This method is for setting Properties to JsonSchema.
\r
130 * @param name -name of property as string
\r
131 * @param property -pointer to Properties
\r
133 void addProperty(const std::string &name, const PropertiesPtr &property)
\r
135 if (m_properties.end() == m_properties.find(name))
\r
137 m_properties[name] = property;
\r
142 * This method is for setting RequiredValue to JsonSchema.
\r
144 * @param reqValue -name of RequiredValue as string
\r
146 void setRequiredValue(const std::string &reqValue)
\r
148 if (m_required.end() == std::find(m_required.begin(), m_required.end(), reqValue))
\r
150 m_required.push_back(reqValue);
\r
155 * This method is for getting RequiredValues from JsonSchema.
\r
157 * @return vector of RequiredValues as string
\r
159 std::vector<std::string> const &getRequiredValues()
\r
165 * This method is for setting Definitions to JsonSchema.
\r
167 * @param defName -name of Definitions as string
\r
168 * @param definition -pointer to Definitions
\r
170 void addDefinition(const std::string &defName, const DefinitionsPtr &definition)
\r
172 if (m_definition.end() == m_definition.find(defName))
\r
174 m_definition[defName] = definition;
\r
179 * This method is for getting Definitions from JsonSchema.
\r
181 * @param defName -Definition name as string
\r
183 * @return pointer to Definitions
\r
185 DefinitionsPtr getDefinition(const std::string &defName)
\r
187 if (m_definition.end() != m_definition.find(defName))
\r
189 return m_definition[defName];
\r
195 * This method is for getting Type from JsonSchema.
\r
197 * @return JsonSchema Type as string
\r
199 std::string getType()
\r
205 * This method is for getting Id from JsonSchema.
\r
207 * @return JsonSchema Id as string
\r
209 std::string getId()
\r
215 * This method is for getting Schema from JsonSchema.
\r
217 * @return Schema as string
\r
219 std::string getSchema()
\r
225 * This method is for getting Description from JsonSchema.
\r
227 * @return JsonSchema Description as string
\r
229 std::string getDescription()
\r
231 return m_description;
\r
235 * This method is for getting Title from JsonSchema.
\r
237 * @return JsonSchema Title as string
\r
239 std::string getTitle()
\r
245 * This method is for getting AdditionalProperties from JsonSchema.
\r
247 * @return AdditionalProperties as bool
\r
249 bool getAdditionalProperties()
\r
251 return m_additionalProperties;
\r
255 class JsonParameters
\r
258 std::map<std::string, PropertiesPtr > getProperties() const { return m_properties; }
\r
259 void addProperties(const std::string &name, const PropertiesPtr &prop) { m_properties[name] = prop; }
\r
260 void addProperties(const std::map<std::string, PropertiesPtr > &properties)
\r
262 for (auto prop : properties)
\r
263 m_properties[prop.first] = prop.second;
\r
265 std::vector<std::string> getRequired() const { return m_required; }
\r
266 void addRequired(const std::string &req) { m_required.push_back(req); }
\r
267 void addRequired(const std::vector<std::string> &required)
\r
269 for (auto req : required)
\r
270 m_required.push_back(req);
\r
272 std::string getType() const { return m_type; }
\r
273 void setType(const std::string &type)
\r
275 if (m_type.empty())
\r
280 std::map<std::string, PropertiesPtr > m_properties;
\r
281 std::vector<std::string> m_required;
\r
282 std::string m_type;
\r
286 DefinitionsPtr readDef(cJSON *childDefinitions, const std::string &defName);
\r
287 PropertiesPtr readProp(cJSON *childProperties, const std::string &attName );
\r
288 void readDefaultValue(cJSON *defaultValue, PropertiesPtr &property, const std::string &attType);
\r
289 void readAllowedValues(cJSON *allowedvalues, PropertiesPtr &property, std::string &attType);
\r
290 void readValues( cJSON *childProperties, PropertiesPtr &property ,
\r
291 const std::string &attType);
\r
292 void readString( cJSON *childProperties, PropertiesPtr &property);
\r
293 void readInteger( cJSON *childProperties, PropertiesPtr &property);
\r
294 void readDouble( cJSON *childProperties, PropertiesPtr &property);
\r
295 void readArray( cJSON *childProperties, PropertiesPtr &property);
\r
296 void readItems(cJSON *item, PropertiesPtr &property);
\r
297 void readObject(cJSON *childProperties, PropertiesPtr &property);
\r
299 void readFile(std::string &fileName , JsonParameters ¶m);
\r
300 void readFile(std::string &fileName , std::string &defName , JsonParameters ¶m);
\r
301 void readRef(std::string ref , JsonParameters ¶m);
\r
302 void readJsonRef(cJSON *jsonReference , JsonParameters ¶m);
\r
303 void readAllOf(cJSON *allofValues , JsonParameters &allParams);
\r
306 std::map<std::string, PropertiesPtr > m_properties;
\r
307 std::map<std::string, DefinitionsPtr > m_definition;
\r
309 std::string m_schema;
\r
310 std::string m_title;
\r
311 std::string m_description;
\r
312 bool m_additionalProperties;
\r
313 std::string m_type;
\r
315 std::vector<std::string> m_required;
\r
316 PropertiesPtr m_property;
\r
317 IncludeResolverPtr m_includeResolver;
\r
321 /** JsonSchemaPtr - shared Ptr to JsonSchema.*/
\r
322 typedef std::shared_ptr<JsonSchema> JsonSchemaPtr;
\r