-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Json.h
173 lines (134 loc) · 3.55 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
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project MojoJson, which is hosted on GitHub, and licensed under the MIT License.
*
* License: https://github.com/scottcgi/MojoJson/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/MojoJson
*
* Since : 2013-1-26
* Update : 2021-2-6
* Author : scott.cgi
* Version: 1.2.3
*/
#ifndef JSON_H
#define JSON_H
#include <stdbool.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 JsonObject JsonObject;
/**
* For json array that contains a list of json value.
*/
typedef struct JsonArray 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 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 free.
*/
char* (*GetString) (JsonObject* object, const char* key, const char* defaultValue);
/**
* If not found return NULL.
*/
JsonObject* (*GetObject) (JsonObject* object, const char* key);
/**
* If not found 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 free.
*/
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);
/**
* 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