aws-crt-cpp
JsonObject.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 #include <aws/crt/StlAllocator.h>
17 #include <aws/crt/Types.h>
18 
19 namespace Aws
20 {
21  struct cJSON;
22 
23  namespace Crt
24  {
25  class JsonView;
31  {
32  public:
36  JsonObject();
37 
41  JsonObject(const String &value);
42 
47  JsonObject(const JsonObject &value);
48 
53  JsonObject(JsonObject &&value) noexcept;
54 
55  ~JsonObject();
56 
60  JsonObject &operator=(const JsonObject &other);
61 
69  JsonObject &operator=(JsonObject &&other) noexcept;
70 
71  bool operator==(const JsonObject &other) const;
72  bool operator!=(const JsonObject &other) const;
73 
77  JsonObject &WithString(const String &key, const String &value);
78  JsonObject &WithString(const char *key, const String &value);
79 
83  JsonObject &AsString(const String &value);
84 
88  JsonObject &WithBool(const String &key, bool value);
89  JsonObject &WithBool(const char *key, bool value);
90 
94  JsonObject &AsBool(bool value);
95 
99  JsonObject &WithInteger(const String &key, int value);
100  JsonObject &WithInteger(const char *key, int value);
101 
105  JsonObject &AsInteger(int value);
106 
110  JsonObject &WithInt64(const String &key, int64_t value);
111  JsonObject &WithInt64(const char *key, int64_t value);
112 
116  JsonObject &AsInt64(int64_t value);
117 
121  JsonObject &WithDouble(const String &key, double value);
122  JsonObject &WithDouble(const char *key, double value);
123 
127  JsonObject &AsDouble(double value);
128 
132  JsonObject &WithArray(const String &key, const Vector<String> &array);
133  JsonObject &WithArray(const char *key, const Vector<String> &array);
134 
139  JsonObject &WithArray(const String &key, const Vector<JsonObject> &array);
140 
145  JsonObject &WithArray(const String &key, Vector<JsonObject> &&array);
146 
150  JsonObject &AsArray(const Vector<JsonObject> &array);
151 
155  JsonObject &AsArray(Vector<JsonObject> &&array);
156 
160  JsonObject &AsNull();
161 
166  JsonObject &WithObject(const String &key, const JsonObject &value);
167  JsonObject &WithObject(const char *key, const JsonObject &value);
168 
172  JsonObject &WithObject(const String &key, JsonObject &&value);
173  JsonObject &WithObject(const char *key, JsonObject &&value);
174 
178  JsonObject &AsObject(const JsonObject &value);
179 
183  JsonObject &AsObject(JsonObject &&value);
184 
189  inline bool WasParseSuccessful() const { return m_wasParseSuccessful; }
190 
194  inline const String &GetErrorMessage() const { return m_errorMessage; }
195 
199  JsonView View() const;
200 
201  private:
202  void Destroy();
203  JsonObject(cJSON *value);
204  cJSON *m_value;
205  bool m_wasParseSuccessful;
206  String m_errorMessage;
207  friend class JsonView;
208  };
209 
217  {
218  public:
219  /* constructors */
220  JsonView();
221  JsonView(const JsonObject &val);
222  JsonView &operator=(const JsonObject &val);
223 
227  String GetString(const String &key) const;
231  String GetString(const char *key) const;
232 
237  String AsString() const;
238 
242  bool GetBool(const String &key) const;
246  bool GetBool(const char *key) const;
247 
251  bool AsBool() const;
252 
257  int GetInteger(const String &key) const;
262  int GetInteger(const char *key) const;
263 
267  int AsInteger() const;
268 
273  int64_t GetInt64(const String &key) const;
278  int64_t GetInt64(const char *key) const;
279 
283  int64_t AsInt64() const;
284 
288  double GetDouble(const String &key) const;
292  double GetDouble(const char *key) const;
293 
297  double AsDouble() const;
298 
302  Vector<JsonView> GetArray(const String &key) const;
306  Vector<JsonView> GetArray(const char *key) const;
307 
311  Vector<JsonView> AsArray() const;
312 
316  JsonView GetJsonObject(const String &key) const;
320  JsonView GetJsonObject(const char *key) const;
321 
322  JsonObject GetJsonObjectCopy(const String &key) const;
323 
324  JsonObject GetJsonObjectCopy(const char *key) const;
325 
329  JsonView AsObject() const;
330 
335  Map<String, JsonView> GetAllObjects() const;
336 
341  bool ValueExists(const String &key) const;
346  bool ValueExists(const char *key) const;
347 
351  bool KeyExists(const String &key) const;
355  bool KeyExists(const char *key) const;
356 
360  bool IsObject() const;
361 
365  bool IsBool() const;
366 
370  bool IsString() const;
371 
376  bool IsIntegerType() const;
377 
381  bool IsFloatingPointType() const;
382 
386  bool IsListType() const;
387 
391  bool IsNull() const;
392 
397  String WriteCompact(bool treatAsObject = true) const;
398 
403  String WriteReadable(bool treatAsObject = true) const;
404 
408  JsonObject Materialize() const;
409 
410  private:
411  JsonView(cJSON *val);
412  JsonView &operator=(cJSON *val);
413  cJSON *m_value;
414  };
415  } // namespace Crt
416 } // namespace Aws
Aws::cJSON
Definition: cJSON.h:71
Aws::Crt::Vector
std::vector< T, StlAllocator< T > > Vector
Definition: Types.h:66
Aws::Crt::JsonObject::WasParseSuccessful
bool WasParseSuccessful() const
Definition: JsonObject.h:189
Types.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:34
Aws::cJSON
struct Aws::cJSON cJSON
Aws::Crt::JsonObject::GetErrorMessage
const String & GetErrorMessage() const
Definition: JsonObject.h:194
Aws::Crt::JsonObject
Definition: JsonObject.h:31
Aws
Definition: Api.h:25
Aws::Crt::JsonView
Definition: JsonObject.h:217
StlAllocator.h
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:58
Aws::Crt::Map
std::map< K, V, std::less< K >, StlAllocator< std::pair< const K, V > >> Map
Definition: Types.h:60