-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonInterface.h
93 lines (70 loc) · 2.69 KB
/
jsonInterface.h
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
// File: jsonInterface.h
// Date: 7/7/2016
// Auth: K. Loux
// Desc: Class for interfacing with a remote server using JSON and CURL.
#ifndef JSON_INTERFACE_H_
#define JSON_INTERFACE_H_
// Local headers
#include "cJSON/cJSON.h"
// utilities headers
#include "utilities/uString.h"
// Standard C++ headers
#include <vector>
#include <ctime>
// cJSON forward declarations
struct cJSON;
// for cURL
typedef void CURL;
class JSONInterface
{
public:
explicit JSONInterface(const UString::String& userAgent = UString::String());
virtual ~JSONInterface() = default;
void SetCACertificatePath(const UString::String& path) { caCertificatePath = path; }
void SetVerboseOutput(const bool& verboseOutput = true) { verbose = verboseOutput; }
private:
const UString::String userAgent;
protected:
UString::String caCertificatePath;
bool verbose = false;
struct ModificationData
{
virtual ~ModificationData() = default;
};
typedef bool (*CURLModification)(CURL*, const ModificationData*);
static bool DoNothing(CURL*, const ModificationData*) { return true; }
bool DoCURLPost(const UString::String &url, const std::string &data,
std::string &response, CURLModification curlModification = &JSONInterface::DoNothing,
const ModificationData* modificationData = nullptr) const;
bool DoCURLGet(const UString::String &url, std::string &response,
CURLModification curlModification = &JSONInterface::DoNothing,
const ModificationData* modificationData = nullptr) const;
static bool ReadJSON(cJSON* root, const UString::String& field, int& value);
static bool ReadJSON(cJSON* root, const UString::String& field, unsigned int& value);
static bool ReadJSON(cJSON* root, const UString::String& field, UString::String &value);
static bool ReadJSON(cJSON* root, const UString::String& field, double& value);
static bool ReadJSON(cJSON* root, const UString::String& field, std::tm& value);
static bool ReadJSON(cJSON* root, const UString::String& field, bool& value);
template <typename T>
static bool ReadJSON(cJSON *root, const UString::String& field, std::vector<T>& v);
static bool ReadJSONArrayToVector(cJSON *root, const UString::String& field, std::vector<UString::String>& v);
static size_t CURLWriteCallback(char *ptr, size_t size, size_t nmemb, void *userData);
static UString::String URLEncode(const UString::String& s);
};
template <typename T>
bool JSONInterface::ReadJSON(cJSON *root, const UString::String& field, std::vector<T>& v)
{
v.resize(cJSON_GetArraySize(root));
unsigned int i(0);
for (auto& item : v)
{
cJSON* arrayItem(cJSON_GetArrayItem(root, i));
if (!arrayItem)
return false;
if (!ReadJSON(arrayItem, field, item))
return false;
++i;
}
return true;
}
#endif// JSON_INTERFACE_H_