kodi
JSONServiceDescription.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "JSONUtils.h"
12 #include "utils/Variant.h"
13 
14 #include <limits>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 namespace JSONRPC
20 {
21  class JSONSchemaTypeDefinition;
22  typedef std::shared_ptr<JSONSchemaTypeDefinition> JSONSchemaTypeDefinitionPtr;
23 
35  {
36  public:
38 
39  bool Parse(const CVariant &value, bool isParameter = false);
40  JSONRPC_STATUS Check(const CVariant& value, CVariant& outputValue, CVariant& errorData) const;
41  void Print(bool isParameter, bool isGlobal, bool printDefault, bool printDescriptions, CVariant &output) const;
42  void ResolveReference();
43 
44  std::string missingReference;
45 
50  std::string name;
51 
58  std::string ID;
59 
63  JSONSchemaTypeDefinitionPtr referencedType;
64 
69  bool referencedTypeSet = false;
70 
75  std::vector<JSONSchemaTypeDefinitionPtr> extends;
76 
80  std::string description;
81 
85  JSONSchemaType type = AnyValue;
86 
91  std::vector<JSONSchemaTypeDefinitionPtr> unionTypes;
92 
97  bool optional = true;
98 
104 
109  double minimum;
110 
114  double maximum;
115 
120  bool exclusiveMinimum = false;
121 
126  bool exclusiveMaximum = false;
127 
132  unsigned int divisibleBy = 0;
133 
137  int minLength = -1;
138 
142  int maxLength = -1;
143 
148  std::vector<CVariant> enums;
149 
153  std::vector<JSONSchemaTypeDefinitionPtr> items;
154 
158  unsigned int minItems = 0;
159 
163  unsigned int maxItems = 0;
164 
169  bool uniqueItems = false;
170 
176  std::vector<JSONSchemaTypeDefinitionPtr> additionalItems;
177 
183  {
184  public:
186 
187  void add(const JSONSchemaTypeDefinitionPtr& property);
188 
189  typedef std::map<std::string, JSONSchemaTypeDefinitionPtr>::const_iterator JSONSchemaPropertiesIterator;
190  JSONSchemaPropertiesIterator begin() const;
191  JSONSchemaPropertiesIterator find(const std::string& key) const;
192  JSONSchemaPropertiesIterator end() const;
193  unsigned int size() const;
194  private:
195  std::map<std::string, JSONSchemaTypeDefinitionPtr> m_propertiesmap;
196  };
197 
203 
209 
213  JSONSchemaTypeDefinitionPtr additionalProperties;
214  };
215 
225  class JsonRpcMethod : protected CJSONUtils
226  {
227  public:
228  JsonRpcMethod();
229 
230  bool Parse(const CVariant &value);
231  JSONRPC_STATUS Check(const CVariant &requestParameters, ITransportLayer *transport, IClient *client, bool notification, MethodCall &methodCall, CVariant &outputParameters) const;
232 
233  std::string missingReference;
234 
238  std::string name;
243  MethodCall method;
248  TransportLayerCapability transportneed = Response;
253  OperationPermission permission = ReadData;
257  std::string description;
261  std::vector<JSONSchemaTypeDefinitionPtr> parameters;
265  JSONSchemaTypeDefinitionPtr returns;
266 
267  private:
268  bool parseParameter(const CVariant& value, const JSONSchemaTypeDefinitionPtr& parameter);
269  bool parseReturn(const CVariant &value);
270  static JSONRPC_STATUS checkParameter(const CVariant& requestParameters,
271  const JSONSchemaTypeDefinitionPtr& type,
272  unsigned int position,
273  CVariant& outputParameters,
274  unsigned int& handled,
275  CVariant& errorData);
276  };
277 
283  typedef struct
284  {
288  std::string name;
294  MethodCall method;
296 
311  {
312  friend class JSONSchemaTypeDefinition;
313  friend class JsonRpcMethod;
314  public:
321  static bool AddType(const std::string &jsonType);
322 
330  static bool AddMethod(const std::string &jsonMethod, MethodCall method);
331 
338  static bool AddBuiltinMethod(const std::string &jsonMethod);
339 
346  static bool AddNotification(const std::string &jsonNotification);
347 
348  static bool AddEnum(const std::string &name, const std::vector<CVariant> &values, CVariant::VariantType type = CVariant::VariantTypeNull, const CVariant &defaultValue = CVariant::ConstNullVariant);
349  static bool AddEnum(const std::string &name, const std::vector<std::string> &values);
350  static bool AddEnum(const std::string &name, const std::vector<int> &values);
351 
357  static const char* GetVersion();
358 
368  static JSONRPC_STATUS Print(CVariant &result, ITransportLayer *transport, IClient *client, bool printDescriptions = true, bool printMetadata = false, bool filterByTransport = true, const std::string &filterByName = "", const std::string &filterByType = "", bool printReferences = true);
369 
386  static JSONRPC_STATUS CheckCall(const char* method, const CVariant &requestParameters, ITransportLayer *transport, IClient *client, bool notification, MethodCall &methodCall, CVariant &outputParameters);
387 
388  static JSONSchemaTypeDefinitionPtr GetType(const std::string &identification);
389 
390  static void ResolveReferences();
391  static void Cleanup();
392 
393  private:
394  static bool prepareDescription(std::string &description, CVariant &descriptionObject, std::string &name);
395  static bool addMethod(const std::string &jsonMethod, MethodCall method);
396  static void parseHeader(const CVariant &descriptionObject);
397  static bool parseJSONSchemaType(const CVariant &value, std::vector<JSONSchemaTypeDefinitionPtr>& typeDefinitions, JSONSchemaType &schemaType, std::string &missingReference);
398  static void addReferenceTypeDefinition(const JSONSchemaTypeDefinitionPtr& typeDefinition);
399  static void removeReferenceTypeDefinition(const std::string &typeID);
400 
401  static void getReferencedTypes(const JSONSchemaTypeDefinitionPtr& type,
402  std::vector<std::string>& referencedTypes);
403 
404  class CJsonRpcMethodMap
405  {
406  public:
407  CJsonRpcMethodMap();
408 
409  void add(const JsonRpcMethod &method);
410 
411  typedef std::map<std::string, JsonRpcMethod>::const_iterator JsonRpcMethodIterator;
412  JsonRpcMethodIterator begin() const;
413  JsonRpcMethodIterator find(const std::string& key) const;
414  JsonRpcMethodIterator end() const;
415 
416  void clear();
417  private:
418  std::map<std::string, JsonRpcMethod> m_actionmap;
419  };
420 
421  static CJsonRpcMethodMap m_actionMap;
422  static std::map<std::string, JSONSchemaTypeDefinitionPtr> m_types;
423  static std::map<std::string, CVariant> m_notifications;
424  static JsonRpcMethodMap m_methodMaps[];
425 
426  typedef enum SchemaDefinition
427  {
428  SchemaDefinitionType,
429  SchemaDefinitionMethod
430  } SchemaDefinition;
431 
432  typedef struct IncompleteSchemaDefinition
433  {
434  std::string Schema;
435  SchemaDefinition Type;
436  MethodCall Method;
437  } IncompleteSchemaDefinition;
438 
439  typedef std::map<std::string, std::vector<IncompleteSchemaDefinition> > IncompleteSchemaDefinitionMap;
440  static IncompleteSchemaDefinitionMap m_incompleteDefinitions;
441  };
442 }
JSONSchemaTypeDefinitionPtr additionalProperties
Type definition for additional properties.
Definition: JSONServiceDescription.h:213
double minimum
Minimum value for Integer or Number types.
Definition: JSONServiceDescription.h:109
int maxLength
Maximum length for String types.
Definition: JSONServiceDescription.h:142
std::vector< JSONSchemaTypeDefinitionPtr > unionTypes
JSON schema type definitions in case of a union type.
Definition: JSONServiceDescription.h:91
Structure mapping a json rpc method definition to an actual method implementation.
Definition: JSONServiceDescription.h:283
int minLength
Minimum length for String types.
Definition: JSONServiceDescription.h:137
bool optional
Whether or not the parameter is optional.
Definition: JSONServiceDescription.h:97
std::vector< CVariant > enums
(Optional) List of allowed values for the type
Definition: JSONServiceDescription.h:148
std::string ID
Id of the type (for referenced types) Renamed from "id" because of possible issues with Objective-C...
Definition: JSONServiceDescription.h:58
std::vector< JSONSchemaTypeDefinitionPtr > items
List of possible values in an array.
Definition: JSONServiceDescription.h:153
Maps a properties name to its json schema type definition.
Definition: JSONServiceDescription.h:182
bool referencedTypeSet
Whether the type has been set based on the referenced type.
Definition: JSONServiceDescription.h:69
std::string description
Description of the method.
Definition: JSONServiceDescription.h:257
std::vector< JSONSchemaTypeDefinitionPtr > parameters
List of accepted parameters.
Definition: JSONServiceDescription.h:261
CJsonSchemaPropertiesMap properties
List of properties of the parameter (only needed when the parameter is an object) ...
Definition: JSONServiceDescription.h:202
unsigned int divisibleBy
Integer by which the value (of type Integer) must be divisible without rest.
Definition: JSONServiceDescription.h:132
Definition: AddonsOperations.h:23
Definition: Variant.h:31
std::string name
Name of the represented method.
Definition: JSONServiceDescription.h:238
Helper class for json schema service descriptor based service descriptions for the json rpc API...
Definition: JSONServiceDescription.h:310
Structure for a published json rpc method.
Definition: JSONServiceDescription.h:225
MethodCall method
Pointer tot he implementation of the represented method.
Definition: JSONServiceDescription.h:243
CVariant defaultValue
Default value of the parameter (only needed when it is optional)
Definition: JSONServiceDescription.h:103
Class for a parameter of a json rpc method.
Definition: JSONServiceDescription.h:34
double maximum
Maximum value for Integer or Number types.
Definition: JSONServiceDescription.h:114
JSONRPC_STATUS
Possible statuc codes of a response to a JSON-RPC request.
Definition: JSONRPCUtils.h:29
bool hasAdditionalProperties
Whether the type can have additional properties or not.
Definition: JSONServiceDescription.h:208
OperationPermission
Permission categories for json rpc methods.
Definition: JSONRPCUtils.h:56
std::string name
Name of the json rpc method.
Definition: JSONServiceDescription.h:288
bool exclusiveMaximum
Whether to exclude the defined Maximum value from the valid range or not.
Definition: JSONServiceDescription.h:126
std::vector< JSONSchemaTypeDefinitionPtr > extends
Array of reference types which are extended by this type.
Definition: JSONServiceDescription.h:75
JSONSchemaTypeDefinitionPtr returns
Definition of the return value.
Definition: JSONServiceDescription.h:265
unsigned int minItems
Minimum amount of items in the array.
Definition: JSONServiceDescription.h:158
bool uniqueItems
Whether every value in the array must be unique or not.
Definition: JSONServiceDescription.h:169
Definition: ITransportLayer.h:27
bool exclusiveMinimum
Whether to exclude the defined Minimum value from the valid range or not.
Definition: JSONServiceDescription.h:120
std::string description
Description of the parameter.
Definition: JSONServiceDescription.h:80
JSONSchemaType type
JSON schema type of the parameter&#39;s value.
Definition: JSONServiceDescription.h:85
std::string name
Name of the parameter (for by-name calls)
Definition: JSONServiceDescription.h:50
Definition: IClient.h:13
JSONSchemaTypeDefinitionPtr referencedType
Referenced object.
Definition: JSONServiceDescription.h:63
unsigned int maxItems
Maximum amount of items in the array.
Definition: JSONServiceDescription.h:163
std::vector< JSONSchemaTypeDefinitionPtr > additionalItems
List of json schema definitions for additional items in an array with tuple typing (defined schemas i...
Definition: JSONServiceDescription.h:176
Helper class containing utility methods to handle json rpc method calls.
Definition: JSONUtils.h:46
MethodCall method
Pointer to the actual implementation of the json rpc method.
Definition: JSONServiceDescription.h:294