-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexample.c
More file actions
91 lines (77 loc) · 1.83 KB
/
Copy pathexample.c
File metadata and controls
91 lines (77 loc) · 1.83 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "json.h"
static char *read_file(const char *path) {
FILE *file;
long len;
char *buffer;
size_t read_len;
file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "Expected file \"%s\" not found\n", path);
return NULL;
}
if (fseek(file, 0, SEEK_END) != 0) {
fclose(file);
return NULL;
}
len = ftell(file);
if (len < 0) {
fclose(file);
return NULL;
}
if (fseek(file, 0, SEEK_SET) != 0) {
fclose(file);
return NULL;
}
buffer = (char *)malloc((size_t)len + 1u);
if (buffer == NULL) {
fclose(file);
return NULL;
}
read_len = fread(buffer, 1u, (size_t)len, file);
fclose(file);
if (read_len != (size_t)len) {
free(buffer);
return NULL;
}
buffer[len] = '\0';
return buffer;
}
int main(void) {
char *text;
json_root root;
json_error error;
json_status status;
const json_value *status_value;
const char *str;
size_t len;
long a;
int b;
text = read_file("sample/simple.json");
if (text == NULL) {
return 1;
}
status = json_parse(text, &root, &error);
free(text);
if (status != JSON_OK) {
fprintf(stderr, "Parse failed at byte %lu: %s\n",
(unsigned long)error.offset, json_error_to_string(status));
return 1;
}
status_value = json_object_get(&root.value, "status");
if (json_string_get(status_value, &str, &len) == JSON_OK) {
printf("status: %.*s\n", (int)len, str);
}
if (json_path_get_string(&root.value, "status", &str, &len) == JSON_OK) {
printf("path status: %.*s\n", (int)len, str);
}
if (json_path_get_long(&root.value, "data.a", &a) == JSON_OK) {
printf("data.a: %ld\n", a);
}
if (json_path_get_bool(&root.value, "data.b", &b) == JSON_OK) {
printf("data.b: %s\n", b ? "true" : "false");
}
json_free(&root);
return 0;
}