-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_example.c
63 lines (47 loc) · 1.89 KB
/
basic_example.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
#include <stdio.h>
#include "./cson/include/cson.h"
#include <stdlib.h>
int main() {
Cson* cson = cson_load("./examples/basic.json");
if (cson == NULL) {
return 1;
}
CsonItem string = cson_get(cson->root, "%s", "string");
CsonItem integer = cson_get(cson->root, "%s", "integer");
CsonItem flutuant = cson_get(cson->root, "%s", "float");
CsonItem null = cson_get(cson->root, "%s", "null");
CsonItem booleanTrue = cson_get(cson->root, "%s", "true");
CsonItem booleanFalse = cson_get(cson->root, "%s", "false");
if (string.return_code != CRC_OK) {
fprintf(stderr, "stringField: %s\n", return_code_as_cstr(string.return_code));
return 1;
}
printf("stringField: %s\n", cson_unwrap_string(string));
if (integer.return_code != CRC_OK) {
fprintf(stderr, "integerField: %s\n", return_code_as_cstr(integer.return_code));
} else {
printf("integerField: %ld\n", cson_unwrap_integer(integer));
}
if (flutuant.return_code != CRC_OK) {
fprintf(stderr, "floatField: %s\n", return_code_as_cstr(flutuant.return_code));
} else {
printf("floatField: %.*Lf\n", flutuant.node->precision, cson_unwrap_float(flutuant));
}
if (null.return_code != CRC_OK) {
fprintf(stderr, "nullField: %s\n", return_code_as_cstr(null.return_code));
} else {
printf("nullField: %s\n", cson_unwrap_string(null));
}
if (booleanTrue.return_code != CRC_OK) {
fprintf(stderr, "trueField: %s\n", return_code_as_cstr(booleanTrue.return_code));
} else {
printf("trueField: %d\n", cson_unwrap_boolean(booleanTrue));
}
if (booleanFalse.return_code != CRC_OK) {
fprintf(stderr, "falseField: %s\n", return_code_as_cstr(booleanFalse.return_code));
} else {
printf("falseField: %d\n", cson_unwrap_boolean(booleanFalse));
}
cson_free(cson);
return 0;
}