Skip to content

Commit

Permalink
parser: decoder: json: do not unescape and skip empty spaces (#1278)
Browse files Browse the repository at this point in the history
Due to recent improvements in the JSON -> Msgpack encoder, it's not
longer necessary to run a new 'unescape' routine since JSON strings
are now properly converted.

This patch makes to workaround cases where the stringified JSON map
have leading empty spaces at the beginning, e.g:

  {"log": "   {\"k\": 123}"}

So when decoding 'log' value as JSON it will be converted properly
to a map.

note: issue described on #1278.

Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Jun 6, 2019
1 parent 6ca655d commit b6b376a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/flb_parser_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ static int decode_json(struct flb_parser_dec *dec,
const char *in_buf, size_t in_size,
char **out_buf, size_t *out_size, int *out_type)
{
int len;
int ret;
int root_type;
char *buf;
const char *p;
size_t size;
size_t len;

/* JSON Decoder: content may be escaped */
len = flb_unescape_string(in_buf, in_size, &dec->buffer);
p = in_buf;
while (*p == ' ') p++;

len = in_size - (p - in_buf);

/* It must be a map or array */
if (dec->buffer[0] != '{' && dec->buffer[0] != '[') {
if (p[0] != '{' && p[0] != '[') {
return -1;
}

/* Convert from unescaped JSON to MessagePack */
ret = flb_pack_json(dec->buffer, len, &buf, &size, &root_type);
ret = flb_pack_json(p, len, &buf, &size, &root_type);
if (ret != 0) {
return -1;
}
Expand Down

0 comments on commit b6b376a

Please sign in to comment.