json_parse: wip
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
diff --git a/json/json.c b/json/json.c
index 0fe9a55..faf90be 100644
--- a/json/json.c
+++ b/json/json.c
@@ -12,12 +12,116 @@
*/
#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_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;
+ }
+ return dest;
+}
+
+s_tag * json_buf_parse_list (const 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);
+ return NULL;
+ }
+ return dest;
+}
+
+s_tag * json_buf_parse_str (const s_buf *buf, s_tag *dest) {
+ s_buf_save save;
+ 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);
+ 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);
- if (buf_parse_tag_number(buf, dest) == 0)
- return false;
+ 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;
+ case '"':
+ json_buf_parse_str(buf, dest);
+ if (buf_peek(buf) != '"') {
+ errputs("json_parse: expected '\"'");
+ return false;
+ }
+ break;
+ case '0' ... '9':
+ json_buf_parse_numbers(buf, dest);
+ break;
+ case 't':
+ case 'f':
+ json_buf_parse_boolean(buf, dest);
+ break;
+ case 'n':
+ dest->type = TAG_NULL;
+ break;
+ default:
+ return false;
+ }
return true;
}
@@ -26,7 +130,7 @@ s_tag *json_parse(const s_buf *buf, s_tag *dest) {
assert(dest);
s_buf_save save;
buf_save_init(buf, &save);
- if (!parse_json(buf, dest)) {
+ if (! parse_json(buf, dest)) {
buf_restore_rpos(buf, &save);
return NULL;
}