Browse Source

fixup! json: Support streaming JSON messages

json-streaming
Christian Decker 6 years ago
parent
commit
ace0136234
No known key found for this signature in database GPG Key ID: 1416D83DC4F0E86D
  1. 25
      common/json.c
  2. 4
      common/json.h

25
common/json.c

@ -148,6 +148,16 @@ const jsmntok_t *json_next(const jsmntok_t *tok)
return t; return t;
} }
const jsmntok_t *json_last_toks(const jsmntok_t *tok)
{
const jsmntok_t *t = tok;
for (size_t i=0; i<tok->size; i++) {
t++;
t = json_last_toks(t);
}
return t;
}
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[], const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
const char *label) const char *label)
{ {
@ -187,13 +197,11 @@ jsmntok_t *json_parse_input(const char *input, int len, bool *valid)
jsmntok_t *toks; jsmntok_t *toks;
int ret; int ret;
/* Zero out so we can count elements correctly even on incomplete reads toks = tal_arr(input, jsmntok_t, 10);
* (when jsmn_parse returns -1). This results in all toks being of type toks[0].type = JSMN_UNDEFINED;
* JSMN_UNDEFINED which we can recognize. */
toks = tal_arrz(input, jsmntok_t, 10);
again:
jsmn_init(&parser); jsmn_init(&parser);
again:
ret = jsmn_parse(&parser, input, len, toks, tal_count(toks) - 1); ret = jsmn_parse(&parser, input, len, toks, tal_count(toks) - 1);
switch (ret) { switch (ret) {
@ -201,7 +209,7 @@ again:
*valid = false; *valid = false;
return tal_free(toks); return tal_free(toks);
case JSMN_ERROR_NOMEM: case JSMN_ERROR_NOMEM:
tal_resizez(&toks, tal_count(toks) * 2); tal_resize(&toks, tal_count(toks) * 2);
goto again; goto again;
} }
@ -216,10 +224,7 @@ again:
* ret=JSMN_ERROR_PART, but due to the previous check we know we read at * ret=JSMN_ERROR_PART, but due to the previous check we know we read at
* least one full element, so count tokens that are part of this root * least one full element, so count tokens that are part of this root
* element. */ * element. */
for (ret=0; ret < tal_count(toks)-1; ret++) { ret = json_last_toks(toks) - toks + 1;
if (toks[ret].type == JSMN_UNDEFINED || toks[ret].start >= toks[0].end)
break;
}
/* Cut to length and return. */ /* Cut to length and return. */
*valid = true; *valid = true;

4
common/json.h

@ -106,5 +106,9 @@ void json_add_hex_talarr(struct json_result *result,
const tal_t *data); const tal_t *data);
void json_add_object(struct json_result *result, ...); void json_add_object(struct json_result *result, ...);
/* Get a pointer to the last child element of this object */
const jsmntok_t *json_last_toks(const jsmntok_t *tok);
const char *json_result_string(const struct json_result *result); const char *json_result_string(const struct json_result *result);
#endif /* LIGHTNING_COMMON_JSON_H */ #endif /* LIGHTNING_COMMON_JSON_H */

Loading…
Cancel
Save