Spaces:
Sleeping
Sleeping
File size: 9,540 Bytes
d4b77ac |
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
// https://github.com/vivkin/gason - pulled January 10, 2016
#include "gason.h"
#include <stdlib.h>
#define JSON_ZONE_SIZE 4096
#define JSON_STACK_SIZE 32
const char *jsonStrError(int err) {
switch (err) {
#define XX(no, str) \
case JSON_##no: \
return str;
JSON_ERRNO_MAP(XX)
#undef XX
default:
return "unknown";
}
}
void *JsonAllocator::allocate(size_t size) {
size = (size + 7) & ~7;
if (head && head->used + size <= JSON_ZONE_SIZE) {
char *p = (char *)head + head->used;
head->used += size;
return p;
}
size_t allocSize = sizeof(Zone) + size;
Zone *zone = (Zone *)malloc(allocSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : allocSize);
if (zone == nullptr)
return nullptr;
zone->used = allocSize;
if (allocSize <= JSON_ZONE_SIZE || head == nullptr) {
zone->next = head;
head = zone;
} else {
zone->next = head->next;
head->next = zone;
}
return (char *)zone + sizeof(Zone);
}
void JsonAllocator::deallocate() {
while (head) {
Zone *next = head->next;
free(head);
head = next;
}
}
static inline bool isspace(char c) {
return c == ' ' || (c >= '\t' && c <= '\r');
}
static inline bool isdelim(char c) {
return c == ',' || c == ':' || c == ']' || c == '}' || isspace(c) || !c;
}
static inline bool isdigit(char c) {
return c >= '0' && c <= '9';
}
static inline bool isxdigit(char c) {
return (c >= '0' && c <= '9') || ((c & ~' ') >= 'A' && (c & ~' ') <= 'F');
}
static inline int char2int(char c) {
if (c <= '9')
return c - '0';
return (c & ~' ') - 'A' + 10;
}
static double string2double(char *s, char **endptr) {
char ch = *s;
if (ch == '-')
++s;
double result = 0;
while (isdigit(*s))
result = (result * 10) + (*s++ - '0');
if (*s == '.') {
++s;
double fraction = 1;
while (isdigit(*s)) {
fraction *= 0.1;
result += (*s++ - '0') * fraction;
}
}
if (*s == 'e' || *s == 'E') {
++s;
double base = 10;
if (*s == '+')
++s;
else if (*s == '-') {
++s;
base = 0.1;
}
unsigned int exponent = 0;
while (isdigit(*s))
exponent = (exponent * 10) + (*s++ - '0');
double power = 1;
for (; exponent; exponent >>= 1, base *= base)
if (exponent & 1)
power *= base;
result *= power;
}
*endptr = s;
return ch == '-' ? -result : result;
}
static inline JsonNode *insertAfter(JsonNode *tail, JsonNode *node) {
if (!tail)
return node->next = node;
node->next = tail->next;
tail->next = node;
return node;
}
static inline JsonValue listToValue(JsonTag tag, JsonNode *tail) {
if (tail) {
auto head = tail->next;
tail->next = nullptr;
return JsonValue(tag, head);
}
return JsonValue(tag, nullptr);
}
int jsonParse(char *s, char **endptr, JsonValue *value, JsonAllocator &allocator) {
JsonNode *tails[JSON_STACK_SIZE];
JsonTag tags[JSON_STACK_SIZE];
char *keys[JSON_STACK_SIZE];
JsonValue o;
int pos = -1;
bool separator = true;
JsonNode *node;
*endptr = s;
while (*s) {
while (isspace(*s)) {
++s;
if (!*s) break;
}
*endptr = s++;
switch (**endptr) {
case '-':
if (!isdigit(*s) && *s != '.') {
*endptr = s;
return JSON_BAD_NUMBER;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
o = JsonValue(string2double(*endptr, &s));
if (!isdelim(*s)) {
*endptr = s;
return JSON_BAD_NUMBER;
}
break;
case '"':
o = JsonValue(JSON_STRING, s);
for (char *it = s; *s; ++it, ++s) {
int c = *it = *s;
if (c == '\\') {
c = *++s;
switch (c) {
case '\\':
case '"':
case '/':
*it = c;
break;
case 'b':
*it = '\b';
break;
case 'f':
*it = '\f';
break;
case 'n':
*it = '\n';
break;
case 'r':
*it = '\r';
break;
case 't':
*it = '\t';
break;
case 'u':
c = 0;
for (int i = 0; i < 4; ++i) {
if (isxdigit(*++s)) {
c = c * 16 + char2int(*s);
} else {
*endptr = s;
return JSON_BAD_STRING;
}
}
if (c < 0x80) {
*it = c;
} else if (c < 0x800) {
*it++ = 0xC0 | (c >> 6);
*it = 0x80 | (c & 0x3F);
} else {
*it++ = 0xE0 | (c >> 12);
*it++ = 0x80 | ((c >> 6) & 0x3F);
*it = 0x80 | (c & 0x3F);
}
break;
default:
*endptr = s;
return JSON_BAD_STRING;
}
} else if ((unsigned int)c < ' ' || c == '\x7F') {
*endptr = s;
return JSON_BAD_STRING;
} else if (c == '"') {
*it = 0;
++s;
break;
}
}
if (!isdelim(*s)) {
*endptr = s;
return JSON_BAD_STRING;
}
break;
case 't':
if (!(s[0] == 'r' && s[1] == 'u' && s[2] == 'e' && isdelim(s[3])))
return JSON_BAD_IDENTIFIER;
o = JsonValue(JSON_TRUE);
s += 3;
break;
case 'f':
if (!(s[0] == 'a' && s[1] == 'l' && s[2] == 's' && s[3] == 'e' && isdelim(s[4])))
return JSON_BAD_IDENTIFIER;
o = JsonValue(JSON_FALSE);
s += 4;
break;
case 'n':
if (!(s[0] == 'u' && s[1] == 'l' && s[2] == 'l' && isdelim(s[3])))
return JSON_BAD_IDENTIFIER;
o = JsonValue(JSON_NULL);
s += 3;
break;
case ']':
if (pos == -1)
return JSON_STACK_UNDERFLOW;
if (tags[pos] != JSON_ARRAY)
return JSON_MISMATCH_BRACKET;
o = listToValue(JSON_ARRAY, tails[pos--]);
break;
case '}':
if (pos == -1)
return JSON_STACK_UNDERFLOW;
if (tags[pos] != JSON_OBJECT)
return JSON_MISMATCH_BRACKET;
if (keys[pos] != nullptr)
return JSON_UNEXPECTED_CHARACTER;
o = listToValue(JSON_OBJECT, tails[pos--]);
break;
case '[':
if (++pos == JSON_STACK_SIZE)
return JSON_STACK_OVERFLOW;
tails[pos] = nullptr;
tags[pos] = JSON_ARRAY;
keys[pos] = nullptr;
separator = true;
continue;
case '{':
if (++pos == JSON_STACK_SIZE)
return JSON_STACK_OVERFLOW;
tails[pos] = nullptr;
tags[pos] = JSON_OBJECT;
keys[pos] = nullptr;
separator = true;
continue;
case ':':
if (separator || keys[pos] == nullptr)
return JSON_UNEXPECTED_CHARACTER;
separator = true;
continue;
case ',':
if (separator || keys[pos] != nullptr)
return JSON_UNEXPECTED_CHARACTER;
separator = true;
continue;
case '\0':
continue;
default:
return JSON_UNEXPECTED_CHARACTER;
}
separator = false;
if (pos == -1) {
*endptr = s;
*value = o;
return JSON_OK;
}
if (tags[pos] == JSON_OBJECT) {
if (!keys[pos]) {
if (o.getTag() != JSON_STRING)
return JSON_UNQUOTED_KEY;
keys[pos] = o.toString();
continue;
}
if ((node = (JsonNode *) allocator.allocate(sizeof(JsonNode))) == nullptr)
return JSON_ALLOCATION_FAILURE;
tails[pos] = insertAfter(tails[pos], node);
tails[pos]->key = keys[pos];
keys[pos] = nullptr;
} else {
if ((node = (JsonNode *) allocator.allocate(sizeof(JsonNode) - sizeof(char *))) == nullptr)
return JSON_ALLOCATION_FAILURE;
tails[pos] = insertAfter(tails[pos], node);
}
tails[pos]->value = o;
}
return JSON_BREAKING_BAD;
}
|