MCPcopy Create free account
hub / github.com/antirez/ds4 / json_content

Function json_content

ds4_server.c:419–486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

417 bool in_string = false;
418 bool escape = false;
419 for (const char *s = start; s < end; s++) {
420 unsigned char c = (unsigned char)*s;
421 if (in_string) {
422 buf_putc(&b, (char)c);
423 if (escape) escape = false;
424 else if (c == '\\') escape = true;
425 else if (c == '"') in_string = false;
426 } else if (c == '"') {
427 in_string = true;
428 buf_putc(&b, (char)c);
429 } else if (!isspace(c)) {
430 buf_putc(&b, (char)c);
431 }
432 }
433 return buf_take(&b);
434}
435
436static bool json_content(const char **p, char **out) {
437 json_ws(p);
438 if (**p == '"') return json_string(p, out);
439 if (json_lit(p, "null")) {
440 *out = xstrdup("");
441 return true;
442 }
443 if (**p != '[') {
444 if (!json_skip_value(p)) return false;
445 *out = xstrdup("");
446 return true;
447 }
448
449 (*p)++;
450 buf b = {0};
451 json_ws(p);
452 while (**p && **p != ']') {
453 if (**p == '"') {
454 char *s = NULL;
455 if (!json_string(p, &s)) goto fail;
456 buf_puts(&b, s);
457 free(s);
458 } else if (**p == '{') {
459 (*p)++;
460 json_ws(p);
461 while (**p && **p != '}') {
462 char *key = NULL;
463 if (!json_string(p, &key)) goto fail;
464 json_ws(p);
465 if (**p != ':') {
466 free(key);
467 goto fail;
468 }
469 (*p)++;
470 if (!strcmp(key, "text")) {
471 char *s = NULL;
472 if (!json_string(p, &s)) {
473 free(key);
474 goto fail;
475 }
476 buf_puts(&b, s);

Callers 2

parse_messagesFunction · 0.85

Calls 8

json_wsFunction · 0.85
json_stringFunction · 0.85
json_litFunction · 0.85
json_skip_valueFunction · 0.85
buf_putsFunction · 0.85
buf_takeFunction · 0.85
xstrdupFunction · 0.70
buf_freeFunction · 0.70

Tested by

no test coverage detected