summaryrefslogtreecommitdiff
path: root/sql/item_jsonfunc.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/item_jsonfunc.cc')
-rw-r--r--sql/item_jsonfunc.cc33
1 files changed, 30 insertions, 3 deletions
diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc
index 1c80bb46a91..46ea4f59173 100644
--- a/sql/item_jsonfunc.cc
+++ b/sql/item_jsonfunc.cc
@@ -260,6 +260,10 @@ static int json_nice(json_engine_t *je, String *nice_js,
static const char *comma= ", ", *colon= "\": ";
uint comma_len, colon_len;
int first_value= 1;
+ int value_size = 0;
+ int curr_state= -1;
+ int64_t value_len= 0;
+ String curr_str{};
nice_js->length(0);
nice_js->set_charset(je->s.cs);
@@ -286,6 +290,7 @@ static int json_nice(json_engine_t *je, String *nice_js,
do
{
+ curr_state= je->state;
switch (je->state)
{
case JST_KEY:
@@ -333,17 +338,23 @@ handle_value:
if (append_simple(nice_js, je->value_begin,
je->value_end - je->value_begin))
goto error;
-
+
+ curr_str.copy((const char *)je->value_begin,
+ je->value_end - je->value_begin, je->s.cs);
+ value_len= je->value_end - je->value_begin;
first_value= 0;
+ if (value_size != -1)
+ value_size++;
}
else
{
if (mode == Item_func_json_format::DETAILED &&
- depth > 0 &&
+ depth > 0 && !(curr_state != JST_KEY) &&
append_tab(nice_js, depth, tab_size))
goto error;
nice_js->append((je->value_type == JSON_VALUE_OBJECT) ? "{" : "[", 1);
first_value= 1;
+ value_size= (je->value_type == JSON_VALUE_OBJECT) ? -1: 0;
depth++;
}
@@ -352,11 +363,27 @@ handle_value:
case JST_OBJ_END:
case JST_ARRAY_END:
depth--;
- if (mode == Item_func_json_format::DETAILED &&
+ if (mode == Item_func_json_format::DETAILED && (value_size > 1 || value_size == -1) &&
append_tab(nice_js, depth, tab_size))
goto error;
+
+ if (mode == Item_func_json_format::DETAILED &&
+ value_size == 1 && je->state != JST_OBJ_END)
+ {
+ for (auto i = 0; i < value_len; i++)
+ {
+ nice_js->chop();
+ }
+ for (auto i = 0; i < (depth + 1) * tab_size + 1; i++)
+ {
+ nice_js->chop();
+ }
+ nice_js->append(curr_str);
+ }
+
nice_js->append((je->state == JST_OBJ_END) ? "}": "]", 1);
first_value= 0;
+ value_size= -1;
break;
default: