fixed a bug in the json format reader. it failed to read an empty array before the fix.

fixed a bug in the json format writer. it quoted a numeric string before the fix. it is enhanced to handle the multi-segment string(loaded by an xli format reader) better
This commit is contained in:
hyung-hwan 2018-01-19 04:47:28 +00:00
parent e5fa90ec02
commit 2280811a8e
2 changed files with 14 additions and 4 deletions

View File

@ -690,6 +690,8 @@ static int __read_array (qse_xli_t* xli)
qse_size_t index = 0;
qse_char_t key[64];
if (MATCH(xli, QSE_XLI_TOK_RBRACK)) return 0; // empty array
while (1)
{
val = __read_value(xli);
@ -734,6 +736,7 @@ static int read_array (qse_xli_t* xli, qse_xli_list_t* lv)
return -1;
}
lv->flags |= QSE_XLI_LIST_ARRAYED;
return 0;
}

View File

@ -146,16 +146,23 @@ static int write_list (qse_xli_t* xli, qse_xli_list_t* list, int depth)
qse_xli_str_t* str = (qse_xli_str_t*)pair->val;
/* ignore the string tag(str->tag) in the json format.
* concatenate multi-segmented string into 1 */
if (write_to_current_stream(xli, QSE_T("\""), 1, 0) <= -1) return -1;
* concatenate multi-segmented string into 1 seperated by a comma */
int quote_needed;
/* if the string value is a non-numeric string or
* it is multi-segmented, quoting is needed */
quote_needed = !(str->flags & QSE_XLI_STR_NSTR) || str->next;
if (quote_needed && write_to_current_stream(xli, QSE_T("\""), 1, 0) <= -1) return -1;
while (1)
{
if (write_to_current_stream(xli, str->ptr, str->len, 1) <= -1) return -1;
if (write_to_current_stream(xli, str->ptr, str->len, quote_needed) <= -1) return -1;
if (!str->next) break;
if (write_to_current_stream(xli, QSE_T(","), 1, 0) <= -1) return -1;
str = str->next;
}
if (write_to_current_stream(xli, QSE_T("\""), 1, 0) <= -1) return -1;
if (quote_needed && write_to_current_stream(xli, QSE_T("\""), 1, 0) <= -1) return -1;
break;
}