-
Notifications
You must be signed in to change notification settings - Fork 239
/
CJsonObject.hpp
227 lines (215 loc) · 8.31 KB
/
CJsonObject.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
* Project: neb
* @file CJsonObject.hpp
* @brief Json
* @author bwarliao
* @date: 2014-7-16
* @note
* Modify history:
******************************************************************************/
#ifndef CJSONOBJECT_HPP_
#define CJSONOBJECT_HPP_
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include <malloc.h>
#include <limits.h>
#include <math.h>
#include <string>
#include <list>
#if __cplusplus < 201101L
#include <map>
#else
#include <unordered_map>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "cJSON.h"
#ifdef __cplusplus
}
#endif
namespace neb
{
class CJsonObject
{
public: // method of ordinary json object or json array
CJsonObject();
CJsonObject(const std::string& strJson);
CJsonObject(const CJsonObject* pJsonObject);
CJsonObject(const CJsonObject& oJsonObject);
#if __cplusplus >= 201101L
CJsonObject(CJsonObject&& oJsonObject);
#endif
virtual ~CJsonObject();
CJsonObject& operator=(const CJsonObject& oJsonObject);
#if __cplusplus >= 201101L
CJsonObject& operator=(CJsonObject&& oJsonObject);
#endif
bool operator==(const CJsonObject& oJsonObject) const;
bool Parse(const std::string& strJson);
void Clear();
bool IsEmpty() const;
bool IsArray() const;
std::string ToString() const;
std::string ToFormattedString() const;
const std::string& GetErrMsg() const
{
return(m_strErrMsg);
}
public: // method of ordinary json object
bool AddEmptySubObject(const std::string& strKey);
bool AddEmptySubArray(const std::string& strKey);
bool GetKey(std::string& strKey);
void ResetTraversing();
CJsonObject& operator[](const std::string& strKey);
std::string operator()(const std::string& strKey) const;
bool KeyExist(const std::string& strKey) const;
int ValueType(const std::string& strKey) const;
bool Get(const std::string& strKey, CJsonObject& oJsonObject) const;
bool Get(const std::string& strKey, std::string& strValue) const;
bool Get(const std::string& strKey, int32& iValue) const;
bool Get(const std::string& strKey, uint32& uiValue) const;
bool Get(const std::string& strKey, int64& llValue) const;
bool Get(const std::string& strKey, uint64& ullValue) const;
bool Get(const std::string& strKey, bool& bValue) const;
bool Get(const std::string& strKey, float& fValue) const;
bool Get(const std::string& strKey, double& dValue) const;
bool IsNull(const std::string& strKey) const;
bool Add(const std::string& strKey, const CJsonObject& oJsonObject);
#if __cplusplus < 201101L
bool AddWithMove(const std::string& strKey, CJsonObject& oJsonObject);
#else
bool Add(const std::string& strKey, CJsonObject&& oJsonObject);
#endif
bool Add(const std::string& strKey, const std::string& strValue);
bool Add(const std::string& strKey, int32 iValue);
bool Add(const std::string& strKey, uint32 uiValue);
bool Add(const std::string& strKey, int64 llValue);
bool Add(const std::string& strKey, uint64 ullValue);
bool Add(const std::string& strKey, bool bValue, bool bValueAgain);
bool Add(const std::string& strKey, float fValue);
bool Add(const std::string& strKey, double dValue);
bool AddNull(const std::string& strKey); // add null like this: "key":null
bool Delete(const std::string& strKey);
bool Replace(const std::string& strKey, const CJsonObject& oJsonObject);
#if __cplusplus < 201101L
bool ReplaceWithMove(const std::string& strKey, CJsonObject& oJsonObject);
#else
bool Replace(const std::string& strKey, CJsonObject&& oJsonObject);
#endif
bool Replace(const std::string& strKey, const std::string& strValue);
bool Replace(const std::string& strKey, int32 iValue);
bool Replace(const std::string& strKey, uint32 uiValue);
bool Replace(const std::string& strKey, int64 llValue);
bool Replace(const std::string& strKey, uint64 ullValue);
bool Replace(const std::string& strKey, bool bValue, bool bValueAgain);
bool Replace(const std::string& strKey, float fValue);
bool Replace(const std::string& strKey, double dValue);
bool ReplaceWithNull(const std::string& strKey); // replace value with null
#if __cplusplus < 201101L
bool ReplaceAdd(const std::string& strKey, const CJsonObject& oJsonObject);
bool ReplaceAdd(const std::string& strKey, const std::string& strValue);
template <typename T>
bool ReplaceAdd(const std::string& strKey, T value)
{
if (KeyExist(strKey))
{
return(Replace(strKey, value));
}
return(Add(strKey, value));
}
#else
template <typename T>
bool ReplaceAdd(const std::string& strKey, T&& value)
{
if (KeyExist(strKey))
{
return(Replace(strKey, std::forward<T>(value)));
}
return(Add(strKey, std::forward<T>(value)));
}
#endif
public: // method of json array
int GetArraySize() const;
CJsonObject& operator[](unsigned int uiWhich);
std::string operator()(unsigned int uiWhich) const;
int ValueType(int iWhich) const;
bool Get(int iWhich, CJsonObject& oJsonObject) const;
bool Get(int iWhich, std::string& strValue) const;
bool Get(int iWhich, int32& iValue) const;
bool Get(int iWhich, uint32& uiValue) const;
bool Get(int iWhich, int64& llValue) const;
bool Get(int iWhich, uint64& ullValue) const;
bool Get(int iWhich, bool& bValue) const;
bool Get(int iWhich, float& fValue) const;
bool Get(int iWhich, double& dValue) const;
bool IsNull(int iWhich) const;
bool Add(const CJsonObject& oJsonObject);
#if __cplusplus < 201101L
bool AddWithMove(CJsonObject& oJsonObject);
#else
bool Add(CJsonObject&& oJsonObject);
#endif
bool Add(const std::string& strValue);
bool Add(int32 iValue);
bool Add(uint32 uiValue);
bool Add(int64 llValue);
bool Add(uint64 ullValue);
bool Add(int iAnywhere, bool bValue);
bool Add(float fValue);
bool Add(double dValue);
bool AddNull(); // add a null value
bool AddAsFirst(const CJsonObject& oJsonObject);
#if __cplusplus < 201101L
bool AddAsFirstWithMove(CJsonObject& oJsonObject);
#else
bool AddAsFirst(CJsonObject&& oJsonObject);
#endif
bool AddAsFirst(const std::string& strValue);
bool AddAsFirst(int32 iValue);
bool AddAsFirst(uint32 uiValue);
bool AddAsFirst(int64 llValue);
bool AddAsFirst(uint64 ullValue);
bool AddAsFirst(int iAnywhere, bool bValue);
bool AddAsFirst(float fValue);
bool AddAsFirst(double dValue);
bool AddNullAsFirst(); // add a null value
bool Delete(int iWhich);
bool Replace(int iWhich, const CJsonObject& oJsonObject);
#if __cplusplus < 201101L
bool ReplaceWithMove(int iWhich, CJsonObject& oJsonObject);
#else
bool Replace(int iWhich, CJsonObject&& oJsonObject);
#endif
bool Replace(int iWhich, const std::string& strValue);
bool Replace(int iWhich, int32 iValue);
bool Replace(int iWhich, uint32 uiValue);
bool Replace(int iWhich, int64 llValue);
bool Replace(int iWhich, uint64 ullValue);
bool Replace(int iWhich, bool bValue, bool bValueAgain);
bool Replace(int iWhich, float fValue);
bool Replace(int iWhich, double dValue);
bool ReplaceWithNull(int iWhich); // replace with a null value
private:
CJsonObject(cJSON* pJsonData);
private:
cJSON* m_pJsonData;
cJSON* m_pExternJsonDataRef;
cJSON* m_pKeyTravers;
const char* mc_pError;
std::string m_strErrMsg;
#if __cplusplus < 201101L
std::map<unsigned int, CJsonObject*> m_mapJsonArrayRef;
std::map<unsigned int, CJsonObject*>::iterator m_array_iter;
std::map<std::string, CJsonObject*> m_mapJsonObjectRef;
std::map<std::string, CJsonObject*>::iterator m_object_iter;
#else
mutable std::unordered_map<unsigned int, CJsonObject*> m_mapJsonArrayRef;
mutable std::unordered_map<std::string, CJsonObject*>::iterator m_object_iter;
mutable std::unordered_map<std::string, CJsonObject*> m_mapJsonObjectRef;
mutable std::unordered_map<unsigned int, CJsonObject*>::iterator m_array_iter;
#endif
};
}
#endif /* CJSONHELPER_HPP_ */