1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include "JSONRPCUtils.h"
#include "playlists/SmartPlayList.h"
#include "utils/JSONVariantParser.h"
#include "utils/JSONVariantWriter.h"
#include "utils/SortUtils.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include <stdlib.h>
#include <string.h>
#include <vector>
class CDateTime;
namespace JSONRPC
{
/*!
\brief Possible value types of a parameter or return type
*/
enum JSONSchemaType
{
NullValue = 0x01,
StringValue = 0x02,
NumberValue = 0x04,
IntegerValue = 0x08,
BooleanValue = 0x10,
ArrayValue = 0x20,
ObjectValue = 0x40,
AnyValue = 0x80
};
/*!
\ingroup jsonrpc
\brief Helper class containing utility methods to handle
json rpc method calls.*/
class CJSONUtils
{
public:
static void MillisecondsToTimeObject(int time, CVariant &result)
{
int ms = time % 1000;
result["milliseconds"] = ms;
time = (time - ms) / 1000;
int s = time % 60;
result["seconds"] = s;
time = (time - s) / 60;
int m = time % 60;
result["minutes"] = m;
time = (time -m) / 60;
result["hours"] = time;
}
protected:
static void HandleLimits(const CVariant ¶meterObject, CVariant &result, int size, int &start, int &end)
{
if (size < 0)
size = 0;
start = (int)parameterObject["limits"]["start"].asInteger();
end = (int)parameterObject["limits"]["end"].asInteger();
end = (end <= 0 || end > size) ? size : end;
start = start > end ? end : start;
result["limits"]["start"] = start;
result["limits"]["end"] = end;
result["limits"]["total"] = size;
}
static bool ParseSorting(const CVariant ¶meterObject, SortBy &sortBy, SortOrder &sortOrder, SortAttribute &sortAttributes)
{
std::string method = parameterObject["sort"]["method"].asString();
std::string order = parameterObject["sort"]["order"].asString();
StringUtils::ToLower(method);
StringUtils::ToLower(order);
// parse the sort attributes
sortAttributes = SortAttributeNone;
if (parameterObject["sort"]["ignorearticle"].asBoolean())
sortAttributes = static_cast<SortAttribute>(sortAttributes | SortAttributeIgnoreArticle);
if (parameterObject["sort"]["useartistsortname"].asBoolean())
sortAttributes = static_cast<SortAttribute>(sortAttributes | SortAttributeUseArtistSortName);
// parse the sort order
sortOrder = SortUtils::SortOrderFromString(order);
if (sortOrder == SortOrderNone)
return false;
// parse the sort method
sortBy = SortUtils::SortMethodFromString(method);
return true;
}
static void ParseLimits(const CVariant ¶meterObject, int &limitStart, int &limitEnd)
{
limitStart = (int)parameterObject["limits"]["start"].asInteger();
limitEnd = (int)parameterObject["limits"]["end"].asInteger();
}
/*!
\brief Checks if the given object contains a parameter
\param parameterObject Object to check for a parameter
\param key Possible name of the parameter
\param position Possible position of the parameter
\return True if the parameter is available otherwise false
Checks the given object for a parameter with the given key (if
the given object is not an array) or for a parameter at the
given position (if the given object is an array).
*/
static inline bool ParameterExists(const CVariant& parameterObject,
const std::string& key,
unsigned int position)
{
return IsValueMember(parameterObject, key) ||
(parameterObject.isArray() && parameterObject.size() > position);
}
/*!
\brief Checks if the given object contains a value
with the given key
\param value Value to check for the member
\param key Key of the member to check for
\return True if the given object contains a member with
the given key otherwise false
*/
static inline bool IsValueMember(const CVariant& value, const std::string& key)
{
return value.isMember(key);
}
/*!
\brief Returns the json value of a parameter
\param parameterObject Object containing all provided parameters
\param key Possible name of the parameter
\param position Possible position of the parameter
\return Json value of the parameter with the given name or at the
given position
Returns the value of the parameter with the given key (if
the given object is not an array) or of the parameter at the
given position (if the given object is an array).
*/
static inline CVariant GetParameter(const CVariant& parameterObject,
const std::string& key,
unsigned int position)
{
return IsValueMember(parameterObject, key) ? parameterObject[key] : parameterObject[position];
}
/*!
\brief Returns the json value of a parameter or the given
default value
\param parameterObject Object containing all provided parameters
\param key Possible name of the parameter
\param position Possible position of the parameter
\param fallback Default value of the parameter
\return Json value of the parameter with the given name or at the
given position or the default value if the parameter does not exist
Returns the value of the parameter with the given key (if
the given object is not an array) or of the parameter at the
given position (if the given object is an array). If the
parameter does not exist the given default value is returned.
*/
static inline CVariant GetParameter(const CVariant& parameterObject,
const std::string& key,
unsigned int position,
const CVariant& fallback)
{
return IsValueMember(parameterObject, key)
? parameterObject[key]
: ((parameterObject.isArray() && parameterObject.size() > position)
? parameterObject[position]
: fallback);
}
/*!
\brief Returns the given json value as a string
\param value Json value to convert to a string
\param defaultValue Default string value
\return String value of the given json value or the default value
if the given json value is no string
*/
static inline std::string GetString(const CVariant &value, const char* defaultValue)
{
std::string str = defaultValue;
if (value.isString())
{
str = value.asString();
}
return str;
}
/*!
\brief Returns a TransportLayerCapability value of the
given string representation
\param transport String representation of the TransportLayerCapability
\return TransportLayerCapability value of the given string representation
*/
static inline TransportLayerCapability StringToTransportLayer(const std::string& transport)
{
if (transport.compare("Announcing") == 0)
return Announcing;
if (transport.compare("FileDownloadDirect") == 0)
return FileDownloadDirect;
if (transport.compare("FileDownloadRedirect") == 0)
return FileDownloadRedirect;
return Response;
}
/*!
\brief Returns a JSONSchemaType value for the given
string representation
\param valueType String representation of the JSONSchemaType
\return JSONSchemaType value of the given string representation
*/
static inline JSONSchemaType StringToSchemaValueType(const std::string& valueType)
{
if (valueType.compare("null") == 0)
return NullValue;
if (valueType.compare("string") == 0)
return StringValue;
if (valueType.compare("number") == 0)
return NumberValue;
if (valueType.compare("integer") == 0)
return IntegerValue;
if (valueType.compare("boolean") == 0)
return BooleanValue;
if (valueType.compare("array") == 0)
return ArrayValue;
if (valueType.compare("object") == 0)
return ObjectValue;
return AnyValue;
}
/*!
\brief Returns a string representation for the
given JSONSchemaType
\param valueType Specific JSONSchemaType
\return String representation of the given JSONSchemaType
*/
static inline std::string SchemaValueTypeToString(JSONSchemaType valueType)
{
std::vector<JSONSchemaType> types = std::vector<JSONSchemaType>();
for (unsigned int value = 0x01; value <= (unsigned int)AnyValue; value *= 2)
{
if (HasType(valueType, (JSONSchemaType)value))
types.push_back((JSONSchemaType)value);
}
std::string strType;
if (types.size() > 1)
strType.append("[");
for (unsigned int index = 0; index < types.size(); index++)
{
if (index > 0)
strType.append(", ");
switch (types.at(index))
{
case StringValue:
strType.append("string");
break;
case NumberValue:
strType.append("number");
break;
case IntegerValue:
strType.append("integer");
break;
case BooleanValue:
strType.append("boolean");
break;
case ArrayValue:
strType.append("array");
break;
case ObjectValue:
strType.append("object");
break;
case AnyValue:
strType.append("any");
break;
case NullValue:
strType.append("null");
break;
default:
strType.append("unknown");
}
}
if (types.size() > 1)
strType.append("]");
return strType;
}
/*!
\brief Converts the given json schema type into
a json object
\param valueTye json schema type(s)
\param jsonObject json object into which the json schema type(s) are stored
*/
static inline void SchemaValueTypeToJson(JSONSchemaType valueType, CVariant &jsonObject)
{
jsonObject = CVariant(CVariant::VariantTypeArray);
for (unsigned int value = 0x01; value <= (unsigned int)AnyValue; value *= 2)
{
if (HasType(valueType, (JSONSchemaType)value))
jsonObject.append(SchemaValueTypeToString((JSONSchemaType)value));
}
if (jsonObject.size() == 1)
{
CVariant jsonType = jsonObject[0];
jsonObject = jsonType;
}
}
static inline const char *ValueTypeToString(CVariant::VariantType valueType)
{
switch (valueType)
{
case CVariant::VariantTypeString:
return "string";
case CVariant::VariantTypeDouble:
return "number";
case CVariant::VariantTypeInteger:
case CVariant::VariantTypeUnsignedInteger:
return "integer";
case CVariant::VariantTypeBoolean:
return "boolean";
case CVariant::VariantTypeArray:
return "array";
case CVariant::VariantTypeObject:
return "object";
case CVariant::VariantTypeNull:
case CVariant::VariantTypeConstNull:
return "null";
default:
return "unknown";
}
}
/*!
\brief Checks if the parameter with the given name or at
the given position is of a certain type
\param parameterObject Object containing all provided parameters
\param key Possible name of the parameter
\param position Possible position of the parameter
\param valueType Expected type of the parameter
\return True if the specific parameter is of the given type otherwise false
*/
static inline bool IsParameterType(const CVariant ¶meterObject, const char *key, unsigned int position, JSONSchemaType valueType)
{
if ((valueType & AnyValue) == AnyValue)
return true;
CVariant parameter;
if (IsValueMember(parameterObject, key))
parameter = parameterObject[key];
else if(parameterObject.isArray() && parameterObject.size() > position)
parameter = parameterObject[position];
return IsType(parameter, valueType);
}
/*!
\brief Checks if the given json value is of the given type
\param value Json value to check
\param valueType Expected type of the json value
\return True if the given json value is of the given type otherwise false
*/
static inline bool IsType(const CVariant &value, JSONSchemaType valueType)
{
if (HasType(valueType, AnyValue))
return true;
if (HasType(valueType, StringValue) && value.isString())
return true;
if (HasType(valueType, NumberValue) && (value.isInteger() || value.isUnsignedInteger() || value.isDouble()))
return true;
if (HasType(valueType, IntegerValue) && (value.isInteger() || value.isUnsignedInteger()))
return true;
if (HasType(valueType, BooleanValue) && value.isBoolean())
return true;
if (HasType(valueType, ArrayValue) && value.isArray())
return true;
if (HasType(valueType, ObjectValue) && value.isObject())
return true;
return value.isNull();
}
/*!
\brief Sets the value of the given json value to the
default value of the given type
\param value Json value to be set
\param valueType Type of the default value
*/
static inline void SetDefaultValue(CVariant &value, JSONSchemaType valueType)
{
switch (valueType)
{
case StringValue:
value = CVariant("");
break;
case NumberValue:
value = CVariant(CVariant::VariantTypeDouble);
break;
case IntegerValue:
value = CVariant(CVariant::VariantTypeInteger);
break;
case BooleanValue:
value = CVariant(CVariant::VariantTypeBoolean);
break;
case ArrayValue:
value = CVariant(CVariant::VariantTypeArray);
break;
case ObjectValue:
value = CVariant(CVariant::VariantTypeObject);
break;
default:
value = CVariant(CVariant::VariantTypeNull);
}
}
static inline bool HasType(JSONSchemaType typeObject, JSONSchemaType type) { return (typeObject & type) == type; }
static inline bool ParameterNotNull(const CVariant& parameterObject, const std::string& key)
{
return parameterObject.isMember(key) && !parameterObject[key].isNull();
}
/*!
\brief Copies the values from the jsonStringArray to the stringArray.
stringArray is cleared.
\param jsonStringArray JSON object representing a string array
\param stringArray String array where the values are copied into (cleared)
*/
static void CopyStringArray(const CVariant &jsonStringArray, std::vector<std::string> &stringArray)
{
if (!jsonStringArray.isArray())
return;
stringArray.clear();
for (CVariant::const_iterator_array it = jsonStringArray.begin_array(); it != jsonStringArray.end_array(); ++it)
stringArray.push_back(it->asString());
}
static void SetFromDBDate(const CVariant& jsonDate, CDateTime& date);
static void SetFromDBDateTime(const CVariant& jsonDate, CDateTime& date);
static bool GetXspFiltering(const std::string &type, const CVariant &filter, std::string &xsp)
{
if (type.empty() || !filter.isObject())
return false;
CVariant xspObj(CVariant::VariantTypeObject);
xspObj["type"] = type;
if (filter.isMember("field"))
{
xspObj["rules"]["and"] = CVariant(CVariant::VariantTypeArray);
xspObj["rules"]["and"].push_back(filter);
}
else
xspObj["rules"] = filter;
CSmartPlaylist playlist;
return playlist.Load(xspObj) && playlist.SaveAsJson(xsp, false);
}
};
}
|