json compiled
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
diff --git a/json/json.c b/json/json.c
index faf90be..7e5e445 100644
--- a/json/json.c
+++ b/json/json.c
@@ -12,127 +12,77 @@
*/
#include <libkc3/kc3.h>
#include "json.h"
-#include "buf.h"
-#include "buf_parse.h"
-#include "err.h"
-#include "tag.h"
-s_tag * json_buf_parse_map (const s_buf *buf, s_tag *dest) {
+/*s_tag * json_buf_parse_null (const s_buf *buf, s_tag *dest) {
+ character c;
+ sw r;
s_buf_save save;
assert(buf);
assert(dest);
- dest->type = TAG_MAP
- dest->data.map = map_new();
buf_save_init(buf, &save);
- if (! buf_parse_map(buf, dest->data.map)) {
- buf_restore_rpos(buf, &save);
- return NULL;
+ if ((r = buf_read_1(buf, "void")) <= 0)
+ goto clean;
+ if (buf_peek_character_utf8(buf, &c) > 0 &&
+ ! ident_character_is_reserved(c)) {
+ r = 0;
+ goto restore;
}
- return dest;
-}
+ goto clean;
+ restore:
+ buf_save_restore_rpos(buf, &save);
+ clean:
+ buf_save_clean(buf, &save);
+ return r;
+}*/
-s_tag * json_buf_parse_list (const s_buf *buf, s_tag *dest) {
+s_tag * json_buf_parse_numbers (s_buf *buf, s_tag *dest)
+{
s_buf_save save;
assert(buf);
assert(dest);
- dest->type = TAG_LIST;
- dest->data.list = list_new();
buf_save_init(buf, &save);
- if (! buf_parse_list(buf, dest->data.list)) {
- buf_restore_rpos(buf, &save);
+ if (! buf_parse_tag_number(buf, dest)) {
+ buf_save_restore_rpos(buf, &save);
return NULL;
}
return dest;
}
-s_tag * json_buf_parse_str (const s_buf *buf, s_tag *dest) {
+s_tag * json_buf_parse (s_buf *buf, s_tag *dest)
+{
+ character c;
+ sw r;
s_buf_save save;
+ s_tag tmp;
assert(buf);
assert(dest);
- dest->type = TAG_STR;
- dest->data.str = str_new();
buf_save_init(buf, &save);
- if (! buf_parse_str(buf, dest->data.str)) {
- buf_restore_rpos(buf, &save);
- return NULL;
- }
- return dest;
-}
-
-s_tag * json_buf_parse_numbers (const s_buf *buf, s_tag *dest) {
- assert(buf);
- assert(dest);
- if (! buf_parse_tag_number(buf, dest)) {
- buf_restore_rpos(buf, &save);
+ if ((r =buf_peek_character_utf8(buf, &c)) < 0)
return NULL;
- }
- return dest;
-}
-
-s_tag * json_buf_parse_boolean (const s_buf *buf, s_tag *dest) {
- assert(buf);
- assert(dest);
- if (buf_peek(buf) == 't') {
- dest->type = TAG_BOOL;
- dest->data.boolean = true;
- buf_skip(buf, 4);
- } else {
- dest->type = TAG_BOOL;
- dest->data.boolean = false;
- buf_skip(buf, 5);
- }
- return dest;
-}
-
-static bool parse_json(const s_buf *buf, s_tag *dest) {
- assert(buf);
- assert(dest);
- switch (buf_peek(buf)) {
- case '{':
- json_buf_parse_map(buf, dest);
- if (buf_peek(buf) != '}') {
- errputs("json_parse: expected '}'");
- return false;
- }
- break;
- case '[':
- json_buf_parse_list(buf, dest);
- if (buf_peek(buf) != ']') {
- errputs("json_parse: expected ']'");
- return false;
- }
- break;
+ switch (c) {
case '"':
- json_buf_parse_str(buf, dest);
- if (buf_peek(buf) != '"') {
- errputs("json_parse: expected '\"'");
- return false;
- }
+ buf_parse_tag_str(buf, &tmp);
break;
- case '0' ... '9':
- json_buf_parse_numbers(buf, dest);
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '-':
+ json_buf_parse_numbers(buf, &tmp);
break;
case 't':
case 'f':
- json_buf_parse_boolean(buf, dest);
- break;
- case 'n':
- dest->type = TAG_NULL;
+ buf_parse_tag_bool(buf, &tmp);
break;
default:
- return false;
- }
- return true;
-}
-
-s_tag *json_parse(const s_buf *buf, s_tag *dest) {
- assert(buf);
- assert(dest);
- s_buf_save save;
- buf_save_init(buf, &save);
- if (! parse_json(buf, dest)) {
- buf_restore_rpos(buf, &save);
- return NULL;
+ return NULL;
}
+ *dest = tmp;
return dest;
}
diff --git a/libkc3/map.h b/libkc3/map.h
index 2b77b7f..a1bc363 100644
--- a/libkc3/map.h
+++ b/libkc3/map.h
@@ -17,7 +17,7 @@
/* Stack allocation compatible functions, call map_clean after use. */
void map_clean (s_map *map);
-s_map * map_init (s_map *map, uw size);
+s_map * map_init (s_map *map, uw count);
s_map * map_init_1 (s_map *map, const char *p);
s_map * map_init_cast (s_map *map, const s_tag *tag);
s_map * map_init_copy (s_map *map, const s_map *src);
diff --git a/test/json/basics.diff b/test/json/basics.diff
new file mode 100644
index 0000000..d9a6cbc
--- /dev/null
+++ b/test/json/basics.diff
@@ -0,0 +1,4 @@
+--- basics.ret.expected 2024-08-13 18:25:48.016470147 +0200
++++ basics.ret 2024-08-14 13:27:51.097516149 +0200
+@@ -0,0 +1 @@
++0
diff --git a/test/json/basics.out b/test/json/basics.out
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.out
diff --git a/test/json/basics.out.expected b/test/json/basics.out.expected
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.out.expected
diff --git a/test/json/basics.ret b/test/json/basics.ret
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/test/json/basics.ret
@@ -0,0 +1 @@
+0
diff --git a/test/json/basics.ret.expected b/test/json/basics.ret.expected
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.ret.expected