-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCJsonHelper.h
169 lines (139 loc) · 5.24 KB
/
CJsonHelper.h
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
/********************************************************************
created: 2014/10/01
filename: CJsonHelper.h
file base: CJsonHelper
purpose: cJSON library c++ encapsulation;
author: fanxiangdong;
mail: [email protected];
qq: 435337751;
*********************************************************************/
#ifndef __CJSON_HELPER_H__
#define __CJSON_HELPER_H__
#include <string>
#include <map>
using std::string;
using std::map;
// Define integer types with known size: int64_t, uint64_t.
// If this doesn't work then insert compiler-specific definitions here:
#if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1600)
// Compilers supporting C99 or C++0x have stdint.h defining these integer types
#include <stdint.h>
#include <inttypes.h>
#define __STDC_FORMAT_MACROS
#elif defined(_MSC_VER)
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
typedef long long int64_t;
typedef unsigned long long uint64_t;
#endif
struct cJSON;
class CJsonHelper
{
private:
typedef map<string, CJsonHelper*> CJsonHelperObjRefMap;
typedef CJsonHelperObjRefMap::iterator CJsonHelperObjRefMapIter;
typedef map<unsigned int, CJsonHelper*> CJsonHelperArrayRefMap;
typedef CJsonHelperArrayRefMap::iterator CJsonHelperArrayRefMapIter;
private:
bool m_bRoot;
cJSON* m_pJsonData;
char* m_pszErrorMsg;
CJsonHelperObjRefMap m_CJsonHelperObjRefMap;
CJsonHelperArrayRefMap m_CJsonHelperArrayRefMap;
public:
CJsonHelper();
CJsonHelper( const CJsonHelper& refOther );
~CJsonHelper();
CJsonHelper& operator=( const CJsonHelper& refOther );
bool operator == ( const CJsonHelper& refOther ) const;
bool parse( const char* szJson );
void clear();
bool isEmpty() const;
bool isArray() const;
bool isObject() const;
const char* getError() const { return m_pszErrorMsg; };
string toString() const;
string toFormattedString() const;
public:// method of ordinary json;
bool addEmptySubObject( const char* szKey );
bool addEmptySubArray( const char* szKey );
CJsonHelper* operator[]( const char* szKey );
string operator()( const char* szKey ) const;
//get method of json object;
bool get( const char* szKey, CJsonHelper** pCJsonHelperAddr );
bool get( const char* szKey, string& strValue ) const;
bool get( const char* szKey, char* szValue, int nBufLen ) const;
bool get( const char* szKey, bool& bValue ) const;
bool get( const char* szKey, int& nValue ) const;
bool get( const char* szKey, float& fValue ) const;
bool get( const char* szKey, double& dValue ) const;
bool get( const char* szKey, int64_t& llValue ) const;
bool get( const char* szKey, uint64_t& ullValue ) const;
//set method of json object;
bool set( const char* szKey, const CJsonHelper* pCJsonHelper );
bool set( const char* szKey, const char* szValue );
bool set( const char* szKey, bool bValue );
bool set( const char* szKey, int nValue );
bool set( const char* szKey, float fValue );
bool set( const char* szKey, double dValue );
bool set( const char* szKey, int64_t llValue );
bool set( const char* szKey, uint64_t ullValue );
//delete method of json object;
bool del( const char* szKey );
public:// method of json array;
int getArraySize() const;
CJsonHelper* operator[]( int uiWhich );
string operator()( int uiWhich ) const;
//get method of json array;
bool get( int uiWhich, CJsonHelper** pCJsonHelperAddr );
bool get( int uiWhich, string& strValue ) const;
bool get( int uiWhich, char* szValue, int nBufLen ) const;
bool get( int uiWhich, bool& bValue ) const;
bool get( int uiWhich, int& nValue ) const;
bool get( int uiWhich, float& fValue ) const;
bool get( int uiWhich, double& dValue ) const;
bool get( int uiWhich, int64_t& llValue ) const;
bool get( int uiWhich, uint64_t& ullValue ) const;
//set method of json array(set method must have uiWhich item);
bool set( int uiWhich, const CJsonHelper* pCJsonHelper );
bool set( int uiWhich, const char* szValue );
bool set( int uiWhich, bool bValue );
bool set( int uiWhich, int nValue );
bool set( int uiWhich, float fValue );
bool set( int uiWhich, double dValue );
bool set( int uiWhich, int64_t llValue );
bool set( int uiWhich, uint64_t ullValue );
//delete method of json array;
bool del( int uiWhich );
//append to json array end;
bool append( const CJsonHelper* pCJsonHelper );
bool append( const char* szValue );
bool append( bool bValue );
bool append( int nValue );
bool append( float fValue );
bool append( double dValue );
bool append( int64_t llValue );
bool append( uint64_t ullValue );
private:
void allocErrorMsg();
bool isVaild( const char* szStr ) const;
//object method;
bool setCommon( const char* szKey, cJSON* pItem );
bool setCommonNumber( const char* szKey, double num );
bool get( cJSON* pItem, bool& bValue ) const;
bool get( cJSON* pItem, int& nValue ) const;
bool get( cJSON* pItem, float& fValue ) const;
bool get( cJSON* pItem, double& dValue ) const;
bool get( cJSON* pItem, int64_t& llValue ) const;
bool get( cJSON* pItem, uint64_t& ullValue ) const;
//array method;
bool isArrayItem( int uiWhich ) const;
bool setCommon( int uiWhich, cJSON* pItem );
bool setCommonNumber( int uiWhich, double num );
//append to json array;
bool appendCommon( cJSON* pItem );
bool appendCommonNumber( double num );
explicit CJsonHelper(cJSON* pJsonData);
};
#endif