RDK Documentation (Open Sourced RDK Components)
AampJsonObject.h
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2020 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #ifndef _AAMP_JSON_OBJECT_H
20 #define _AAMP_JSON_OBJECT_H
21 
22 /**
23  * @file AampJsonObject.h
24  * @brief File to handle Json format
25  */
26 
27 
28 #include <string>
29 #include <vector>
30 
31 #include <cjson/cJSON.h>
32 
33 /**
34  * @class AampJsonObject
35  * @brief Utility class to construct a JSON string
36  */
38 public:
40  AampJsonObject(const std::string& jsonStr);
41  AampJsonObject(const char* jsonStr);
42  ~AampJsonObject();
43  AampJsonObject(const AampJsonObject&) = delete;
44  AampJsonObject& operator=(const AampJsonObject&) = delete;
45 
46  enum ENCODING
47  {
48  ENCODING_STRING, /**< Bytes encoded as a string as-is */
49  ENCODING_BASE64, /**< Bytes base64 encoded to a string */
50  ENCODING_BASE64_URL /**< Bytes base64url encoded to a string */
51  };
52 
53  /**
54  * @fn add
55  *
56  * @param name name for the value
57  * @param value string value to add
58  * @return true if successfully added, false otherwise
59  */
60  bool add(const std::string& name, const std::string& value, const ENCODING encoding = ENCODING_STRING);
61 
62  /**
63  * @fn add
64  *
65  * @param name name for the value
66  * @param value string value to add
67  * @return true if successfully added, false otherwise
68  */
69  bool add(const std::string& name, const char *value, const ENCODING encoding = ENCODING_STRING);
70 
71  /**
72  * @fn add
73  *
74  * @param name name for the array
75  * @param values vector of strings to add as an array
76  * @return true if successfully added, false otherwise
77  */
78  bool add(const std::string& name, const std::vector<std::string>& values);
79 
80  /**
81  * @fn add
82  *
83  * @param name name for the value
84  * @param values vector of bytes to add in the specified encoding
85  * @param encoding how to encode the byte array
86  * @return true if successfully added, false otherwise
87  */
88  bool add(const std::string& name, const std::vector<uint8_t>& values, const ENCODING encoding = ENCODING_STRING);
89 
90  /**
91  * @fn add
92  *
93  * @param name name for the array
94  * @param values vector of #AampJsonObject to add as an array
95  * @return true if successfully added, false otherwise
96  */
97  bool add(const std::string& name, std::vector<AampJsonObject*>& values);
98 
99  /**
100  * @fn get
101  *
102  * @param name name for the array
103  * @param[out] values String Array
104  * @return true if successfully added, false otherwise
105  */
106  bool get(const std::string& name, std::vector<std::string>& values);
107 
108  /**
109  * @fn get
110  *
111  * @param name name for the property
112  * @param[out] values int value
113  * @return true if successfully added, false otherwise
114  */
115  bool get(const std::string& name, int& value);
116 
117  /**
118  * @fn get
119  *
120  * @param name name of the property to retrieve
121  * @param value string to populate with the retrieved value
122  * @return true if successfully retrieved value, false otherwise
123  */
124  bool get(const std::string& name, std::string& value);
125 
126  /**
127  * @fn fn
128  *
129  * @param name name of the property to retrieve
130  * @param values vector to populate with the retrieved value
131  * @param encoding the encoding of the string, used to determine how to decode the content into the vector
132  * @return true if successfully retrieved and decoded value, false otherwise
133  */
134  bool get(const std::string& name, std::vector<uint8_t>& values, const ENCODING encoding = ENCODING_STRING);
135 
136  /**
137  * @fn get
138  *
139  * @param name Name of the internal json data
140  * @param value[out] reference Object which return as json object inside json data.
141  * @return true if successfully retrieved and decoded value, false otherwise
142  */
143  bool get(const std::string& name, AampJsonObject &value);
144 
145  /**
146  * @fn print
147  *
148  * @return JSON string
149  */
150  std::string print();
151 
152 
153  /**
154  * @fn print_UnFormatted
155  *
156  * @return JSON string
157  */
158  std::string print_UnFormatted();
159 
160  /**
161  * @fn print
162  *
163  * @param[out] data - vector to output printed JSON to
164  * @return void.
165  */
166  void print(std::vector<uint8_t>& data);
167  /**
168  * @fn add
169  *
170  * @param name name for the value
171  * @param value bool to add
172  * @return true if successfully added, false otherwise
173  */
174  bool add(const std::string& name, bool value);
175  /**
176  * @fn add
177  *
178  * @param name name for the value
179  * @param value int to add
180  * @return true if successfully added, false otherwise
181  */
182  bool add(const std::string& name, int value);
183 
184  /**
185  * @fn add
186  *
187  * @param name name for the value
188  * @param value double to add
189  * @return true if successfully added, false otherwise
190  */
191  bool add(const std::string& name, double value);
192 
193  /**
194  * @fn add
195  *
196  * @param name name for the value
197  * @param value long to add
198  * @return true if successfully added, false otherwise
199  */
200  bool add(const std::string& name, long value);
201 
202  bool add(const std::string& name, AampJsonObject& value);
203 
204  /**
205  * @fn add
206  *
207  * @param name name for the value
208  * @param value long to add
209  * @return true if successfully added, false otherwise
210  */
211  bool add(const std::string& name, cJSON *value);
212 
213  /**
214  * @fn isArray
215  *
216  * @return true if it is Array or false
217  */
218  bool isArray(const std::string& name);
219 
220  /**
221  * @fn isString
222  *
223  * @return true if it is String or false
224  */
225  bool isString(const std::string& name);
226 
227  /**
228  * @fn isNumber
229  *
230  * @return true if it is Number or false
231  */
232  bool isNumber(const std::string& name);
233 
234  /**
235  * @fn isObject
236  *
237  * @return true if it is Object or false
238  */
239  bool isObject(const std::string& name);
240 
241 private:
242  bool set(AampJsonObject *parent, cJSON *object);
243  bool add(const std::string& name);
244  cJSON *mJsonObj;
245  AampJsonObject *mParent;
246 };
247 
248 /**
249  * @class AampJsonParseException
250  * @brief Handles the exception for JSON parser
251  */
252 class AampJsonParseException : public std::exception
253 {
254 public:
255  virtual const char* what() const throw()
256  {
257  return "Failed to parse JSON string";
258  }
259 };
260 
261 #endif //_AAMP_JSON_OBJECT_H
AampJsonObject::set
bool set(AampJsonObject *parent, cJSON *object)
Set cJSON value.
Definition: AampJsonObject.cpp:229
AampJsonParseException
Handles the exception for JSON parser.
Definition: AampJsonObject.h:252
AampJsonObject::get
bool get(const std::string &name, std::vector< std::string > &values)
Get a string value.
Definition: AampJsonObject.cpp:291
AampJsonObject::isArray
bool isArray(const std::string &name)
Check whether the value is Array or not.
Definition: AampJsonObject.cpp:404
AampJsonObject::isObject
bool isObject(const std::string &name)
Check whether the value is Object or not.
Definition: AampJsonObject.cpp:446
AampJsonObject::isString
bool isString(const std::string &name)
Check whether the value is String or not.
Definition: AampJsonObject.cpp:418
AampJsonObject::ENCODING_BASE64
@ ENCODING_BASE64
Definition: AampJsonObject.h:49
AampJsonObject::print
std::string print()
Print the constructed JSON to a string.
Definition: AampJsonObject.cpp:365
AampJsonObject::ENCODING
ENCODING
Definition: AampJsonObject.h:46
AampJsonObject::ENCODING_STRING
@ ENCODING_STRING
Definition: AampJsonObject.h:48
AampJsonObject::isNumber
bool isNumber(const std::string &name)
Check whether the value is Number or not.
Definition: AampJsonObject.cpp:432
AampJsonObject::print_UnFormatted
std::string print_UnFormatted()
Print the constructed JSON to a string.
Definition: AampJsonObject.cpp:380
AampJsonObject::add
bool add(const std::string &name, const std::string &value, const ENCODING encoding=ENCODING_STRING)
Add a string value.
Definition: AampJsonObject.cpp:69
AampJsonObject::ENCODING_BASE64_URL
@ ENCODING_BASE64_URL
Definition: AampJsonObject.h:50
AampJsonObject
Utility class to construct a JSON string.
Definition: AampJsonObject.h:37