My Project
 All Classes Functions Variables Pages
JSON.h
1 /*
2  * JSON.h
3  *
4  * Created on: May 23, 2017
5  * Author: kolban
6  */
7 
8 #ifndef COMPONENTS_CPP_UTILS_JSON_H_
9 #define COMPONENTS_CPP_UTILS_JSON_H_
10 #include <cJSON.h>
11 #include <string>
12 
13 // Forward declarations
14 class JsonObject;
15 class JsonArray;
16 
20 class JSON {
21 public:
22  static JsonObject createObject();
23  static JsonArray createArray();
24  static void deleteObject(JsonObject jsonObject);
25  static void deleteArray(JsonArray jsonArray);
26  static JsonObject parseObject(std::string text);
27  static JsonArray parseArray(std::string text);
28 
29 }; // JSON
30 
31 
35 class JsonArray {
36 public:
37  int getInt(int item);
38  JsonObject getObject(int item);
39  std::string getString(int item);
40  bool getBoolean(int item);
41  double getDouble(int item);
42  void addBoolean(bool value);
43  void addDouble(double value);
44  void addInt(int value);
45  void addObject(JsonObject value);
46  void addString(std::string value);
47  std::string toString();
48  std::string toStringUnformatted();
49  std::size_t size();
50 
51 private:
52  JsonArray(cJSON* node);
53  friend class JSON;
54  friend class JsonObject;
58  cJSON* m_node;
59 
60 }; // JsonArray
61 
62 
66 class JsonObject {
67 public:
68  JsonArray getArray(std::string name);
69  bool getBoolean(std::string name);
70  double getDouble(std::string name);
71  int getInt(std::string name);
72  JsonObject getObject(std::string name);
73  std::string getString(std::string name);
74  bool hasItem(std::string name);
75  bool isValid();
76  void setArray(std::string name, JsonArray array);
77  void setBoolean(std::string name, bool value);
78  void setDouble(std::string name, double value);
79  void setInt(std::string name, int value);
80  void setObject(std::string name, JsonObject value);
81  void setString(std::string name, std::string value);
82  std::string toString();
83  std::string toStringUnformatted();
84 
85 private:
86  JsonObject(cJSON* node);
87  friend class JSON;
88  friend class JsonArray;
92  cJSON* m_node;
93 
94 }; // JsonObject
95 
96 
97 #endif /* COMPONENTS_CPP_UTILS_JSON_H_ */
void setDouble(std::string name, double value)
Set the named double property.
Definition: JSON.cpp:327
double getDouble(std::string name)
Get the named double value from the object.
Definition: JSON.cpp:239
A JSON object.
Definition: JSON.h:66
std::string getString(std::string name)
Get the named string value from the object.
Definition: JSON.cpp:274
void setString(std::string name, std::string value)
Set the named string property.
Definition: JSON.cpp:360
double getDouble(int item)
Get the indexed double value from the array.
Definition: JSON.cpp:138
Top level JSON handler.
Definition: JSON.h:20
void setInt(std::string name, int value)
Set the named int property.
Definition: JSON.cpp:338
int getInt(std::string name)
Get the named int value from the object.
Definition: JSON.cpp:251
static void deleteArray(JsonArray jsonArray)
Delete a JSON array.
Definition: JSON.cpp:38
void addObject(JsonObject value)
Add an object value to the array.
Definition: JSON.cpp:108
bool getBoolean(int item)
Get the indexed boolean value from the array.
Definition: JSON.cpp:127
static JsonArray parseArray(std::string text)
Parse a string that contains a JSON array.
Definition: JSON.cpp:57
void setArray(std::string name, JsonArray array)
Set the named array property.
Definition: JSON.cpp:305
JsonObject getObject(std::string name)
Get the named object value from the object.
Definition: JSON.cpp:263
static JsonObject parseObject(std::string text)
Parse a string that contains a JSON object.
Definition: JSON.cpp:67
void addDouble(double value)
Add a double value to the array.
Definition: JSON.cpp:90
std::string toString()
Convert the JSON object to a string.
Definition: JSON.cpp:369
int getInt(int item)
Get the indexed int value from the array.
Definition: JSON.cpp:149
std::size_t size()
Get the number of elements from the array.
Definition: JSON.cpp:205
JsonObject getObject(int item)
Get the indexed object value from the array.
Definition: JSON.cpp:160
void addString(std::string value)
Add a string value to the array.
Definition: JSON.cpp:117
static void deleteObject(JsonObject jsonObject)
Delete a JSON object.
Definition: JSON.cpp:47
static JsonObject createObject()
Create an empty JSON object.
Definition: JSON.cpp:28
std::string toStringUnformatted()
Build an unformatted string representation.
Definition: JSON.cpp:193
std::string toStringUnformatted()
Build an unformatted string representation.
Definition: JSON.cpp:381
void addInt(int value)
Add an int value to the array.
Definition: JSON.cpp:99
void addBoolean(bool value)
Add a boolean value to the array.
Definition: JSON.cpp:81
bool isValid()
Determine if this represents a valid JSON node.
Definition: JSON.cpp:295
bool hasItem(std::string name)
Determine if the object has the specified item.
Definition: JSON.cpp:286
void setObject(std::string name, JsonObject value)
Set the named object property.
Definition: JSON.cpp:349
void setBoolean(std::string name, bool value)
Set the named boolean property.
Definition: JSON.cpp:316
std::string getString(int item)
Get the indexed object value from the array.
Definition: JSON.cpp:171
bool getBoolean(std::string name)
Get the named boolean value from the object.
Definition: JSON.cpp:227
std::string toString()
Convert the JSON array to a string.
Definition: JSON.cpp:181
A JSON array.
Definition: JSON.h:35
static JsonArray createArray()
Create an empty JSON array.
Definition: JSON.cpp:19