From 2280811a8ed39bd0a9047e112ec8b3e2f0ec6927 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 19 Jan 2018 04:47:28 +0000 Subject: [PATCH] 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 --- qse/lib/xli/read-json.c | 3 +++ qse/lib/xli/write-json.c | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/qse/lib/xli/read-json.c b/qse/lib/xli/read-json.c index e89c7735..1883261c 100644 --- a/qse/lib/xli/read-json.c +++ b/qse/lib/xli/read-json.c @@ -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; } diff --git a/qse/lib/xli/write-json.c b/qse/lib/xli/write-json.c index 57075d9c..5b5c221c 100644 --- a/qse/lib/xli/write-json.c +++ b/qse/lib/xli/write-json.c @@ -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; }