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
|
/*
* This file is part of MXE. See LICENSE.md for licensing information.
*/
/*
* Copyright (c) 2013 Andrew Kelley
*
* This file is part of liblaxjson, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include <laxjson.h>
#include <stdio.h>
static int on_string(struct LaxJsonContext *context,
enum LaxJsonType type, const char *value, int length)
{
(void)context;
(void)length;
char *type_name = (type == LaxJsonTypeProperty) ? "property" : "string";
printf("%s: %s\n", type_name, value);
return 0;
}
static int on_number(struct LaxJsonContext *context, double x)
{
(void)context;
printf("number: %f\n", x);
return 0;
}
static int on_primitive(struct LaxJsonContext *context, enum LaxJsonType type)
{
(void)context;
char *type_name;
if (type == LaxJsonTypeTrue)
type_name = "true";
else if (type == LaxJsonTypeFalse)
type_name = "false";
else
type_name = "null";
printf("primitive: %s\n", type_name);
return 0;
}
static int on_begin(struct LaxJsonContext *context, enum LaxJsonType type)
{
(void)context;
char *type_name = (type == LaxJsonTypeArray) ? "array" : "object";
printf("begin %s\n", type_name);
return 0;
}
static int on_end(struct LaxJsonContext *context, enum LaxJsonType type)
{
(void)context;
char *type_name = (type == LaxJsonTypeArray) ? "array" : "object";
printf("end %s\n", type_name);
return 0;
}
int main() {
char buf[1024];
struct LaxJsonContext *context;
FILE *f;
int amt_read;
context = lax_json_create();
context->userdata = NULL; /* can set this to whatever you want */
context->string = on_string;
context->number = on_number;
context->primitive = on_primitive;
context->begin = on_begin;
context->end = on_end;
f = fopen("file.json", "rb");
while ((amt_read = fread(buf, 1, sizeof(buf), f))) {
lax_json_feed(context, amt_read, buf);
}
lax_json_destroy(context);
return 0;
}
|