summaryrefslogtreecommitdiff
path: root/xs_json.h
diff options
context:
space:
mode:
Diffstat (limited to 'xs_json.h')
-rw-r--r--xs_json.h32
1 files changed, 17 insertions, 15 deletions
diff --git a/xs_json.h b/xs_json.h
index 3004027..9b700a2 100644
--- a/xs_json.h
+++ b/xs_json.h
@@ -4,7 +4,7 @@
4 4
5#define _XS_JSON_H 5#define _XS_JSON_H
6 6
7xs_str *xs_json_dumps_pp(xs_val *data, int indent); 7xs_str *xs_json_dumps_pp(const xs_val *data, int indent);
8#define xs_json_dumps(data) xs_json_dumps_pp(data, 0) 8#define xs_json_dumps(data) xs_json_dumps_pp(data, 0)
9xs_val *xs_json_loads(const xs_str *json); 9xs_val *xs_json_loads(const xs_str *json);
10 10
@@ -15,7 +15,7 @@ xs_val *xs_json_loads(const xs_str *json);
15 15
16/** JSON dumps **/ 16/** JSON dumps **/
17 17
18d_char *_xs_json_dumps_str(d_char *s, char *data) 18static xs_str *_xs_json_dumps_str(xs_str *s, const char *data)
19/* dumps a string in JSON format */ 19/* dumps a string in JSON format */
20{ 20{
21 unsigned char c; 21 unsigned char c;
@@ -55,7 +55,7 @@ d_char *_xs_json_dumps_str(d_char *s, char *data)
55} 55}
56 56
57 57
58d_char *_xs_json_indent(d_char *s, int level, int indent) 58static xs_str *_xs_json_indent(xs_str *s, int level, int indent)
59/* adds indentation */ 59/* adds indentation */
60{ 60{
61 if (indent) { 61 if (indent) {
@@ -71,11 +71,12 @@ d_char *_xs_json_indent(d_char *s, int level, int indent)
71} 71}
72 72
73 73
74d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent) 74static xs_str *_xs_json_dumps(xs_str *s, const xs_val *s_data, int level, int indent)
75/* dumps partial data as JSON */ 75/* dumps partial data as JSON */
76{ 76{
77 char *k, *v;
78 int c = 0; 77 int c = 0;
78 xs_val *v;
79 xs_val *data = (xs_val *)s_data;
79 80
80 switch (xs_type(data)) { 81 switch (xs_type(data)) {
81 case XSTYPE_NULL: 82 case XSTYPE_NULL:
@@ -115,6 +116,7 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent)
115 case XSTYPE_DICT: 116 case XSTYPE_DICT:
116 s = xs_str_cat(s, "{"); 117 s = xs_str_cat(s, "{");
117 118
119 xs_str *k;
118 while (xs_dict_iter(&data, &k, &v)) { 120 while (xs_dict_iter(&data, &k, &v)) {
119 if (c != 0) 121 if (c != 0)
120 s = xs_str_cat(s, ","); 122 s = xs_str_cat(s, ",");
@@ -148,7 +150,7 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent)
148} 150}
149 151
150 152
151xs_str *xs_json_dumps_pp(xs_val *data, int indent) 153xs_str *xs_json_dumps_pp(const xs_val *data, int indent)
152/* dumps a piece of data as JSON */ 154/* dumps a piece of data as JSON */
153{ 155{
154 xstype t = xs_type(data); 156 xstype t = xs_type(data);
@@ -188,11 +190,11 @@ typedef enum {
188} js_type; 190} js_type;
189 191
190 192
191d_char *_xs_json_loads_lexer(const char **json, js_type *t) 193static xs_val *_xs_json_loads_lexer(const char **json, js_type *t)
192{ 194{
193 char c; 195 char c;
194 const char *s = *json; 196 const char *s = *json;
195 d_char *v = NULL; 197 xs_val *v = NULL;
196 198
197 /* skip blanks */ 199 /* skip blanks */
198 while (*s == L' ' || *s == L'\t' || *s == L'\n' || *s == L'\r') 200 while (*s == L' ' || *s == L'\t' || *s == L'\n' || *s == L'\r')
@@ -324,10 +326,10 @@ d_char *_xs_json_loads_lexer(const char **json, js_type *t)
324} 326}
325 327
326 328
327d_char *_xs_json_loads_array(const char **json, js_type *t); 329static xs_list *_xs_json_loads_array(const char **json, js_type *t);
328d_char *_xs_json_loads_object(const char **json, js_type *t); 330static xs_dict *_xs_json_loads_object(const char **json, js_type *t);
329 331
330d_char *_xs_json_loads_value(const char **json, js_type *t, d_char *v) 332static xs_val *_xs_json_loads_value(const char **json, js_type *t, xs_val *v)
331/* parses a JSON value */ 333/* parses a JSON value */
332{ 334{
333 if (*t == JS_OBRACK) 335 if (*t == JS_OBRACK)
@@ -345,12 +347,12 @@ d_char *_xs_json_loads_value(const char **json, js_type *t, d_char *v)
345} 347}
346 348
347 349
348d_char *_xs_json_loads_array(const char **json, js_type *t) 350static xs_list *_xs_json_loads_array(const char **json, js_type *t)
349/* parses a JSON array */ 351/* parses a JSON array */
350{ 352{
351 const char *s = *json; 353 const char *s = *json;
352 xs *v; 354 xs *v;
353 d_char *l; 355 xs_list *l;
354 js_type tt; 356 js_type tt;
355 357
356 l = xs_list_new(); 358 l = xs_list_new();
@@ -401,12 +403,12 @@ d_char *_xs_json_loads_array(const char **json, js_type *t)
401} 403}
402 404
403 405
404d_char *_xs_json_loads_object(const char **json, js_type *t) 406static xs_dict *_xs_json_loads_object(const char **json, js_type *t)
405/* parses a JSON object */ 407/* parses a JSON object */
406{ 408{
407 const char *s = *json; 409 const char *s = *json;
408 xs *k1; 410 xs *k1;
409 d_char *d; 411 xs_dict *d;
410 js_type tt; 412 js_type tt;
411 413
412 d = xs_dict_new(); 414 d = xs_dict_new();