forked from jonaskgandersson/jWrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
257 lines (220 loc) · 6.84 KB
/
main.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// main.c
//
// Test harness command-line for jWrite
//
// TonyWilk
// 17mar2015
//
#define _CRT_SECURE_NO_WARNINGS // stop complaining about deprecated functions
#include <stdio.h>
#include "jWrite.h"
//-------------------------------------------------
// Command-line interface
// usage:
// jWrite prints example tests
//
void jWriteTest();
int main(int argc, char * argv[])
{
jWriteTest();
return 0;
}
//------------------------------------------------------------
#ifdef JW_GLOBAL_CONTROL_STRUCT
// Examples of jWrite
// - using global control structure
//
void jWriteTest()
{
char buffer[1024];
unsigned int buflen= 1024;
int err;
printf("A JSON object example:\n\n" );
jwOpen( buffer, buflen, JW_OBJECT, JW_PRETTY ); // start root object
jwObj_string( "key", "value" ); // add object key:value pairs
jwObj_int( "int", 1 );
jwObj_double( "double", 1.234 );
jwObj_null( "nullThing" );
jwObj_bool( "bool", 1 );
jwObj_array( "EmptyArray" );
// empty array
jwEnd();
jwObj_array( "anArray" ); // array with elements
jwArr_string("array one" );
jwArr_int( -2 );
jwArr_double( 1234.567 );
jwArr_null();
jwArr_bool( 0 );
jwArr_object(); // object in array
jwObj_string( "obj3_one", "one");
jwObj_string( "obj3_two", "two");
jwEnd();
jwArr_array(); // array in array
jwArr_int( 0 );
jwArr_int( 1 );
jwArr_int( 2 );
jwEnd();
jwEnd(); // end of "anArray" , back to root object
jwObj_object( "EmptyObject" );
jwEnd();
jwObj_object( "anObject" ); // object in the root object
jwObj_string("msg","object in object");
jwObj_string("msg2","object in object 2nd entry");
jwEnd();
jwObj_string( "ObjEntry", "This is the last one" );
err= jwClose(); // close and get error code
printf( buffer );
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos() );
printf("\n\nA JSON array example:\n\n" );
jwOpen( buffer, buflen, JW_ARRAY, JW_PRETTY );
jwArr_string( "String value" );
jwArr_int( 1234 );
jwArr_double( 567.89012 );
jwArr_bool( 1 );
jwArr_null();
jwArr_object();
// empty object
jwEnd();
jwArr_object();
jwObj_string( "key", "value" );
jwObj_string( "key2", "value2" );
jwEnd();
jwArr_array(); // array in array
jwArr_string("Array in array");
jwArr_string("the end");
jwEnd();
jwArr_string("Empty array next...");
jwArr_array();
// empty array
jwEnd();
err= jwClose();
printf( buffer );
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos() );
printf("\n\nExample error:\n\n" );
// this is a copy of the above array example with an error introduced...
//
jwOpen( buffer, buflen, JW_ARRAY, JW_PRETTY ); // 1
jwArr_string( "String value" ); // 2
jwArr_int( 1234 ); // 3
jwArr_double( 567.89012 ); // 4
jwArr_bool( 1 ); // 5
jwArr_null(); // 6
jwArr_object(); // 7
// empty object
//jwEnd();
jwArr_object(); // 8 <-- this is where the error is
jwObj_string( "key", "value" ); // 9
jwObj_string( "key2", "value2" ); // 10
jwEnd(); // 11
jwArr_array(); // array in array // 12
jwArr_string("Array in array"); // 13
jwArr_string("the end"); // 14
jwEnd(); // 15
jwArr_string("Empty array next..."); // 16
jwArr_array(); // 17
// empty array
jwEnd(); // 18
err= jwClose();
printf( buffer );
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos() );
return;
}
#else /* JW_GLOBAL_CONTROL_STRUCT */
// Same examples using user-defined control structure
// - to compile in this mode, comment out the JW_GLOBAL_CONTROL_STRUCT definition
//
void jWriteTest()
{
char buffer[1024];
unsigned int buflen= 1024;
int err;
struct jWriteControl jwc;
printf("jWrite - a JSON object example:\n\n" );
//
// Example JSON object
//
jwOpen( &jwc, buffer, buflen, JW_OBJECT, 1 );
jwObj_string( &jwc, "key", "value" );
jwObj_int( &jwc, "int", 1 );
jwObj_double( &jwc, "double", 1.234 );
jwObj_null( &jwc, "nullThing" );
jwObj_bool( &jwc, "bool", 1 );
jwObj_array( &jwc, "EmptyArray" ); // empty array
jwEnd( &jwc );
jwObj_array( &jwc, "anArray" );
jwArr_string(&jwc, "array one" );
jwArr_int( &jwc, 2 );
jwArr_double( &jwc, 1234.567 );
jwArr_null(&jwc);
jwArr_bool( &jwc, 0 );
jwArr_object( &jwc );
jwObj_string( &jwc, "obj3_one", "one");
jwObj_string( &jwc, "obj3_two", "two");
jwEnd(&jwc);
jwArr_array(&jwc);
jwArr_int( &jwc, 0 );
jwArr_int( &jwc, 1 );
jwArr_int( &jwc, 2 );
jwEnd(&jwc );
jwEnd(&jwc);
jwObj_object( &jwc, "EmptyObject" );
jwEnd(&jwc);
jwObj_object( &jwc, "anObject" );
jwObj_string(&jwc, "msg","object in object");
jwObj_string(&jwc, "msg2","object in object 2nd entry");
jwEnd( &jwc );
jwObj_string( &jwc, "ObjEntry", "This is the last one" );
err= jwClose(&jwc);
printf( buffer );
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos(&jwc) );
printf("\n\nA JSON array example:\n\n" );
jwOpen( &jwc, buffer, buflen, JW_ARRAY, 1 );
jwArr_string( &jwc, "String value" );
jwArr_int( &jwc, 1234 );
jwArr_double( &jwc, 567.89012 );
jwArr_bool( &jwc, 1 );
jwArr_null( &jwc );
jwArr_object( &jwc );
// empty object
jwEnd( &jwc );
jwArr_object( &jwc );
jwObj_string( &jwc, "key", "value" );
jwObj_string( &jwc, "key2", "value2" );
jwEnd( &jwc );
jwArr_array( &jwc ); // array in array
jwArr_string(&jwc, "Array in array");
jwArr_string(&jwc, "the end");
jwEnd( &jwc );
err= jwClose( &jwc );
printf( buffer );
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos( &jwc ) );
printf("\n\nExample error:\n\n" );
jwOpen( &jwc, buffer, buflen, JW_ARRAY, 1 ); // 1
jwArr_string( &jwc, "String value" ); // 2
jwArr_int( &jwc, 1234 ); // 3
jwArr_double( &jwc, 567.89012 ); // 4
jwArr_bool( &jwc, 1 ); // 5
jwArr_null( &jwc ); // 6
jwArr_object( &jwc ); // 7
// empty object
//jwEnd( &jwc );
jwArr_object( &jwc ); // 8 <-- this is where the error is
jwObj_string( &jwc, "key", "value" ); // 9
jwObj_string( &jwc, "key2", "value2" ); // 10
jwEnd( &jwc ); // 11
jwArr_array( &jwc ); // array in array // 12
jwArr_string(&jwc, "Array in array"); // 13
jwArr_string(&jwc, "the end"); // 14
jwEnd( &jwc ); // 15
err= jwClose( &jwc ); // 16
printf( buffer );
if( err != JWRITE_OK )
printf( "Error: %s at function call %d\n", jwErrorToString(err), jwErrorPos( &jwc ) );
return;
}
#endif /* JW_GLOBAL_CONTROL_STRUCT */