AlexaClientSDK  1.26.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  if (!checkWriter() || !m_writer.Key(key.c_str(), key.length())) {
214  return false;
215  }
216  m_writer.StartArray();
217  for (const auto& value : collection) {
218  static_assert(std::is_same<ValueT, std::string>::value, "We only support string collection.");
219  m_writer.String(value);
220  }
221  m_writer.EndArray();
222  return true;
223 }
224 
225 template <typename CollectionT, typename ValueT>
226 bool JsonGenerator::addMembersArray(const std::string& key, const CollectionT& collection) {
227  if (!checkWriter() || !m_writer.Key(key.c_str(), key.length())) {
228  return false;
229  }
230  m_writer.StartArray();
231  for (const auto& value : collection) {
232  static_assert(std::is_same<ValueT, std::string>::value, "We only support string collection.");
233  m_writer.RawValue(value.c_str(), value.length(), rapidjson::kStringType);
234  }
235  m_writer.EndArray();
236  return true;
237 }
238 
239 template <typename CollectionArrayT, typename CollectionValueT, typename ValueT>
240 bool JsonGenerator::addCollectionOfStringArray(const std::string& key, const CollectionArrayT& collection) {
241  if (!checkWriter() || !m_writer.Key(key.c_str(), key.length())) {
242  return false;
243  }
244  m_writer.StartArray();
245  for (const auto& stringArray : collection) {
246  m_writer.StartArray();
247  for (const auto& value : stringArray) {
248  static_assert(std::is_same<ValueT, std::string>::value, "We only support string collection.");
249  m_writer.String(value);
250  }
251  m_writer.EndArray();
252  }
253  m_writer.EndArray();
254  return true;
255 }
256 
257 } // namespace json
258 } // namespace utils
259 } // namespace avsCommon
260 } // namespace alexaClientSDK
261 
262 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_JSON_JSONGENERATOR_H_
bool finishObject()
Definition: JSONGenerator.cpp:46
std::string toString(bool finalize=true)
Definition: JSONGenerator.cpp:120
bool addStringArray(const std::string &key, const CollectionT &collection)
Definition: JSONGenerator.h:212
JsonGenerator()
Definition: JSONGenerator.cpp:38
bool addCollectionOfStringArray(const std::string &key, const CollectionArrayT &collection)
Definition: JSONGenerator.h:240
bool isFinalized()
Definition: JSONGenerator.cpp:135
bool startArray(const std::string &key)
Definition: JSONGenerator.cpp:95
bool startArrayElement()
Definition: JSONGenerator.cpp:103
bool finishArray()
Definition: JSONGenerator.cpp:99
bool startObject(const std::string &key)
Definition: JSONGenerator.cpp:42
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool finishArrayElement()
Definition: JSONGenerator.cpp:106
bool addMember(const std::string &key, const char *value)
Definition: JSONGenerator.cpp:74
bool addRawJsonMember(const std::string &key, const std::string &json, bool validate=true)
Definition: JSONGenerator.cpp:82
bool addMembersArray(const std::string &key, const CollectionT &collection)
Definition: JSONGenerator.h:226
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58

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