AlexaClientSDK  1.25.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
JSONUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONUTILS_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONUTILS_H_
18 
19 #include <cstdint>
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include <rapidjson/document.h>
25 
27 
28 namespace alexaClientSDK {
29 namespace avsCommon {
30 namespace utils {
31 namespace json {
32 namespace jsonUtils {
33 
37 inline static std::string getTag() {
38  return "JsonUtils";
39 }
40 
51 bool findNode(
52  const rapidjson::Value& jsonNode,
53  const std::string& key,
54  rapidjson::Value::ConstMemberIterator* iteratorPtr);
55 
63 bool parseJSON(const std::string& jsonContent, rapidjson::Document* document);
64 
72 bool convertToValue(const rapidjson::Value& documentNode, std::string* value);
73 
81 bool convertToValue(const rapidjson::Value& documentNode, int64_t* value);
82 
91 bool convertToValue(const rapidjson::Value& documentNode, uint64_t* value);
92 
100 bool convertToValue(const rapidjson::Value& documentNode, bool* value);
101 
109 bool convertToValue(const rapidjson::Value& documentNode, double* value);
110 
123 template <typename T>
124 bool retrieveValue(const rapidjson::Value& jsonNode, const std::string& key, T* value) {
125  if (!value) {
126  logger::acsdkError(logger::LogEntry(getTag(), "retrieveValueFailed").d("reason", "nullValue"));
127  return false;
128  }
129 
130  rapidjson::Value::ConstMemberIterator iterator;
131  if (!findNode(jsonNode, key, &iterator)) {
132  return false;
133  }
134 
135  return convertToValue(iterator->value, value);
136 }
137 
149 template <typename T>
150 bool retrieveValue(const std::string jsonString, const std::string& key, T* value) {
151  if (!value) {
152  logger::acsdkError(logger::LogEntry(getTag(), "retrieveValueFailed").d("reason", "nullValue"));
153  return false;
154  }
155 
156  rapidjson::Document document;
157  if (!parseJSON(jsonString, &document)) {
158  logger::acsdkError(logger::LogEntry(getTag(), "retrieveValueFailed").d("reason", "parsingError"));
159  return false;
160  }
161 
162  return retrieveValue(document, key, value);
163 }
164 
172 bool jsonArrayExists(const rapidjson::Value& parsedDocument, const std::string& key);
173 
190 template <class CollectionT>
191 CollectionT retrieveStringArray(const std::string& jsonString, const std::string& key);
192 
203 template <class CollectionT>
204 CollectionT retrieveStringArray(const std::string& jsonString);
205 
213 template <class CollectionT>
214 CollectionT retrieveStringArray(const rapidjson::Value& value);
215 
222 template <class CollectionT>
223 std::string convertToJsonString(const CollectionT& elements);
224 
233 std::map<std::string, std::string> retrieveStringMap(const rapidjson::Value& value, const std::string& key);
234 
244  const rapidjson::Value& value,
245  const std::string& key,
246  std::map<std::string, std::string>& elements);
247 
258  const rapidjson::Value& value,
259  const std::string& key,
260  std::vector<std::map<std::string, std::string>>& elements);
261 
262 //----- Templated functions implementation and specializations.
263 
264 template <>
265 std::vector<std::string> retrieveStringArray<std::vector<std::string>>(
266  const std::string& jsonString,
267  const std::string& key);
268 
269 template <>
270 std::vector<std::string> retrieveStringArray<std::vector<std::string>>(const std::string& jsonString);
271 
272 template <>
273 std::vector<std::string> retrieveStringArray<std::vector<std::string>>(const rapidjson::Value& value);
274 
275 template <>
276 std::string convertToJsonString<std::vector<std::string>>(const std::vector<std::string>& elements);
277 
278 template <class CollectionT>
279 CollectionT retrieveStringArray(const std::string& jsonString, const std::string& key) {
280  auto values = retrieveStringArray<std::vector<std::string>>(jsonString, key);
281  return CollectionT{values.begin(), values.end()};
282 }
283 
284 template <class CollectionT>
285 CollectionT retrieveStringArray(const std::string& jsonString) {
286  auto values = retrieveStringArray<std::vector<std::string>>(jsonString);
287  return CollectionT{values.begin(), values.end()};
288 }
289 
290 template <class CollectionT>
291 CollectionT retrieveStringArray(const rapidjson::Value& value) {
292  auto values = retrieveStringArray<std::vector<std::string>>(value);
293  return CollectionT{values.begin(), values.end()};
294 }
295 
296 template <class CollectionT>
297 std::string convertToJsonString(const CollectionT& elements) {
298  return convertToJsonString(std::vector<std::string>{elements.begin(), elements.end()});
299 }
300 
301 } // namespace jsonUtils
302 } // namespace json
303 } // namespace utils
304 } // namespace avsCommon
305 } // namespace alexaClientSDK
306 
307 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONUTILS_H_
std::map< std::string, std::string > retrieveStringMap(const rapidjson::Value &value, const std::string &key)
Definition: JSONUtils.cpp:340
std::string convertToJsonString(const CollectionT &elements)
Definition: JSONUtils.h:297
bool retrieveArrayOfStringMapFromArray(const rapidjson::Value &value, const std::string &key, std::vector< std::map< std::string, std::string >> &elements)
Definition: JSONUtils.cpp:398
CollectionT retrieveStringArray(const std::string &jsonString, const std::string &key)
Definition: JSONUtils.h:279
bool findNode(const rapidjson::Value &jsonNode, const std::string &key, rapidjson::Value::ConstMemberIterator *iteratorPtr)
Definition: JSONUtils.cpp:124
bool retrieveValue(const rapidjson::Value &jsonNode, const std::string &key, T *value)
Definition: JSONUtils.h:124
static std::string getTag()
Definition: JSONUtils.h:37
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool convertToValue(const rapidjson::Value &documentNode, std::string *value)
Definition: JSONUtils.cpp:146
void acsdkError(const LogEntry &entry)
Definition: LoggerUtils.cpp:80
void retrieveStringMapFromArray(const rapidjson::Value &value, const std::string &key, std::map< std::string, std::string > &elements)
Definition: JSONUtils.cpp:358
bool parseJSON(const std::string &jsonContent, rapidjson::Document *document)
Definition: JSONUtils.cpp:81
bool jsonArrayExists(const rapidjson::Value &parsedDocument, const std::string &key)
Definition: JSONUtils.cpp:249
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:32

AlexaClientSDK 1.25.0 - Copyright 2016-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0