File: test-json.c

package info (click to toggle)
uget 2.2.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,348 kB
  • sloc: ansic: 41,325; sh: 1,444; makefile: 325
file content (887 lines) | stat: -rw-r--r-- 24,574 bytes parent folder | download | duplicates (2)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/*
 *
 *   Copyright (C) 2012-2020 by C.H. Huang
 *   plushuang.tw@gmail.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  ---
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU Lesser General Public License in all respects
 *  for all of the code used other than OpenSSL.  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so.  If you
 *  do not wish to do so, delete this exception statement from your
 *  version.  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <UgJson.h>
#include <UgList.h>
#include <UgArray.h>
#include <UgEntry.h>
#include <UgValue.h>
#include <UgJson-custom.h>

// ----------------------------------------------------------------------------
// WorkedId

typedef struct
{
	int     worked;
	char*   id;
} WorkedId;

UgJsonError worked_id_custom_parse (UgJson* json,
                                    const char* name, const char* value,
                                    void* dest, void* none);
void        worked_id_custom_write (UgJson* json, void* data);

UgEntry	WorkedIdEntry[] =
{
	{"worked", offsetof (WorkedId, worked), UG_ENTRY_BOOL,   NULL, NULL},
	{"id",     offsetof (WorkedId, id),     UG_ENTRY_STRING, NULL, NULL},
	{NULL},
};
// used by test_json_object_custom()
UgEntry	WorkedIdCustomEntry[] = {
	{NULL, 0, UG_ENTRY_CUSTOM,
			(UgJsonParseFunc) worked_id_custom_parse,
			(UgJsonWriteFunc) worked_id_custom_write },
	{NULL}
};

// ------------------------------------
// WorkedId functions

void  worked_id_init (WorkedId* wid)
{
	wid->worked = 0;
	wid->id = NULL;
}

void  worked_id_final (WorkedId* wid)
{
	ug_free (wid->id);
}

WorkedId*  worked_id_new (void)
{
	WorkedId*	wid;

	wid = ug_malloc (sizeof (WorkedId));
	worked_id_init(wid);
	return wid;
}

void  worked_id_free (WorkedId* wid)
{
	worked_id_final (wid);
}

// UgEntry.type   = UG_ENTRY_CUSTOM
// UgEntry.param1 = (UgJsonParseFunc) worked_id_custom_parse
UgJsonError worked_id_custom_parse (UgJson* json,
                                    const char* name, const char* value,
                                    void* dest, void* none)
{
	// WorkedId's type is UG_JSON_OBJECT
	if (json->type != UG_JSON_OBJECT) {
//		if (json->type == UG_JSON_ARRAY)
//			ug_json_push (json, ug_json_parse_unknown, NULL, NULL);
		return UG_JSON_ERROR_TYPE_NOT_MATCH;
	}

	// initialize WorkedId. If you have initialized it, don't initialize again.
//	worked_id_init (dest);

	// push JSON parser, only UG_JSON_OBJECT or UG_JSON_ARRAY need to push parser.
	ug_json_push (json, ug_json_parse_entry, dest, WorkedIdEntry);
	// return error code
	return UG_JSON_ERROR_NONE;
}

// UgEntry.type   = UG_ENTRY_CUSTOM
// UgEntry.param2 = (UgJsonWriteFunc) worked_id_custom_write
void  worked_id_custom_write (UgJson* json, void* data)
{
	ug_json_write_object_head (json);
	ug_json_write_entry (json, data, WorkedIdEntry);
	ug_json_write_object_tail (json);
}

// ----------------------------------------------------------------------------
// WorkedIdArray

typedef UG_ARRAY (WorkedId)    WorkedIdArray;

UgJsonError worked_id_array_parse (UgJson* json,
                                   const char* name, const char* value,
                                   void* array, void* none)
{
	WorkedId* wid;

	// WorkedId's type is UG_JSON_OBJECT
	if (json->type != UG_JSON_OBJECT) {
//		if (json->type == UG_JSON_ARRAY)
//			ug_json_push (json, ug_json_parse_unknown, NULL, NULL);
		return UG_JSON_ERROR_TYPE_NOT_MATCH;
	}

	wid = ug_array_alloc (array, 1);
	worked_id_init (wid);
	// push JSON parser, only UG_JSON_OBJECT or UG_JSON_ARRAY need to push parser.
	ug_json_push (json, ug_json_parse_entry, wid, WorkedIdEntry);
	// return error code
	return UG_JSON_ERROR_NONE;
}

void  worked_id_array_write (UgJson* json, void* array)
{
	WorkedId* cur;
	WorkedId* end;

	cur = ((WorkedIdArray*)array)->at;
	end = ((WorkedIdArray*)array)->at + ((WorkedIdArray*)array)->length;
	for (;  cur < end;  cur++)
		worked_id_custom_write (json, cur);
}

// ----------------------------------------------------------------------------
// SampleJs

typedef struct
{
	char*  name;

	struct Info {
		struct FtpInfo {
			char*   user;
			char*   password;
			int     active_mode;
		} ftp;

		struct HttpInfo {
			char*   user_agent;
			char*   referrer;
		} http;

		UgArrayStr          sarray;
		UgArrayInt          iarray;
		UG_ARRAY(WorkedId)  oarray;
	} info;
} SampleJs;

// SampleJs.Info.FtpInfo
UgEntry	FtpInfoEntry[] =
{
	{"user",       offsetof (struct FtpInfo, user),       UG_ENTRY_STRING, NULL, NULL},
	{"password",   offsetof (struct FtpInfo, password),   UG_ENTRY_STRING, NULL, NULL},
	{"active-mode",offsetof (struct FtpInfo, active_mode),UG_ENTRY_BOOL,   NULL, NULL},
	{NULL},
};

// SampleJs.Info.HttpInfo
UgEntry	HttpInfoEntry[] =
{
	{"user-agent", offsetof (struct HttpInfo, user_agent), UG_ENTRY_STRING, NULL, NULL},
	{"referrer",   offsetof (struct HttpInfo, referrer),   UG_ENTRY_STRING, NULL, NULL},
	{NULL},
};

// SampleJs.Info
UgEntry	InfoEntry[] =
{
	{"ftp",    offsetof (struct Info, ftp),    UG_ENTRY_OBJECT,
			FtpInfoEntry,  (UgInitFunc) NULL},
	{"http",   offsetof (struct Info, http),   UG_ENTRY_OBJECT,
			HttpInfoEntry, (UgInitFunc) NULL},
	{"sarray", offsetof (struct Info, sarray), UG_ENTRY_ARRAY,
			ug_json_parse_array_string, ug_json_write_array_string},
	{"iarray", offsetof (struct Info, iarray), UG_ENTRY_ARRAY,
			ug_json_parse_array_int, ug_json_write_array_int},
	{"oarray", offsetof (struct Info, oarray), UG_ENTRY_ARRAY,
			worked_id_array_parse, worked_id_array_write},
	{NULL},
};

// SampleJs
UgEntry	 SampleJsEntry[] =
{
	{"name", offsetof (SampleJs, name), UG_ENTRY_STRING,
			NULL,                   NULL},
	{"info", offsetof (SampleJs, info), UG_ENTRY_OBJECT,
			InfoEntry, (UgInitFunc) NULL},
	{NULL},
};
// used by sample_js_parse()
UgEntry  SampleJsObjectEntry[] = {
	{NULL, 0, UG_ENTRY_OBJECT, SampleJsEntry, (UgInitFunc) NULL},
	{NULL}
};

// ------------------------------------
// SampleJs functions

SampleJs*  sample_js_new (void)
{
	SampleJs*  samplejs;

	samplejs = calloc (1, sizeof (SampleJs));
	ug_array_init (&samplejs->info.sarray, sizeof (char*), 0);
	ug_array_init (&samplejs->info.iarray, sizeof (int), 0);
	ug_array_init (&samplejs->info.oarray, sizeof (WorkedId), 0);
	return samplejs;
}

void  sample_js_free (SampleJs* samplejs)
{
	ug_array_clear (&samplejs->info.sarray);
	ug_array_clear (&samplejs->info.iarray);
	ug_array_clear (&samplejs->info.oarray);
	free (samplejs);
}

// UgJsonParseFunc
UgJsonError json_debug_parser (UgJson* json, const char* name, const char* value, void* dest, void* none)
{
	printf ("scope : %s\n", (json->scope == UG_JSON_ARRAY) ? "A" : "O");
	if (json->type == UG_JSON_ARRAY || json->type == UG_JSON_OBJECT)
		ug_json_push (json, json_debug_parser, NULL, NULL);

	// UgJson.index[0] : index of name
	// UgJson.index[1] : index of value

	// debug
	if (json->index[0])
		printf ("'%s' : ", json->buf.at + json->index[0]);

	switch (json->type) {
	case UG_JSON_NULL:		// null
		printf ("NULL '%s'", json->buf.at + json->index[1]);
		break;
	case UG_JSON_TRUE:		// true
		printf ("TRUE '%s'", json->buf.at + json->index[1]);
		break;
	case UG_JSON_FALSE:		// false
		printf ("FALSE '%s'", json->buf.at + json->index[1]);
		break;
	case UG_JSON_NUMBER:	// 1234, 0.567
		printf ("NUMBER '%s'", json->buf.at + json->index[1]);
		break;
	case UG_JSON_STRING:	// "string"
		printf ("STRING '%s'", json->buf.at + json->index[1]);
		break;
	case UG_JSON_OBJECT:	// {
		printf ("OBJECT");
		break;
	case UG_JSON_ARRAY:		// [
		printf ("ARRAY");
		break;
	}

	printf ("\n");
	return 0;
}

static const char* sample_js_string =
{
	"{"
	"  \"name\": \"\\u5927\\u4E2Dfile.torrent\","
	"  \"info\":"
	"  {"
	"    \"ftp\":"
	"    {"
	"      \"user\": \"anonymous\","
	"      \"password\": \"guest@unknown\","
	"      \"active-mode\": false"
	"    },"
	"    \"http\":"
	"    {"
	"      \"user-agent\": \"Browser\\/version\","
	"      \"referrer\": \"http:\\/\\/127.0.0.1\\/\" "
	"    },"
	"    \"sarray\":"
	"    ["
	"      \"element1\", \"element2\","
	"      \"element3\", \"element\\n4\" "
	"    ],"
	"    \"iarray\":"
	"    ["
	"      1234, 2012,"
	"      4567, 2011 "
	"    ],"
	"    \"oarray\":"
	"    ["
	"      { \"worked\" : false, \"id\": \"KMan\" },"
	"      { \"worked\" : true , \"id\": \"Kimi\" },"
	"      { \"worked\" : false, \"id\": \"XMan\" },"
	"      { \"worked\" : true , \"id\": \"WMan\" }"
	"    ]"
	"  }"
	"}"
};

void  sample_js_parse (SampleJs* samplejs)
{
	UgJson  json;
	int     code;
	// JSON string for SampleJs
	const char*	json_string = sample_js_string;

	memset (&json, 0, sizeof (json));
	ug_json_init (&json);
	ug_json_begin_parse (&json);
#if 1
	// method 1: use UgEntry to parse start of object
	ug_json_push (&json, ug_json_parse_entry, samplejs, SampleJsObjectEntry);
#elif 1
	// method 2: push ug_json_parse_object() to parse start of object
	ug_json_push (&json, ug_json_parse_entry, samplejs, SampleJsEntry);
	ug_json_push (&json, ug_json_parse_object, NULL, NULL);
#else
	// debug
	ug_json_push (&json, json_debug_parser, NULL, NULL);
	ug_json_push (&json, ug_json_parse_object, NULL, NULL);
#endif
	code = ug_json_parse (&json, json_string, -1);
	printf ("ug_json_parse response %d\n", code);
	code = ug_json_end_parse (&json);
	printf ("ug_json_end_parse response %d\n", code);
	ug_json_final (&json);
}

void  sample_js_reparse (SampleJs* samplejs)
{
	UgJson   json;
	UgValue  value;

	ug_value_init (&value);

	memset (&json, 0, sizeof (json));
	ug_json_init (&json);
	ug_json_begin_parse (&json);
	ug_json_push (&json, ug_json_parse_value, &value, NULL);
	ug_json_parse (&json, sample_js_string, -1);
	ug_json_end_parse (&json);

	// convert UgValue to UgEntry
	ug_json_begin_parse (&json);
	ug_json_push (&json, ug_json_parse_entry, samplejs, SampleJsObjectEntry);
	ug_json_parse_by_value (&json, &value);
	ug_json_end_parse (&json);
	ug_json_final (&json);

	ug_value_clear (&value);
}

void  sample_js_print (SampleJs* samplejs)
{
	UgJson   json;
	UgBuffer buffer;

	printf ("\n"
			"obj.info.ftp.user: %s\n"
			"obj.info.ftp.password: %s\n"
			"obj.info.ftp.active_mode: %d\n"
			"obj.info.http.user_agent: %s\n"
			"obj.info.http.referrer: %s\n",
			samplejs->info.ftp.user,
			samplejs->info.ftp.password,
			samplejs->info.ftp.active_mode,
			samplejs->info.http.user_agent,
			samplejs->info.http.referrer
			);
	printf ("obj.info.sarray[0]: %s\n"
			"obj.info.sarray[1]: %s\n"
			"obj.info.sarray[2]: %s\n"
			"obj.info.sarray[3]: %s\n",
			samplejs->info.sarray.at[0],
			samplejs->info.sarray.at[1],
			samplejs->info.sarray.at[2],
			samplejs->info.sarray.at[3]);
	printf ("obj.info.iarray[0]: %d\n"
			"obj.info.iarray[1]: %d\n"
			"obj.info.iarray[2]: %d\n"
			"obj.info.iarray[3]: %d\n",
			samplejs->info.iarray.at[0],
			samplejs->info.iarray.at[1],
			samplejs->info.iarray.at[2],
			samplejs->info.iarray.at[3]);
	printf ("obj.info.oarray[0].worked: %d\n"
			"obj.info.oarray[0].id: %s\n"
			"obj.info.oarray[1].worked: %d\n"
			"obj.info.oarray[1].id: %s\n"
			"obj.info.oarray[2].worked: %d\n"
			"obj.info.oarray[2].id: %s\n"
			"obj.info.oarray[3].worked: %d\n"
			"obj.info.oarray[3].id: %s\n",
			samplejs->info.oarray.at[0].worked,
			samplejs->info.oarray.at[0].id,
			samplejs->info.oarray.at[1].worked,
			samplejs->info.oarray.at[1].id,
			samplejs->info.oarray.at[2].worked,
			samplejs->info.oarray.at[2].id,
			samplejs->info.oarray.at[3].worked,
			samplejs->info.oarray.at[3].id);

	ug_json_init (&json);
	ug_buffer_init (&buffer, 1024);

	ug_json_begin_write (&json, UG_JSON_FORMAT_INDENT, &buffer);
#if 1
	// method 1: use UgEntry to write start of object
	ug_json_write_entry (&json, samplejs, SampleJsObjectEntry);
#else
	// method 2: call ug_json_write_object_head() to write start of object
	ug_json_write_object_head (&json);
	ug_json_write_entry (&json, samplejs, SampleJsEntry);
	ug_json_write_object_tail (&json);
#endif
	ug_json_end_write (&json);

	ug_buffer_write_char (&buffer, '\0');
	puts ((char*)buffer.beg);

	ug_buffer_clear (&buffer, 1);
	ug_json_final (&json);
}

// UgBufferFunc
static int  buffer_to_file (UgBuffer* buffer)
{
	FILE*   file = buffer->data;

	printf ("write %d bytes to file\n", ug_buffer_length (buffer));
	fwrite (buffer->beg, 1, ug_buffer_length(buffer), file);
	buffer->cur = buffer->beg;
	return 0;
}

void  sample_js_to_file (SampleJs* samplejs, const char* filename)
{
	UgBuffer buffer;
	UgJson   json;
	FILE*    file;

	file = fopen (filename, "w");
	ug_buffer_init (&buffer, 128);
	buffer.more = buffer_to_file;
	buffer.data = file;

	ug_json_init (&json);
	ug_json_begin_write (&json, UG_JSON_FORMAT_INDENT, &buffer);
#if 1
	// method 1: use UgEntry to write start of object
	ug_json_write_entry (&json, samplejs, SampleJsObjectEntry);
#else
	// method 2: call ug_json_write_object_head() to write start of object
	ug_json_write_object_head (&json);
	ug_json_write_entry (&json, samplejs, SampleJsEntry);
	ug_json_write_object_tail (&json);
#endif
	ug_json_end_write (&json);
	ug_json_final (&json);

	fclose (file);
}

// ----------------------------------------------------------------------------
// JSON object sample

void  test_json_object_custom (void)
{
	UgBuffer    buffer;
	WorkedId    worked;
	int         code;
	UgJson      json;
	const char* json_string = {
		"{ \"worked\" : true , \"id\": \"C.H. Huang\" }"
	};

	puts ("\n--- test_json_object_custom:");

	worked_id_init (&worked);
	ug_json_init (&json);
	ug_buffer_init (&buffer, 128);

	// parse
	ug_json_begin_parse (&json);
#if 1
	// method 1: use UgEntry to parse start of object
	ug_json_push (&json, ug_json_parse_entry, &worked, WorkedIdCustomEntry);
#else
	// method 2: push ug_json_parse_object() to parse start of object
	ug_json_push (&json, ug_json_parse_entry, &worked, WorkedIdEntry);
	ug_json_push (&json, ug_json_parse_object, NULL, NULL);
#endif
	code = ug_json_parse (&json, json_string, -1);
	printf ("ug_json_parse response %d\n", code);
	code = ug_json_end_parse (&json);
	printf ("ug_json_end_parse response %d\n", code);

	// write
	ug_json_begin_write (&json, UG_JSON_FORMAT_INDENT, &buffer);
#if 1
	// method 1: use UgEntry to write start of object
	ug_json_write_entry (&json, &worked, WorkedIdCustomEntry);
#else
	// method 2: call ug_json_write_object_head() to write start of object
	ug_json_write_object_head (&json);
	ug_json_write_entry (&json, &worked, WorkedIdEntry);
	ug_json_write_object_tail (&json);
#endif
	ug_json_end_write (&json);

	ug_buffer_write_char (&buffer, '\0');
	puts (buffer.beg);

	ug_buffer_clear (&buffer, 1);
	ug_json_final (&json);
	worked_id_final (&worked);
}

// ----------------------------------------------------------------------------
// JSON array sample

// use UgArray to store JSON number array
void  test_json_int_array (void)
{
	// method 1
	const UgEntry  intArrayEntry[] = {
		{NULL,    0,  UG_ENTRY_ARRAY,
				ug_json_parse_array_int, ug_json_write_array_int},
		{NULL}
	};
	// common
	UgJson      json;
	UgArrayInt  array;
	UgJsonError code;
	char*   teststr = "[12,22,34,56]";
	int     index;

	puts ("\n--- test_json_int_array:");

	memset (&json, 0, sizeof (json));
	ug_array_init (&array, sizeof (int), 0);

	// JSON begin
	ug_json_init (&json);
	// JSON parse
	ug_json_begin_parse (&json);
#if 1
	// method 1: use UgEntry to parse start of array
	ug_json_push (&json, ug_json_parse_entry, &array, (void*)intArrayEntry);
#else
	// method 2: push ug_json_parse_array() to parse start of array
	ug_json_push (&json, ug_json_parse_array_int, &array, NULL);
	ug_json_push (&json, ug_json_parse_array, NULL, NULL);
#endif
	code = ug_json_parse (&json, teststr, -1);
	printf ("ug_json_parse response %d\n", code);
	code = ug_json_end_parse (&json);
	printf ("ug_json_end_parse response %d\n", code);

	// JSON end
	ug_json_final (&json);

	printf ("array.length = '%d'\n"
			"array.allocated = '%d'\n\n",
			array.length, array.allocated);

	for (index = 0;  index < array.length;  index++)
		printf ("array.at[%d] = %d\n", index, array.at[index]);
	ug_array_clear (&array);
}

// use UgList to store JSON string and number array
void  test_json_array_by_list (void)
{
	// method 1
	const UgEntry  stringListEntry[] = {
		{NULL,   0, UG_ENTRY_ARRAY,
				ug_json_parse_list_string, ug_json_write_list_string},
		{NULL}
	};
	const UgEntry  intListEntry[] = {
		{NULL, 0, UG_ENTRY_ARRAY,
				ug_json_parse_list_int, ug_json_write_list_int},
		{NULL}
	};
	// common
	UgJson		json;
	UgList		list;
	UgLink*		link;
	UgJsonError	code;
	UgBuffer    buffer;
	char*	jarraystr = "[\"Str1\",\"Str2\",\"Str3\",\"Str4\"]";
	char*	jarrayint = "[21,32,43,54]";

	puts ("\n--- test_json_array_by_list:");

	memset (&json, 0, sizeof (json));
	ug_list_init (&list);

	// JSON begin
	ug_json_init (&json);
	ug_buffer_init (&buffer, 4096);

	puts ("\n  parse JSON string array:");
	// JSON parse - string array
	ug_json_begin_parse (&json);
#if 1
	// method 1: use UgEntry to parse start of array
	ug_json_push (&json, ug_json_parse_entry, &list, (void*)stringListEntry);
#else
	// method 2: push ug_json_parse_array() to parse start of array
	ug_json_push (&json, ug_json_parse_list_string, &list, NULL);
	ug_json_push (&json, ug_json_parse_array, NULL, NULL);
#endif
	code = ug_json_parse (&json, jarraystr, -1);
	printf ("ug_json_parse response %d\n", code);
	code = ug_json_end_parse (&json);
	printf ("ug_json_end_parse response %d\n", code);

	printf ("list.size = '%d'\n", (int)list.size);
	for (link = list.head;  link;  link = link->next)
		printf ("link->data = \"%s\"\n", (char*)link->data);
	// JSON write - string array
	ug_json_begin_write (&json, 0, &buffer);
#if 1
	// method 1: use UgEntry to write start of array
	ug_json_write_entry (&json, &list, (void*)stringListEntry);
#else
	// method 2: call ug_json_write_array_head() to write start of array
	ug_json_write_array_head (&json);
	ug_json_write_list_string (&json, &list);
	ug_json_write_array_tail (&json);
#endif
	ug_json_end_write (&json);

	puts ("ug_json_write_list_string:");
	ug_buffer_write_char (&buffer, '\0');
	puts (buffer.beg);
	// free strings in list
	ug_list_foreach (&list, (UgForeachFunc) ug_free, NULL);
	ug_list_clear (&list, TRUE);

	puts ("\n  parse JSON int array:");
	// JSON parse - int array
	ug_json_begin_parse (&json);
#if 1
	// method 1: use UgEntry to parse start of array
	ug_json_push (&json, ug_json_parse_entry, &list, (void*)intListEntry);
#else
	// method 2: push ug_json_parse_array() to parse start of array
	ug_json_push (&json, ug_json_parse_list_int, &list, NULL);
	ug_json_push (&json, ug_json_parse_array, NULL, NULL);
#endif
	code = ug_json_parse (&json, jarrayint, -1);
	printf ("ug_json_parse response %d\n", code);
	code = ug_json_end_parse (&json);
	printf ("ug_json_end_parse response %d\n", code);

	printf ("list.size = '%d'\n", (int)list.size);
	for (link = list.head;  link;  link = link->next)
		printf ("link->data = %d\n", (int)(intptr_t)link->data);
	// JSON write - int array
	buffer.cur = buffer.beg;
	ug_json_begin_write (&json, 0, &buffer);
#if 1
	// method 1: use UgEntry to write start of array
	ug_json_write_entry (&json, &list, (void*)intListEntry);
#else
	// method 2: call ug_json_write_array_head() to write start of array
	ug_json_write_array_head (&json);
	ug_json_write_list_int (&json, &list);
	ug_json_write_array_tail (&json);
#endif
	ug_json_end_write (&json);

	puts ("ug_json_write_list_int:");
	ug_buffer_write_char (&buffer, '\0');
	puts (buffer.beg);
	// free list
	ug_list_clear (&list, TRUE);

	ug_buffer_clear (&buffer, 1);
	// JSON end
	ug_json_final (&json);
}

// ----------------------------------------------------------------------------
// JSON value sample

void  test_json_value (void)
{
	UgJson  json;
	int     code;
#if 0
	const char* json_string = "1234";
#elif 0
	const char* json_string = "-100";
#elif 0
	const char* json_string = "\"This is string.\"";
#elif 0
	const char* json_string = "\"This is";  // uncompleted string
#elif 0
	const char* json_string = "true";
#elif 0
	const char* json_string = "tr";         // uncompleted true
#elif 0
	const char* json_string = "{";          // uncompleted object
#else
	// "Sam a tűzoltó - Világcsúcs kísérletek"
	const char* json_string = "\"Sam a t\\u0171zolt\\u00f3 - Vil\\u00e1gcs\\u00facs k\\u00eds\\u00e9rletek\"";
#endif

	puts ("\n--- test_json_value:");

	ug_json_init (&json);
	ug_json_begin_parse (&json);
#if 1
	// debug
	ug_json_push (&json, json_debug_parser, NULL, NULL);
#endif
	code = ug_json_parse (&json, json_string, -1);
	printf ("ug_json_parse response %d\n", code);
	code = ug_json_end_parse (&json);
	printf ("ug_json_end_parse response %d\n", code);
	ug_json_final (&json);
}

// ----------------------------------------------------------------------------
// test JSON writer

void  test_json_writer (void)
{
	UgJson   json;
	UgBuffer buffer;

	puts ("\n--- test_json_writer:");

	ug_json_init (&json);
	ug_buffer_init (&buffer, 4096);

	ug_json_begin_write (&json, UG_JSON_FORMAT_INDENT, &buffer);

	// --- object1 begin
	ug_json_write_object_head (&json);

	// --- array1 begin
	ug_json_write_array_head (&json);
	ug_json_write_array_tail (&json);
	// --- array1 end

	// --- object2 begin
	ug_json_write_object_head (&json);
	ug_json_write_string (&json, "name1");
	// --- array2 begin
	ug_json_write_array_head (&json);
	ug_json_write_int (&json, 1205);
	ug_json_write_int (&json, 2011);
	ug_json_write_int (&json, 1102);
	ug_json_write_array_tail (&json);
	// --- array2 end
	ug_json_write_string (&json, "name2");
//	ug_json_write_string (&json, "value2");
	ug_json_write_string (&json, "Sam a tűzoltó - Világcsúcs kísérletek");
	ug_json_write_object_tail (&json);
	// --- object2 end

	ug_json_write_object_tail (&json);
	// --- object1 end

	ug_json_end_write (&json);

	ug_buffer_write_char (&buffer, '\0');
	puts (buffer.beg);

	ug_buffer_clear (&buffer, 1);
	ug_json_final (&json);
}

// ----------------------------------------------------------------------------
// test UgArray

void  test_array (void)
{
	UgArrayChar array;
	char*   str = "This one is test string.";

	puts ("\n--- test_array:");

	ug_array_init (&array, sizeof (char), 0);
	ug_array_append (&array, str, strlen (str));
	ug_array_end0 (&array);
	printf ("\n"
			"array.at = '%s'\n"
			"array.length = '%d'\n"
			"array.allocated = '%d'\n",
			array.at, array.length, array.allocated);
}

// ----------------------------------------------------------------------------
// test main function

int   main (void)
{
	SampleJs*   samplejs;
//	g_mem_set_vtable (glib_mem_profiler_table);

	// test UgArray
	test_array ();
	// test JSON writer
	test_json_writer ();
	// test simple JSON value
	test_json_value ();
	// use Custom type (WorkedId) to store JSON object
	test_json_object_custom ();
	// use UgArray(int) to store JSON number array
	test_json_int_array ();
	// use UgList to store JSON string and number array
	test_json_array_by_list ();

	puts ("\n--- SampleJs functions:");
	// C struct sample code: parse, print, and save file.
	samplejs = sample_js_new ();
	sample_js_parse (samplejs);
	sample_js_print (samplejs);
	sample_js_to_file (samplejs, "test-SampleJs.json");
	sample_js_free (samplejs);

	samplejs = sample_js_new ();
	sample_js_reparse (samplejs);
	sample_js_print (samplejs);
	sample_js_free (samplejs);

//	g_mem_profile ();

	return 0;
}