-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathJson.h
192 lines (152 loc) · 4.1 KB
/
Json.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2013-1-26
* Update : 2019-1-8
* Author : scott.cgi
*/
#ifndef JSON_H
#define JSON_H
#include "Engine/Toolkit/Utils/ArrayStrMap.h"
#include "Engine/Toolkit/Utils/ArrayList.h"
/**
* Json value type.
*/
typedef enum
{
JsonType_Object,
JsonType_Array,
JsonType_String,
JsonType_Float,
JsonType_Null,
}
JsonType;
/**
* For json object that contains a set of k-v pairs.
*/
typedef struct
{
ArrayStrMap(objectKey, JsonValue*) valueMap[1];
}
JsonObject;
/**
* For json array that contains a list of json value.
*/
typedef struct
{
ArrayList(JsonValue*) valueList[1];
}
JsonArray;
/**
* One json value.
*/
typedef struct
{
JsonType type;
union
{
/**
* For JsonType_String.
*/
char* jsonString;
/**
* For JsonType_Object.
*/
JsonObject* jsonObject;
/**
* For JsonType_Array.
*/
JsonArray* jsonArray;
/**
* For JsonType_Float and int value.
*/
float jsonFloat;
};
}
JsonValue;
/**
* Get different types of values from JsonObject.
*/
struct AJsonObject
{
bool (*GetBool) (JsonObject* object, const char* key, bool defaultValue);
int (*GetInt) (JsonObject* object, const char* key, int defaultValue);
float (*GetFloat) (JsonObject* object, const char* key, float defaultValue);
JsonType (*GetType) (JsonObject* object, const char* key);
/**
* When JsonValue released the string value will be freed.
*/
char* (*GetString) (JsonObject* object, const char* key, const char* defaultValue);
/**
* If not found key then return NULL.
*/
JsonObject* (*GetObject) (JsonObject* object, const char* key);
/**
* If not found key then return NULL.
*/
JsonArray* (*GetArray) (JsonObject* object, const char* key);
/**
* Get JsonObject's key.
*/
const char* (*GetKey) (JsonObject* object, int index);
/**
* Get index of JsonObject in JsonObject map.
*/
JsonObject* (*GetObjectByIndex)(JsonObject* object, int index);
/**
* Get index of JsonObject in JsonArray map.
*/
JsonArray* (*GetArrayByIndex) (JsonObject* object, int index);
};
extern struct AJsonObject AJsonObject[1];
/**
* Get different types of values from JsonArray.
*/
struct AJsonArray
{
bool (*GetBool) (JsonArray* array, int index);
int (*GetInt) (JsonArray* array, int index);
float (*GetFloat) (JsonArray* array, int index);
JsonType (*GetType) (JsonArray* array, int index);
/**
* When JsonValue released the string value will be freed.
*/
char* (*GetString)(JsonArray* array, int index);
JsonObject* (*GetObject)(JsonArray* array, int index);
JsonArray* (*GetArray) (JsonArray* array, int index);
};
extern struct AJsonArray AJsonArray[1];
/**
* Control Json data.
*/
struct AJson
{
/**
* Parse the Json string, return root JsonValue.
*/
JsonValue* (*Parse) (const char* jsonString);
/**
* Parse the Json file from jsonFilePath, return root JsonValue.
*
* jsonFilePath:
* Android: assets
* IOS : NSBundle
*/
JsonValue* (*ParseFile)(const char* jsonFilePath);
/**
* Destroy JsonValue member memory space and free itself,
* if Destroy root JsonValue will free all memory space.
*
* important: after Destroy the jsonValue will be invalidated.
*/
void (*Destroy) (JsonValue* jsonValue);
};
extern struct AJson AJson[1];
#endif