AWS IoT Device SDK C++ v2  1.32.6
AWS IoT Device SDK C++ v2
JsonObject.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <aws/crt/StlAllocator.h>
7 #include <aws/crt/Types.h>
8 
9 struct aws_json_value;
10 namespace Aws
11 {
12  namespace Crt
13  {
14 
15  class JsonView;
21  {
22  public:
26  JsonObject();
27 
32  JsonObject(const String &stringToParse);
33 
38  JsonObject(const JsonObject &other);
39 
44  JsonObject(JsonObject &&other) noexcept;
45 
46  ~JsonObject();
47 
51  JsonObject &operator=(const JsonObject &other);
52 
60  JsonObject &operator=(JsonObject &&other) noexcept;
61 
62  bool operator==(const JsonObject &other) const;
63  bool operator!=(const JsonObject &other) const;
64 
68  JsonObject &WithString(const String &key, const String &value);
69  JsonObject &WithString(const char *key, const String &value);
70 
74  JsonObject &AsString(const String &value);
75 
79  JsonObject &WithBool(const String &key, bool value);
80  JsonObject &WithBool(const char *key, bool value);
81 
85  JsonObject &AsBool(bool value);
86 
91  JsonObject &WithInteger(const String &key, int value);
92  JsonObject &WithInteger(const char *key, int value);
93 
98  JsonObject &AsInteger(int value);
99 
104  JsonObject &WithInt64(const String &key, int64_t value);
105  JsonObject &WithInt64(const char *key, int64_t value);
106 
111  JsonObject &AsInt64(int64_t value);
112 
116  JsonObject &WithDouble(const String &key, double value);
117  JsonObject &WithDouble(const char *key, double value);
118 
122  JsonObject &AsDouble(double value);
123 
127  JsonObject &WithArray(const String &key, const Vector<String> &array);
128  JsonObject &WithArray(const char *key, const Vector<String> &array);
129 
134  JsonObject &WithArray(const String &key, const Vector<JsonObject> &array);
135 
140  JsonObject &WithArray(const String &key, Vector<JsonObject> &&array);
141 
145  JsonObject &AsArray(const Vector<JsonObject> &array);
146 
150  JsonObject &AsArray(Vector<JsonObject> &&array);
151 
155  JsonObject &AsNull();
156 
161  JsonObject &WithObject(const String &key, const JsonObject &value);
162  JsonObject &WithObject(const char *key, const JsonObject &value);
163 
167  JsonObject &WithObject(const String &key, JsonObject &&value);
168  JsonObject &WithObject(const char *key, JsonObject &&value);
169 
173  JsonObject &AsObject(const JsonObject &value);
174 
178  JsonObject &AsObject(JsonObject &&value);
179 
183  inline bool WasParseSuccessful() const { return m_value != nullptr; }
184 
188  const String &GetErrorMessage() const;
189 
193  JsonView View() const;
194 
195  private:
199  JsonObject(const aws_json_value *valueToCopy);
200 
205  JsonObject &AsNewValue(aws_json_value *valueToOwn);
206 
212  JsonObject &WithNewKeyValue(const char *key, aws_json_value *valueToOwn);
213 
217  static aws_json_value *NewArray(const Vector<JsonObject> &objectsToCopy);
218 
222  static aws_json_value *NewArray(Vector<JsonObject> &&objectsToMove);
223 
224  aws_json_value *m_value;
225 
226  /* Once upon a time each class instance had an m_errorMessage string member,
227  * and if parse failed the string would explain why.
228  * When we switched json implementations, there was no longer a unique string
229  * explaining why parse failed so we dropped that member from the class.
230  * To avoid breaking the GetErrorMessage() API, which returns the string by REFERENCE,
231  * we now use singletons that are created/destroyed along with library init/cleanup. */
232  static std::unique_ptr<String> s_errorMessage;
233  static std::unique_ptr<String> s_okMessage;
234  static void OnLibraryInit();
235  static void OnLibraryCleanup();
236 
237  friend class JsonView;
238  friend class ApiHandle;
239  };
240 
248  {
249  public:
250  /* constructors */
251  JsonView();
252  JsonView(const JsonObject &val);
253  JsonView &operator=(const JsonObject &val);
254 
258  String GetString(const String &key) const;
262  String GetString(const char *key) const;
263 
268  String AsString() const;
269 
273  bool GetBool(const String &key) const;
277  bool GetBool(const char *key) const;
278 
282  bool AsBool() const;
283 
288  int GetInteger(const String &key) const;
293  int GetInteger(const char *key) const;
294 
298  int AsInteger() const;
299 
304  int64_t GetInt64(const String &key) const;
309  int64_t GetInt64(const char *key) const;
310 
314  int64_t AsInt64() const;
315 
319  double GetDouble(const String &key) const;
323  double GetDouble(const char *key) const;
324 
328  double AsDouble() const;
329 
333  Vector<JsonView> GetArray(const String &key) const;
337  Vector<JsonView> GetArray(const char *key) const;
338 
342  Vector<JsonView> AsArray() const;
343 
347  JsonView GetJsonObject(const String &key) const;
351  JsonView GetJsonObject(const char *key) const;
352 
353  JsonObject GetJsonObjectCopy(const String &key) const;
354 
355  JsonObject GetJsonObjectCopy(const char *key) const;
356 
360  JsonView AsObject() const;
361 
366  Map<String, JsonView> GetAllObjects() const;
367 
372  bool ValueExists(const String &key) const;
377  bool ValueExists(const char *key) const;
378 
382  bool KeyExists(const String &key) const;
386  bool KeyExists(const char *key) const;
387 
391  bool IsObject() const;
392 
396  bool IsBool() const;
397 
401  bool IsString() const;
402 
406  bool IsNumber() const;
407 
411  bool IsIntegerType() const;
412 
416  bool IsFloatingPointType() const;
417 
421  bool IsListType() const;
422 
426  bool IsNull() const;
427 
432  String WriteCompact(bool treatAsObject = true) const;
433 
438  String WriteReadable(bool treatAsObject = true) const;
439 
443  JsonObject Materialize() const;
444 
445  private:
446  JsonView(const aws_json_value *val);
447 
448  String Write(bool treatAsObject, bool readable) const;
449 
450  const aws_json_value *m_value;
451  };
452  } // namespace Crt
453 } // namespace Aws
Aws::Crt::operator==
bool operator==(const basic_string_view< CharT, Traits > &lhs, const basic_string_view< CharT, Traits > &rhs) noexcept
Definition: StringView.h:665
Aws::Crt::ApiHandle
Definition: Api.h:48
StlAllocator.h
Aws::Crt::JsonView
Definition: JsonObject.h:248
Aws::Crt::Map
std::map< K, V, std::less< K >, StlAllocator< std::pair< const K, V > >> Map
Definition: Types.h:47
Aws::Crt::operator!=
bool operator!=(const basic_string_view< CharT, Traits > &lhs, const basic_string_view< CharT, Traits > &rhs) noexcept
Definition: StringView.h:690
Types.h
Aws::Crt::JsonObject::WasParseSuccessful
bool WasParseSuccessful() const
Definition: JsonObject.h:183
Aws::Crt::JsonObject
Definition: JsonObject.h:21
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:53
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45