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
35 #if defined (__TIZENRT__)
\r
36 #include <apps/netutils/cJSON.h>
\r
42 #include "IncludeResolver.h"
\r
48 * @brief This class provides data Model for Json Schema file.
\r
54 * Constructor of JsonSchema.
\r
56 JsonSchema() : m_additionalProperties(cJSON_True), m_cjson(NULL), m_includeResolver(NULL) {}
\r
59 * Constructor of JsonSchema.
\r
61 * @param includeResolver - Reference to IncludeResolver for reading external files
\r
63 JsonSchema(const IncludeResolverPtr &includeResolver) : m_additionalProperties(cJSON_True),
\r
65 m_includeResolver(includeResolver) {}
\r
68 * Constructor of JsonSchema.
\r
70 * @param cjson - pointer to cjson
\r
71 * @param includeResolver - Reference to IncludeResolver for reading external files
\r
73 JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_additionalProperties(
\r
74 cJSON_True), m_cjson(cjson),
\r
75 m_includeResolver(includeResolver) { readJson(); }
\r
79 * This method is for setting cJson pointer to JsonSchema.
\r
81 * @param cjson -pointer to cJson
\r
83 void setcJson(cJSON *cjson) {m_cjson = cjson; readJson(); }
\r
86 * This method is for getting size of Properties from JsonSchema.
\r
88 * @return size of Properties
\r
92 return m_properties.size();
\r
96 * This method is for getting Properties from JsonSchema.
\r
98 * @param name -name of property as string
\r
100 * @return pointer to Properties
\r
102 PropertiesPtr getProperty(const std::string &name)
\r
104 if (m_properties.end() != m_properties.find(name))
\r
106 return m_properties[name];
\r
112 * This method is for getting Properties from JsonSchema.
\r
114 * @return map of Properties name and pointer to Properties
\r
116 std::map<std::string, PropertiesPtr > const &getProperties()
\r
118 return m_properties;
\r
122 * This method is for getting Definitions from JsonSchema.
\r
124 * @return map of Definitions name and pointer to Definitions
\r
126 std::map<std::string, DefinitionsPtr > const &getDefinitions()
\r
128 return m_definition;
\r
132 * This method is for setting Properties to JsonSchema.
\r
134 * @param name -name of property as string
\r
135 * @param property -pointer to Properties
\r
137 void addProperty(const std::string &name, const PropertiesPtr &property)
\r
139 if (m_properties.end() == m_properties.find(name))
\r
141 m_properties[name] = property;
\r
146 * This method is for setting RequiredValue to JsonSchema.
\r
148 * @param reqValue -name of RequiredValue as string
\r
150 void setRequiredValue(const std::string &reqValue)
\r
152 if (m_required.end() == std::find(m_required.begin(), m_required.end(), reqValue))
\r
154 m_required.push_back(reqValue);
\r
159 * This method is for getting RequiredValues from JsonSchema.
\r
161 * @return vector of RequiredValues as string
\r
163 std::vector<std::string> const &getRequiredValues()
\r
169 * This method is for setting Definitions to JsonSchema.
\r
171 * @param defName -name of Definitions as string
\r
172 * @param definition -pointer to Definitions
\r
174 void addDefinition(const std::string &defName, const DefinitionsPtr &definition)
\r
176 if (m_definition.end() == m_definition.find(defName))
\r
178 m_definition[defName] = definition;
\r
183 * This method is for getting Definitions from JsonSchema.
\r
185 * @param defName -Definition name as string
\r
187 * @return pointer to Definitions
\r
189 DefinitionsPtr getDefinition(const std::string &defName)
\r
191 if (m_definition.end() != m_definition.find(defName))
\r
193 return m_definition[defName];
\r
199 * This method is for getting Type from JsonSchema.
\r
201 * @return JsonSchema Type as string
\r
203 std::string getType()
\r
209 * This method is for getting Id from JsonSchema.
\r
211 * @return JsonSchema Id as string
\r
213 std::string getId()
\r
219 * This method is for getting Schema from JsonSchema.
\r
221 * @return Schema as string
\r
223 std::string getSchema()
\r
229 * This method is for getting Description from JsonSchema.
\r
231 * @return JsonSchema Description as string
\r
233 std::string getDescription()
\r
235 return m_description;
\r
239 * This method is for getting Title from JsonSchema.
\r
241 * @return JsonSchema Title as string
\r
243 std::string getTitle()
\r
249 * This method is for getting AdditionalProperties from JsonSchema.
\r
251 * @return AdditionalProperties as bool
\r
253 bool getAdditionalProperties()
\r
255 return m_additionalProperties;
\r
259 class JsonParameters
\r
262 std::map<std::string, PropertiesPtr > getProperties() const { return m_properties; }
\r
263 void addProperties(const std::string &name, const PropertiesPtr &prop) { m_properties[name] = prop; }
\r
264 void addProperties(const std::map<std::string, PropertiesPtr > &properties)
\r
266 for (auto prop : properties)
\r
267 m_properties[prop.first] = prop.second;
\r
269 std::vector<std::string> getRequired() const { return m_required; }
\r
270 void addRequired(const std::string &req) { m_required.push_back(req); }
\r
271 void addRequired(const std::vector<std::string> &required)
\r
273 for (auto req : required)
\r
274 m_required.push_back(req);
\r
276 std::string getType() const { return m_type; }
\r
277 void setType(const std::string &type)
\r
279 if (m_type.empty())
\r
284 std::map<std::string, PropertiesPtr > m_properties;
\r
285 std::vector<std::string> m_required;
\r
286 std::string m_type;
\r
290 DefinitionsPtr readDef(cJSON *childDefinitions, const std::string &defName);
\r
291 PropertiesPtr readProp(cJSON *childProperties, const std::string &attName );
\r
292 void readDefaultValue(cJSON *defaultValue, PropertiesPtr &property, const std::string &attType);
\r
293 void readAllowedValues(cJSON *allowedvalues, PropertiesPtr &property, std::string &attType);
\r
294 void readValues( cJSON *childProperties, PropertiesPtr &property ,
\r
295 const std::string &attType);
\r
296 void readString( cJSON *childProperties, PropertiesPtr &property);
\r
297 void readInteger( cJSON *childProperties, PropertiesPtr &property);
\r
298 void readDouble( cJSON *childProperties, PropertiesPtr &property);
\r
299 void readArray( cJSON *childProperties, PropertiesPtr &property);
\r
300 void readItems(cJSON *item, PropertiesPtr &property);
\r
301 void readObject(cJSON *childProperties, PropertiesPtr &property);
\r
303 void readFile(std::string &fileName , JsonParameters ¶m);
\r
304 void readFile(std::string &fileName , std::string &defName , JsonParameters ¶m);
\r
305 void readRef(std::string ref , JsonParameters ¶m);
\r
306 void readJsonRef(cJSON *jsonReference , JsonParameters ¶m);
\r
307 void readAllOf(cJSON *allofValues , JsonParameters &allParams);
\r
310 std::map<std::string, PropertiesPtr > m_properties;
\r
311 std::map<std::string, DefinitionsPtr > m_definition;
\r
313 std::string m_schema;
\r
314 std::string m_title;
\r
315 std::string m_description;
\r
316 bool m_additionalProperties;
\r
317 std::string m_type;
\r
319 std::vector<std::string> m_required;
\r
320 PropertiesPtr m_property;
\r
321 IncludeResolverPtr m_includeResolver;
\r
325 /** JsonSchemaPtr - shared Ptr to JsonSchema.*/
\r
326 typedef std::shared_ptr<JsonSchema> JsonSchemaPtr;
\r