3 * Copyright (C) 2005-2010 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 * http://www.gnu.org/copyleft/gpl.html
26 #include "JSONUtils.h"
32 \brief Structure for a parameter of a
35 Represents a parameter of a defined
36 json rpc method and is used to verify
37 and extract the value of the parameter
40 typedef struct JSONSchemaTypeDefinition
42 JSONSchemaTypeDefinition()
43 : type(AnyValue), minimum(std::numeric_limits<double>::min()), maximum(std::numeric_limits<double>::max()),
44 exclusiveMinimum(false), exclusiveMaximum(false), divisibleBy(0),
45 minLength(-1), maxLength(-1),
46 minItems(0), maxItems(0), uniqueItems(false),
47 hasAdditionalProperties(false), additionalProperties(NULL)
51 \brief Name of the parameter (for
57 \brief Id of the type (for
59 Renamed from "id" because of possible
60 issues with Objective-C.
65 \brief Array of reference types
66 which are extended by this type.
68 std::vector<JSONSchemaTypeDefinition> extends;
71 \brief Description of the parameter
73 std::string description;
76 \brief JSON schema type of the parameter's value
81 \brief JSON schema type definitions in case
84 std::vector<JSONSchemaTypeDefinition> unionTypes;
87 \brief Whether or not the parameter is
93 \brief Default value of the parameter
94 (only needed when it is optional)
96 CVariant defaultValue;
99 \brief Minimum value for Integer
105 \brief Maximum value for Integer or Number types
110 \brief Whether to exclude the defined Minimum
111 value from the valid range or not
113 bool exclusiveMinimum;
116 \brief Whether to exclude the defined Maximum
117 value from the valid range or not
119 bool exclusiveMaximum;
122 \brief Integer by which the value (of type
123 Integer) must be divisible without rest
125 unsigned int divisibleBy;
128 \brief Minimum length for String types
133 \brief Maximum length for String types
138 \brief (Optional) List of allowed values
141 std::vector<CVariant> enums;
144 \brief List of possible values in an array
146 std::vector<JSONSchemaTypeDefinition> items;
149 \brief Minimum amount of items in the array
151 unsigned int minItems;
154 \brief Maximum amount of items in the array
156 unsigned int maxItems;
159 \brief Whether every value in the array
160 must be unique or not
165 \brief List of json schema definitions for
166 additional items in an array with tuple
167 typing (defined schemas in "items")
169 std::vector<JSONSchemaTypeDefinition> additionalItems;
172 \brief Maps a properties name to its
173 json schema type definition
175 class CJsonSchemaPropertiesMap
178 CJsonSchemaPropertiesMap();
180 void add(JSONSchemaTypeDefinition &property);
182 typedef std::map<std::string, JSONSchemaTypeDefinition>::const_iterator JSONSchemaPropertiesIterator;
183 JSONSchemaPropertiesIterator begin() const;
184 JSONSchemaPropertiesIterator find(const std::string& key) const;
185 JSONSchemaPropertiesIterator end() const;
186 unsigned int size() const;
188 std::map<std::string, JSONSchemaTypeDefinition> m_propertiesmap;
192 \brief List of properties of the parameter (only needed when the
193 parameter is an object)
195 CJsonSchemaPropertiesMap properties;
198 \brief Whether the type can have additional properties
201 bool hasAdditionalProperties;
204 \brief Type definition for additional properties
206 JSONSchemaTypeDefinition* additionalProperties;
207 } JSONSchemaTypeDefinition;
211 \brief Structure for a published json
214 Represents a published json rpc method
215 and is used to verify an incoming json
216 rpc request against a defined method.
221 \brief Name of the represented method
225 \brief Pointer tot he implementation
226 of the represented method
230 \brief Definition of the type of
233 TransportLayerCapability transportneed;
235 \brief Definition of the permissions needed
236 to execute the method
238 OperationPermission permission;
240 \brief Description of the method
242 std::string description;
244 \brief List of accepted parameters
246 std::vector<JSONSchemaTypeDefinition> parameters;
248 \brief Definition of the return value
250 JSONSchemaTypeDefinition returns;
255 \brief Structure mapping a json rpc method
256 definition to an actual method implementation.
261 \brief Name of the json rpc method.
265 \brief Pointer to the actual
266 implementation of the json rpc
274 \brief Helper class for json schema service descriptor based
275 service descriptions for the json rpc API
277 Provides static functions to parse a complete json schema
278 service descriptor of a published service containing json rpc
279 methods, print the json schema service descriptor representation
280 into a string (mainly for output purposes) and evaluate and verify
281 parameters provided in a call to one of the publish json rpc methods
282 against a parameter definition parsed from a json schema service
285 class CJSONServiceDescription : public CJSONUtils
289 \brief Parses the given json schema description and evaluates
290 and stores the defined type
291 \param jsonType json schema description to parse
292 \return True if the json schema description has been parsed sucessfully otherwise false
294 static bool AddType(std::string jsonType);
297 \brief Parses the given json schema description and evaluates
298 and stores the defined method
299 \param jsonMethod json schema description to parse
300 \param method pointer to the implementation
301 \return True if the json schema description has been parsed sucessfully otherwise false
303 static bool AddMethod(std::string jsonMethod, MethodCall method);
306 \brief Parses the given json schema description and evaluates
307 and stores the defined builtin method
308 \param jsonMethod json schema description to parse
309 \return True if the json schema description has been parsed sucessfully otherwise false
311 static bool AddBuiltinMethod(std::string jsonMethod);
314 \brief Parses the given json schema description and evaluates
315 and stores the defined notification
316 \param jsonNotification json schema description to parse
317 \return True if the json schema description has been parsed sucessfully otherwise false
319 static bool AddNotification(std::string jsonNotification);
322 \brief Gets the version of the json
324 \return Version of the json schema description
326 static int GetVersion();
329 \brief Prints the json schema description into the given result object
330 \param result Object into which the json schema description is printed
331 \param transport Transport layer capabilities
332 \param client Client requesting a print
333 \param printDescriptions Whether to print descriptions or not
334 \param printMetadata Whether to print XBMC specific data or not
335 \param filterByTransport Whether to filter by transport or not
337 static JSON_STATUS Print(CVariant &result, ITransportLayer *transport, IClient *client, bool printDescriptions = true, bool printMetadata = false, bool filterByTransport = true, std::string filterByName = "", std::string filterByType = "", bool printReferences = true);
340 \brief Checks the given parameters from the request against the
341 json schema description for the given method
342 \param method Called method
343 \param requestParameters Parameters from the request
344 \param client Client who sent the request
345 \param notification Whether the request was sent as a notification or not
346 \param methodCall Object which will contain the actual C/C++ method to be called
347 \param outputParameters Cleaned up parameter list
348 \return OK if the validation of the request succeeded otherwise an appropriate error code
350 Checks if the given method is a valid json rpc method, if the client has the permission
351 to call this method, if the method can be called as a notification or not, assigns the
352 actual C/C++ implementation of the method to the "methodCall" parameter and checks the
353 given parameters from the request against the json schema description for the given method.
355 static JSON_STATUS CheckCall(const char* const method, const CVariant &requestParameters, ITransportLayer *transport, IClient *client, bool notification, MethodCall &methodCall, CVariant &outputParameters);
358 static bool prepareDescription(std::string &description, CVariant &descriptionObject, std::string &name);
359 static bool addMethod(std::string &jsonMethod, MethodCall method);
360 static void printType(const JSONSchemaTypeDefinition &type, bool isParameter, bool isGlobal, bool printDefault, bool printDescriptions, CVariant &output);
361 static JSON_STATUS checkParameter(const CVariant &requestParameters, const JSONSchemaTypeDefinition &type, unsigned int position, CVariant &outputParameters, unsigned int &handled, CVariant &errorData);
362 static JSON_STATUS checkType(const CVariant &value, const JSONSchemaTypeDefinition &type, CVariant &outputValue, CVariant &errorData);
363 static void parseHeader(const CVariant &descriptionObject);
364 static bool parseMethod(const CVariant &value, JsonRpcMethod &method);
365 static bool parseParameter(CVariant &value, JSONSchemaTypeDefinition ¶meter);
366 static bool parseTypeDefinition(const CVariant &value, JSONSchemaTypeDefinition &type, bool isParameter);
367 static void parseReturn(const CVariant &value, JSONSchemaTypeDefinition &returns);
368 static JSONSchemaType parseJSONSchemaType(const CVariant &value, std::vector<JSONSchemaTypeDefinition>& typeDefinitions);
369 static void addReferenceTypeDefinition(JSONSchemaTypeDefinition &typeDefinition);
371 static void getReferencedTypes(const JSONSchemaTypeDefinition &type, std::vector<std::string> &referencedTypes);
373 class CJsonRpcMethodMap
378 void add(const JsonRpcMethod &method);
380 typedef std::map<std::string, JsonRpcMethod>::const_iterator JsonRpcMethodIterator;
381 JsonRpcMethodIterator begin() const;
382 JsonRpcMethodIterator find(const std::string& key) const;
383 JsonRpcMethodIterator end() const;
385 std::map<std::string, JsonRpcMethod> m_actionmap;
388 static CJsonRpcMethodMap m_actionMap;
389 static std::map<std::string, JSONSchemaTypeDefinition> m_types;
390 static std::map<std::string, CVariant> m_notifications;
391 static JsonRpcMethodMap m_methodMaps[];