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
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <libkc3/kc3.h>
#include "json.h"
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);
buf_save_init(buf, &save);
if ((r = buf_peek_character_utf8(buf, &c)) < 0)
return NULL;
switch (c) {
case '{':
return json_buf_parse_map(buf, dest);
case '"':
return json_buf_parse_str(buf, dest);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
return json_buf_parse_number(buf, dest);
break;
case 't':
case 'f':
return json_buf_parse_bool(buf, dest);
break;
default:
return NULL;
}
*dest = tmp;
return dest;
}
s_tag * json_buf_parse_bool (s_buf *buf, s_tag *dest)
{
sw r;
if ((r = buf_parse_tag_bool(buf, dest)) <= 0)
return NULL;
return dest;
}
s_tag * json_buf_parse_map (s_buf *buf, s_tag *dest)
{
s_list **k;
s_list *keys;
sw r;
s_buf_save save;
s_list **v;
s_list *values;
assert(buf);
assert(dest);
keys = NULL;
k = &keys;
values = NULL;
v = &values;
buf_save_init(buf, &save);
if ((r = buf_read_1(buf, "{")) <= 0)
goto clean;
while (1) {
if ((r = buf_ignore_spaces(buf)) < 0)
goto restore;
if ((r = buf_read_1(buf, "}")) < 0)
goto restore;
if (r > 0)
break;
*k = list_new(NULL);
if (! *k)
goto restore;
if ((r = buf_parse_tag_str(buf, &(*k)->tag)) <= 0)
goto restore;
k = &(*k)->next.data.list;
*v = list_new(NULL);
if (! *v)
goto restore;
if (! json_buf_parse(buf, &(*v)->tag))
goto restore;
v = &(*v)->next.data.list;
}
if (! tag_init_map_from_lists(dest, keys, values))
goto restore;
list_delete_all(keys);
list_delete_all(values);
buf_save_clean(buf, &save);
return dest;
restore:
buf_save_restore_rpos(buf, &save);
clean:
list_delete_all(keys);
list_delete_all(values);
buf_save_clean(buf, &save);
return NULL;
}
s_tag * json_buf_parse_null (s_buf *buf, s_tag *dest)
{
character c;
sw r;
s_buf_save save;
assert(buf);
assert(dest);
buf_save_init(buf, &save);
if ((r = buf_read_1(buf, "null")) <= 0)
goto clean;
if (buf_peek_character_utf8(buf, &c) > 0 &&
! ident_character_is_reserved(c)) {
r = 0;
goto restore;
}
tag_init_void(dest);
buf_save_clean(buf, &save);
return dest;
restore:
buf_save_restore_rpos(buf, &save);
clean:
buf_save_clean(buf, &save);
return NULL;
}
s_tag * json_buf_parse_number (s_buf *buf, s_tag *dest)
{
s_buf_save save;
assert(buf);
assert(dest);
buf_save_init(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 (s_buf *buf, s_tag *dest)
{
sw r;
if ((r = buf_parse_tag_str(buf, dest)) <= 0)
return NULL;
return dest;
}
s_tag * json_from_str (const s_str *src, s_tag *dest)
{
s_buf buf;
buf_init_str_const(&buf, src);
return json_buf_parse(&buf, dest);
}