diff options
Diffstat (limited to 'include/argot/json.h')
-rw-r--r-- | include/argot/json.h | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/include/argot/json.h b/include/argot/json.h new file mode 100644 index 0000000..f453ecf --- /dev/null +++ b/include/argot/json.h | |||
@@ -0,0 +1,99 @@ | |||
1 | /* This file is part of Grecs. | ||
2 | Copyright (C) 2012-2016 Sergey Poznyakoff. | ||
3 | |||
4 | Grecs is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | Grecs is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with Grecs. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include <argot.h> | ||
18 | |||
19 | enum json_value_type | ||
20 | { | ||
21 | json_null, | ||
22 | json_bool, | ||
23 | json_number, | ||
24 | json_string, | ||
25 | json_arr, | ||
26 | json_object | ||
27 | }; | ||
28 | |||
29 | struct json_value; | ||
30 | struct json_array { | ||
31 | size_t oc; | ||
32 | struct json_value **ov; | ||
33 | struct argot_list *ol; | ||
34 | }; | ||
35 | |||
36 | struct json_value { | ||
37 | enum json_value_type type; | ||
38 | union { | ||
39 | int b; /* json_bool */ | ||
40 | double n; /* json_number */ | ||
41 | char *s; /* json_string */ | ||
42 | struct json_array *a; /* json_arr */ | ||
43 | struct argot_symtab *o; /* json_object */ | ||
44 | } v; | ||
45 | }; | ||
46 | |||
47 | struct json_pair { | ||
48 | char *k; | ||
49 | struct json_value *v; | ||
50 | }; | ||
51 | |||
52 | extern char const *json_err_diag; | ||
53 | extern struct argot_locus json_err_locus; | ||
54 | extern struct json_value *json_return_obj; | ||
55 | |||
56 | void jsonlex_setup(char const *s, size_t l); | ||
57 | void jsonlex_diag(const char *s); | ||
58 | int json_unescape(char c, char *o); | ||
59 | |||
60 | struct json_value *json_value_create(int type); | ||
61 | struct argot_symtab *json_assoc_create(void); | ||
62 | void json_value_free(struct json_value *obj); | ||
63 | |||
64 | struct json_value *json_parse_string(char const *input, size_t len); | ||
65 | |||
66 | struct json_value *json_value_lookup(struct json_value *obj, | ||
67 | const char *ident); | ||
68 | |||
69 | |||
70 | struct json_format | ||
71 | { | ||
72 | size_t indent; | ||
73 | int precision; | ||
74 | void (*write) (void *, char const *, size_t); | ||
75 | void *data; | ||
76 | }; | ||
77 | |||
78 | void json_format_value(struct json_value *obj, struct json_format *fmt); | ||
79 | |||
80 | struct json_value *json_new_null(void); | ||
81 | struct json_value *json_new_bool(int b); | ||
82 | struct json_value *json_new_number(double n); | ||
83 | struct json_value *json_new_string(char const *str); | ||
84 | |||
85 | struct json_value *json_new_object(void); | ||
86 | int json_object_set(struct json_value *obj, char const *name, | ||
87 | struct json_value *val); | ||
88 | int json_object_get(struct json_value *obj, char const *name, | ||
89 | struct json_value **retval); | ||
90 | |||
91 | struct json_value *json_new_array(void); | ||
92 | size_t json_array_size(struct json_value *j); | ||
93 | void json_array_flatten(struct json_value *j); | ||
94 | int json_array_insert(struct json_value *j, size_t idx, struct json_value *v); | ||
95 | int json_array_append(struct json_value *j, struct json_value *v); | ||
96 | int json_array_set(struct json_value *j, size_t idx, struct json_value *v); | ||
97 | int json_array_get(struct json_value *j, size_t idx, | ||
98 | struct json_value **retval); | ||
99 | |||