ZFFramework
 
Loading...
Searching...
No Matches
ZFJson.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFJson_h_
7#define _ZFI_ZFJson_h_
8
9#include "ZFAlgorithmDef.h"
11
12/*
13 * all types:
14 * Null
15 * Value
16 * Object
17 * Array
18 *
19 * here is a typical json data:
20 "{"
21 " \"k0\" : \"v0\","
22 ""
23 " \"object0\" : {},"
24 " \"object1\" : {"
25 " \"k0\" : \"v0\","
26 " \"k1\" : \"v1\""
27 " },"
28 " \"object2\" : {"
29 " \"k0\" : {\"k0\" : \"v0\"}"
30 " },"
31 ""
32 " \"array0\" : [],"
33 " \"array1\" : ["
34 " {\"k0\" : \"v0\"},"
35 " {\"k0\" : \"v0\"}"
36 " ],"
37 ""
38 " \"k1\" : \"v1\""
39 "}"
40 */
41
42// ============================================================
43// ZFJsonType
59
60// ============================================================
65public:
68
72
75
78
81
84
85public:
90
99
114
115public:
118 : jsonNewLineToken("\n")
119 , jsonIndentToken(" ")
121 , jsonSeparatorToken(",")
123 , jsonKeyTagLeft("\"")
124 , jsonKeyTagRight("\"")
125 , jsonValueTagLeft("\"")
126 , jsonValueTagRight("\"")
127 , jsonObjectTagLeft("{")
128 , jsonObjectTagRight("}")
129 , jsonArrayTagLeft("[")
130 , jsonArrayTagRight("]")
137 {
138 }
139 zfbool operator == (ZF_IN ZFJsonOutputToken const &ref) const;
140 zfbool operator != (ZF_IN ZFJsonOutputToken const &ref) const {
141 return !this->operator == (ref);
142 }
144};
147
148
160
161// ============================================================
162// ZFJson
163zfclassFwd _ZFP_ZFJsonPrivate;
168 // ============================================================
169public:
171 ZFJson(void);
172 ZFJson(ZF_IN const zfnullT &dummy);
173 ZFJson(ZF_IN ZFJsonType type);
174 ZFJson(ZF_IN const ZFJson &ref);
175 virtual ~ZFJson(void);
176
177 ZFJson &operator = (ZF_IN const ZFJson &ref);
178 ZFJson &operator = (ZF_IN const zfnullT &dummy);
179 inline zfbool operator == (ZF_IN const zfnullT &ref) const {return !this->valid();}
180 inline zfbool operator != (ZF_IN const zfnullT &ref) const {return this->valid();}
181 zfbool operator == (ZF_IN const ZFJson &ref) const;
182 inline zfbool operator != (ZF_IN const ZFJson &ref) const {return !(this->operator == (ref));}
184
185 // ============================================================
186public:
190 inline zfstring objectInfo(void) const {
191 zfstring ret;
192 this->objectInfoT(ret);
193 return ret;
194 }
195
196public:
201
202 // ============================================================
203public:
204 void _ZFP_ZFJson_jsonType(ZF_IN ZFJsonType type);
208 ZFJsonType type(void) const;
212 inline zfbool valid(void) const {
213 return (this->type() != v_ZFJsonType::e_Null);
214 }
215
221 ZFJson copy(void) const;
222
230 ZFJson &merge(ZF_IN const ZFJson &other);
231
232 // ============================================================
233 // for value type
234public:
238 ZFJson &value(ZF_IN const zfstring &value);
242 const zfstring &value(void) const;
243
244 // ============================================================
245 // for object type
246public:
250 zfindex attrCount(void) const;
251
255 ZFJson &attr(
256 ZF_IN const zfstring &key
257 , ZF_IN const zfstring &value
258 );
262 ZFJson &attr(
263 ZF_IN const zfstring &key
264 , ZF_IN const ZFJson &item
265 );
269 ZFJson attr(ZF_IN const zfstring &key) const;
273 zfstring attrValue(ZF_IN const zfstring &key) const;
274
278 ZFJson &attrRemove(ZF_IN const zfstring &key);
282 ZFJson &attrRemoveAll(void);
283
284public:
286 zfiter attrIter(void) const;
292 ZFJson attrIterValue(ZF_IN const zfiter &it) const;
293
296 ZF_IN_OUT zfiter &it
297 , ZF_IN const ZFJson &item
298 );
301
302 // ============================================================
303 // for array type
304public:
308 zfindex childCount(void) const;
312 ZFJson childAt(ZF_IN zfindex index) const;
313
317 ZFJson &child(
318 ZF_IN const zfstring &value
319 , ZF_IN_OPT zfindex index = zfindexMax()
320 );
324 ZFJson &child(
325 ZF_IN const ZFJson &item
326 , ZF_IN_OPT zfindex index = zfindexMax()
327 );
335 ZFJson &childRemoveAll(void);
339 zfindex childFind(ZF_IN const ZFJson &item) const;
340
341 // ============================================================
342 // quick access
343public:
345 inline operator zfstring (void) const {return this->toString();}
347 inline operator zfbool (void) const {return this->valid();}
349 inline ZFJson operator [] (ZF_IN const zfchar *key) const {return this->attr(key);}
351 inline ZFJson operator [] (ZF_IN const zfstring &key) const {return this->attr(key);}
353 inline ZFJson operator [] (ZF_IN zfindex const &index) const {return this->childAt(index);}
354
357
358private:
359 _ZFP_ZFJsonPrivate *d;
360 friend zfclassFwd _ZFP_ZFJsonPrivate;
361private:
362 ZFJson(ZF_IN _ZFP_ZFJsonPrivate *ref);
363};
366ZFOUTPUT_TYPE(ZFJson, {v.objectInfoT(s);})
367
372
373// ============================================================
378 , ZFMP_IN(const ZFInput &, input)
379 , ZFMP_OUT_OPT(zfstring *, errorHint, zfnull)
381
382
389 , ZFMP_IN_OUT(const ZFOutput &, output)
390 , ZFMP_IN(const ZFJson &, item)
400 , ZFMP_IN_OUT(zfstring &, ret)
401 , ZFMP_IN(const ZFJson &, item)
402 , ZFMP_IN(const ZFJsonOutputToken &, token)
406 , ZFMP_IN(const ZFJson &, item)
407 , ZFMP_IN(const ZFJsonOutputToken &, token)
409
410// ============================================================
411// escape chars
428 , ZFMP_OUT(zfstring &, dst)
429 , ZFMP_IN(const zfstring &, src)
430 , ZFMP_IN_OPT(zfindex, count, zfindexMax())
436 , ZFMP_OUT(const ZFOutput &, dst)
437 , ZFMP_IN(const zfchar *, src)
438 , ZFMP_IN_OPT(zfindex, count, zfindexMax())
440
445 , ZFMP_OUT(zfstring &, dst)
446 , ZFMP_IN(const zfchar *, src)
447 , ZFMP_IN_OPT(zfindex, count, zfindexMax())
453 , ZFMP_OUT(const ZFOutput &, dst)
454 , ZFMP_IN(const zfchar *, src)
455 , ZFMP_IN_OPT(zfindex, count, zfindexMax())
457
459#endif // #ifndef _ZFI_ZFJson_h_
460
global header for ZFAlgorithm module
#define ZFLIB_ZFAlgorithm
used to export symbols
Definition ZFAlgorithmDef.h:14
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
#define zfnullT
type for zfnull, can be used for function overload
Definition ZFCoreTypeDef_CoreType.h:85
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
#define ZFOUTPUT_TYPE(T_Type, outputAction)
declare your custom type conversion to string, convenient for debug
Definition ZFCoreTypeDef_OtherType.h:221
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZFENUM_SEPARATOR()
see ZFENUM_BEGIN
Definition ZFEnumDeclare.h:158
#define ZFENUM_VALUE_REGISTER(Value)
see ZFENUM_BEGIN
Definition ZFEnumDeclare.h:168
#define ZFENUM_REG(ZFLIB_, EnumName,...)
see ZFENUM_BEGIN
Definition ZFEnumDeclare.h:186
#define ZFENUM_VALUE(Value)
see ZFENUM_BEGIN
Definition ZFEnumDeclare.h:151
#define ZFENUM_BEGIN(ZFLIB_, EnumName)
macros to define reflectable enum type
Definition ZFEnumDeclare.h:147
#define ZFENUM_END(ZFLIB_, EnumName)
see ZFENUM_BEGIN
Definition ZFEnumDeclare.h:172
#define ZFEXPORT_VAR_DECLARE(ZFLIB_, Type, Name)
util to export global variable
Definition ZFExport.h:68
ZFJsonOutputToken & ZFJsonOutputTokenTrim(void)
trim output token for ZFJsonToOutput
Definition ZFJson.h:151
ZFLIB_ZFAlgorithm ZFJson ZFJsonArray()
util to create ZFJson
ZFLIB_ZFAlgorithm void ZFJsonEscapeCharDecode(zfstring &dst, const zfchar *src, zfindex count=(((zfindex) -1)))
see ZFJsonEscapeCharEncode
ZFLIB_ZFAlgorithm ZFJson ZFJsonFromInput(const ZFInput &input, zfstring *errorHint=(zft_zfnull))
parse json, or return an item with null type if fail
ZFLIB_ZFAlgorithm zfbool ZFJsonToOutput(const ZFOutput &output, const ZFJson &item, const ZFJsonOutputToken &token=(ZFJsonOutputTokenDefault()))
convert json to output
zfstring ZFJsonToString(_ZFP_PropTypeW_ZFJson const &v, zfstring *errorHint=zft_zfnull)
util method to convert ZFJson to string
Definition ZFJson.h:364
ZFJsonOutputToken & ZFJsonOutputTokenDetail(void)
detailed output token for ZFJsonToOutput
Definition ZFJson.h:155
ZFLIB_ZFAlgorithm void ZFJsonEscapeCharEncode(zfstring &dst, const zfstring &src, zfindex count=(((zfindex) -1)))
encode json chars
v_ZFJsonType::ZFEnumType ZFJsonType
see v_ZFJsonType
Definition ZFJson.h:57
ZFLIB_ZFAlgorithm ZFJson ZFJsonObject()
util to create ZFJson
ZFJsonOutputToken & ZFJsonOutputTokenDefault(void)
default output token for ZFJsonToOutput
Definition ZFJson.h:159
ZFLIB_ZFAlgorithm zfbool ZFJsonToStringT(zfstring &s, _ZFP_PropTypeW_ZFJson const &v, zfstring *errorHint=zft_zfnull)
util method to convert ZFJson to string
#define ZFMP_IN_OUT(ParamType, paramName)
see ZFMP_IN
Definition ZFMethod.h:117
#define ZFMP_OUT(ParamType, paramName)
see ZFMP_IN
Definition ZFMethod.h:111
#define ZFMP_OUT_OPT(ParamType, paramName, DefaultValue)
see ZFMP_IN
Definition ZFMethod.h:114
#define ZFMP_IN_OPT(ParamType, paramName, DefaultValue)
see ZFMP_IN
Definition ZFMethod.h:108
#define ZFMP_IN(ParamType, paramName)
macro to wrap param types for ZFMETHOD_INLINE_0 series
Definition ZFMethod.h:105
#define ZFMETHOD_FUNC_DECLARE_2(ZFLIB_, ReturnType, MethodName, ZFMP_0, ZFMP_1)
see ZFMETHOD_FUNC_DECLARE_0
Definition ZFMethodFuncDeclare.h:763
#define ZFMETHOD_FUNC_DECLARE_3(ZFLIB_, ReturnType, MethodName, ZFMP_0, ZFMP_1, ZFMP_2)
see ZFMETHOD_FUNC_DECLARE_0
Definition ZFMethodFuncDeclare.h:910
#define ZFMETHOD_FUNC_DECLARE_0(ZFLIB_, ReturnType, MethodName)
declare function type of ZFMethod
Definition ZFMethodFuncDeclare.h:493
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define ZFTYPEID_DECLARE(ZFLIB_, TypeName, Type)
register a type for reflection
Definition ZFTypeIdDeclare.h:137
#define ZFTYPEID_ACCESS_ONLY_REG(ZFLIB_, TypeName, Type,...)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:205
#define ZFTYPEID_ACCESS_ONLY_DECLARE(ZFLIB_, TypeName, Type)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:195
#define ZFTYPEID_REG(ZFLIB_, TypeName, Type,...)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:151
general input callback
Definition ZFIOCallback_input.h:37
JSON item.
Definition ZFJson.h:167
ZFJson & attr(const zfstring &key, const zfstring &value)
set json item for key, valid only for v_ZFJsonType::e_Object
zfindex childCount(void) const
json child count, valid only for v_ZFJsonType::e_Array
ZFJson & attrRemove(const zfstring &key)
remove json item, valid only for v_ZFJsonType::e_Object
ZFJson childAt(zfindex index) const
get json child at index, valid only for v_ZFJsonType::e_Array
zfstring objectInfo(void) const
return object info
Definition ZFJson.h:190
zfbool valid(void) const
true if type is not v_ZFJsonType::e_Null
Definition ZFJson.h:212
ZFJson & attrRemoveAll(void)
remove all json item, valid only for v_ZFJsonType::e_Object
zfstring toString(const ZFJsonOutputToken &token=ZFJsonOutputTokenDefault()) const
return ZFJsonToString
ZFJson & merge(const ZFJson &other)
util to merge from other json
const zfstring & value(void) const
see value
ZFJson copy(void) const
return a copy of this node as well as all of its content
ZFJson & childRemoveAll(void)
remove all json child, valid only for v_ZFJsonType::e_Array
void objectInfoT(zfstring &ret) const
see objectInfo
zfiter attrIter(void) const
see zfiter
void attrIterRemove(zfiter &it)
see zfiter
ZFJson & childRemoveAt(zfindex index)
remove json child at index, valid only for v_ZFJsonType::e_Array
ZFJson attr(const zfstring &key) const
get json item for key, valid only for v_ZFJsonType::e_Object
ZFJson & child(const zfstring &value, zfindex index=((zfindex) -1))
add json child to specified index (ranged in [0, count]), valid only for v_ZFJsonType::e_Array
ZFJson & attr(const zfstring &key, const ZFJson &item)
set json item for key, valid only for v_ZFJsonType::e_Object
zfiter attrIterFind(const zfstring &key) const
see zfiter
zfindex objectRetainCount(void) const
get current retain count
ZFJson attrIterValue(const zfiter &it) const
see zfiter
zfindex attrCount(void) const
json item count, valid only for v_ZFJsonType::e_Object
void attrIterValue(zfiter &it, const ZFJson &item)
see zfiter
zfstring attrValue(const zfstring &key) const
util method to access json value, valid only for v_ZFJsonType::e_Object
ZFJson & value(const zfstring &value)
value of the node, valid only for v_ZFJsonType::e_Value type
zfstring attrIterKey(const zfiter &it) const
see zfiter
zfindex childFind(const ZFJson &item) const
find json child, valid only for v_ZFJsonType::e_Array
ZFJsonType type(void) const
type of this item
ZFJson & child(const ZFJson &item, zfindex index=((zfindex) -1))
add json child to specified index (ranged in [0, count]), valid only for v_ZFJsonType::e_Array
token for output json
Definition ZFJson.h:64
zfstring jsonArrayTagRight
"]" by default
Definition ZFJson.h:83
zfstring jsonValueTagRight
"\"" by default
Definition ZFJson.h:77
zfbool jsonObjectTagInSameLineIfNoContent
whether to put right tag in same line if no content, true by default
Definition ZFJson.h:98
zfstring jsonSeparatorInSameLineToken
", " by default
Definition ZFJson.h:71
zfbool jsonArrayAddNewLineForContent
whether add new line for content of a json array, true by default
Definition ZFJson.h:103
zfstring jsonSeparatorToken
"," by default
Definition ZFJson.h:70
zfstring jsonValueTagLeft
"\"" by default
Definition ZFJson.h:76
zfbool jsonObjectAddNewLineForContent
whether add new line for content of a json object, true by default
Definition ZFJson.h:94
zfstring jsonObjectTagRight
"}" by default
Definition ZFJson.h:80
zfstring jsonArrayTagLeft
"[" by default
Definition ZFJson.h:82
zfstring jsonNewLineToken
"\n" by default
Definition ZFJson.h:66
zfstring jsonKeyTagLeft
"\"" by default
Definition ZFJson.h:73
zfstring jsonValueSeparatorToken
" : " by default
Definition ZFJson.h:69
zfbool jsonArrayContentTagInSameLine
whether trim element tag in same line for a json array, valid only if jsonArrayAddNewLineForContent i...
Definition ZFJson.h:109
zfstring jsonIndentToken
" " by default
Definition ZFJson.h:67
zfstring jsonObjectTagLeft
"{" by default
Definition ZFJson.h:79
zfbool jsonArrayTagInSameLineIfNoContent
whether add new line for content of a json array, true by default
Definition ZFJson.h:113
zfstring jsonKeyTagRight
"\"" by default
Definition ZFJson.h:74
zfstring jsonGlobalLineBeginToken
strings added to head of each new line, empty by default
Definition ZFJson.h:89
general output callback
Definition ZFIOCallback_output.h:37
@ e_Null
Definition ZFJson.h:48
iterator for ZFFramework
Definition zfiter.h:40