AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
JSONGenerator.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 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONGENERATOR_H_
16 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONGENERATOR_H_
17 
18 #include <cstdint>
19 #include <string>
20 #include <type_traits>
21 
22 #include <rapidjson/stringbuffer.h>
23 #include <rapidjson/writer.h>
24 
25 namespace alexaClientSDK {
26 namespace avsCommon {
27 namespace utils {
28 namespace json {
29 
48 public:
52  JsonGenerator();
53 
57  ~JsonGenerator() = default;
58 
64  bool isFinalized();
65 
72  bool startObject(const std::string& key);
73 
79  bool finishObject();
80 
87  bool startArray(const std::string& key);
88 
94  bool startArrayElement();
95 
101  bool finishArrayElement();
102 
108  bool finishArray();
109 
111 
118  bool addMember(const std::string& key, const char* value);
119  bool addMember(const std::string& key, const std::string& value);
120  bool addMember(const std::string& key, int64_t value);
121  bool addMember(const std::string& key, uint64_t value);
122  bool addMember(const std::string& key, int value);
123  bool addMember(const std::string& key, unsigned int value);
124  bool addMember(const std::string& key, bool value);
125  bool addMember(const std::string& key, double value);
127 
138  template <typename CollectionT, typename ValueT = typename CollectionT::value_type>
139  bool addStringArray(const std::string& key, const CollectionT& collection);
140 
152  template <typename CollectionT, typename ValueT = typename CollectionT::value_type>
153  bool addMembersArray(const std::string& key, const CollectionT& collection);
154 
163  bool addRawJsonMember(const std::string& key, const std::string& json, bool validate = true);
164 
176  template <
177  typename CollectionArrayT,
178  typename CollectionValueT = typename CollectionArrayT::value_type,
179  typename ValueT = typename CollectionValueT::value_type>
180  bool addCollectionOfStringArray(const std::string& key, const CollectionArrayT& collection);
181 
191  std::string toString(bool finalize = true);
192 
193 private:
195  bool checkWriter();
196 
202  bool finalize();
203 
205  rapidjson::StringBuffer m_buffer;
206 
208  rapidjson::Writer<rapidjson::StringBuffer> m_writer;
209 };
210 
211 template <typename CollectionT, typename ValueT>
212 bool JsonGenerator::addStringArray(const std::string& key, const CollectionT& collection) {
213  auto keyLength = static_cast<rapidjson::SizeType>(key.length());
214  if (!checkWriter() || !m_writer.Key(key.c_str(), keyLength)) {
215  return false;
216  }
217  m_writer.StartArray();
218  for (const auto& value : collection) {
219  static_assert(std::is_same<ValueT, std::string>::value, "We only support string collection.");
220  m_writer.String(value);
221  }
222  m_writer.EndArray();
223  return true;
224 }
225 
226 template <typename CollectionT, typename ValueT>
227 bool JsonGenerator::addMembersArray(const std::string& key, const CollectionT& collection) {
228  auto keyLength = static_cast<rapidjson::SizeType>(key.length());
229  if (!checkWriter() || !m_writer.Key(key.c_str(), keyLength)) {
230  return false;
231  }
232  m_writer.StartArray();
233  for (const auto& value : collection) {
234  static_assert(std::is_same<ValueT, std::string>::value, "We only support string collection.");
235  m_writer.RawValue(value.c_str(), value.length(), rapidjson::kStringType);
236  }
237  m_writer.EndArray();
238  return true;
239 }
240 
241 template <typename CollectionArrayT, typename CollectionValueT, typename ValueT>
242 bool JsonGenerator::addCollectionOfStringArray(const std::string& key, const CollectionArrayT& collection) {
243  auto keyLength = static_cast<rapidjson::SizeType>(key.length());
244  if (!checkWriter() || !m_writer.Key(key.c_str(), keyLength)) {
245  return false;
246  }
247  m_writer.StartArray();
248  for (const auto& stringArray : collection) {
249  m_writer.StartArray();
250  for (const auto& value : stringArray) {
251  static_assert(std::is_same<ValueT, std::string>::value, "We only support string collection.");
252  m_writer.String(value);
253  }
254  m_writer.EndArray();
255  }
256  m_writer.EndArray();
257  return true;
258 }
259 
260 } // namespace json
261 } // namespace utils
262 } // namespace avsCommon
263 } // namespace alexaClientSDK
264 
265 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONGENERATOR_H_
bool addStringArray(const std::string &key, const CollectionT &collection)
Definition: JSONGenerator.h:212
::std::string string
Definition: gtest-port.h:1097
bool addCollectionOfStringArray(const std::string &key, const CollectionArrayT &collection)
Definition: JSONGenerator.h:242
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool addMember(const std::string &key, const char *value)
bool addRawJsonMember(const std::string &key, const std::string &json, bool validate=true)
bool addMembersArray(const std::string &key, const CollectionT &collection)
Definition: JSONGenerator.h:227
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58

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