-
Notifications
You must be signed in to change notification settings - Fork 0
/
AampJsonObject.h
executable file
·261 lines (230 loc) · 6.4 KB
/
AampJsonObject.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
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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _AAMP_JSON_OBJECT_H
#define _AAMP_JSON_OBJECT_H
/**
* @file AampJsonObject.h
* @brief File to handle Json format
*/
#include <string>
#include <vector>
#include <cjson/cJSON.h>
/**
* @class AampJsonObject
* @brief Utility class to construct a JSON string
*/
class AampJsonObject {
public:
AampJsonObject();
AampJsonObject(const std::string& jsonStr);
AampJsonObject(const char* jsonStr);
~AampJsonObject();
AampJsonObject(const AampJsonObject&) = delete;
AampJsonObject& operator=(const AampJsonObject&) = delete;
enum ENCODING
{
ENCODING_STRING, /**< Bytes encoded as a string as-is */
ENCODING_BASE64, /**< Bytes base64 encoded to a string */
ENCODING_BASE64_URL /**< Bytes base64url encoded to a string */
};
/**
* @fn add
*
* @param name name for the value
* @param value string value to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, const std::string& value, const ENCODING encoding = ENCODING_STRING);
/**
* @fn add
*
* @param name name for the value
* @param value string value to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, const char *value, const ENCODING encoding = ENCODING_STRING);
/**
* @fn add
*
* @param name name for the array
* @param values vector of strings to add as an array
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, const std::vector<std::string>& values);
/**
* @fn add
*
* @param name name for the value
* @param values vector of bytes to add in the specified encoding
* @param encoding how to encode the byte array
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, const std::vector<uint8_t>& values, const ENCODING encoding = ENCODING_STRING);
/**
* @fn add
*
* @param name name for the array
* @param values vector of #AampJsonObject to add as an array
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, std::vector<AampJsonObject*>& values);
/**
* @fn get
*
* @param name name for the array
* @param[out] values String Array
* @return true if successfully added, false otherwise
*/
bool get(const std::string& name, std::vector<std::string>& values);
/**
* @fn get
*
* @param name name for the property
* @param[out] values int value
* @return true if successfully added, false otherwise
*/
bool get(const std::string& name, int& value);
/**
* @fn get
*
* @param name name of the property to retrieve
* @param value string to populate with the retrieved value
* @return true if successfully retrieved value, false otherwise
*/
bool get(const std::string& name, std::string& value);
/**
* @fn fn
*
* @param name name of the property to retrieve
* @param values vector to populate with the retrieved value
* @param encoding the encoding of the string, used to determine how to decode the content into the vector
* @return true if successfully retrieved and decoded value, false otherwise
*/
bool get(const std::string& name, std::vector<uint8_t>& values, const ENCODING encoding = ENCODING_STRING);
/**
* @fn get
*
* @param name Name of the internal json data
* @param value[out] reference Object which return as json object inside json data.
* @return true if successfully retrieved and decoded value, false otherwise
*/
bool get(const std::string& name, AampJsonObject &value);
/**
* @fn print
*
* @return JSON string
*/
std::string print();
/**
* @fn print_UnFormatted
*
* @return JSON string
*/
std::string print_UnFormatted();
/**
* @fn print
*
* @param[out] data - vector to output printed JSON to
* @return void.
*/
void print(std::vector<uint8_t>& data);
/**
* @fn add
*
* @param name name for the value
* @param value bool to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, bool value);
/**
* @fn add
*
* @param name name for the value
* @param value int to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, int value);
/**
* @fn add
*
* @param name name for the value
* @param value double to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, double value);
/**
* @fn add
*
* @param name name for the value
* @param value long to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, long value);
bool add(const std::string& name, AampJsonObject& value);
/**
* @fn add
*
* @param name name for the value
* @param value long to add
* @return true if successfully added, false otherwise
*/
bool add(const std::string& name, cJSON *value);
/**
* @fn isArray
*
* @return true if it is Array or false
*/
bool isArray(const std::string& name);
/**
* @fn isString
*
* @return true if it is String or false
*/
bool isString(const std::string& name);
/**
* @fn isNumber
*
* @return true if it is Number or false
*/
bool isNumber(const std::string& name);
/**
* @fn isObject
*
* @return true if it is Object or false
*/
bool isObject(const std::string& name);
private:
bool set(AampJsonObject *parent, cJSON *object);
bool add(const std::string& name);
cJSON *mJsonObj;
AampJsonObject *mParent;
};
/**
* @class AampJsonParseException
* @brief Handles the exception for JSON parser
*/
class AampJsonParseException : public std::exception
{
public:
virtual const char* what() const throw()
{
return "Failed to parse JSON string";
}
};
#endif //_AAMP_JSON_OBJECT_H