Fix json dump and print of circular structure
(cherry picked from commit b3bd54001f
)
This commit is contained in:
parent
a61ec597a1
commit
41b6fdf946
3 changed files with 19 additions and 6 deletions
|
@ -55,7 +55,7 @@ static String _make_indent(const String &p_indent, int p_size) {
|
|||
return indent_text;
|
||||
}
|
||||
|
||||
String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys) {
|
||||
String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers) {
|
||||
String colon = ":";
|
||||
String end_statement = "";
|
||||
|
||||
|
@ -80,20 +80,29 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
|
|||
String s = "[";
|
||||
s += end_statement;
|
||||
Array a = p_var;
|
||||
|
||||
ERR_FAIL_COND_V_MSG(p_markers.has(a.id()), "\"[...]\"", "Converting circular structure to JSON.");
|
||||
p_markers.insert(a.id());
|
||||
|
||||
for (int i = 0; i < a.size(); i++) {
|
||||
if (i > 0) {
|
||||
s += ",";
|
||||
s += end_statement;
|
||||
}
|
||||
s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(a[i], p_indent, p_cur_indent + 1, p_sort_keys);
|
||||
s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(a[i], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
|
||||
}
|
||||
s += end_statement + _make_indent(p_indent, p_cur_indent) + "]";
|
||||
p_markers.erase(a.id());
|
||||
return s;
|
||||
};
|
||||
case Variant::DICTIONARY: {
|
||||
String s = "{";
|
||||
s += end_statement;
|
||||
Dictionary d = p_var;
|
||||
|
||||
ERR_FAIL_COND_V_MSG(p_markers.has(d.id()), "\"{...}\"", "Converting circular structure to JSON.");
|
||||
p_markers.insert(d.id());
|
||||
|
||||
List<Variant> keys;
|
||||
d.get_key_list(&keys);
|
||||
|
||||
|
@ -106,12 +115,13 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
|
|||
s += ",";
|
||||
s += end_statement;
|
||||
}
|
||||
s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(String(E->get()), p_indent, p_cur_indent + 1, p_sort_keys);
|
||||
s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(String(E->get()), p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
|
||||
s += colon;
|
||||
s += _print_var(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys);
|
||||
s += _print_var(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
|
||||
}
|
||||
|
||||
s += end_statement + _make_indent(p_indent, p_cur_indent) + "}";
|
||||
p_markers.erase(d.id());
|
||||
return s;
|
||||
};
|
||||
default:
|
||||
|
@ -120,7 +130,8 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
|
|||
}
|
||||
|
||||
String JSON::print(const Variant &p_var, const String &p_indent, bool p_sort_keys) {
|
||||
return _print_var(p_var, p_indent, 0, p_sort_keys);
|
||||
Set<const void *> markers;
|
||||
return _print_var(p_var, p_indent, 0, p_sort_keys, markers);
|
||||
}
|
||||
|
||||
Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) {
|
||||
|
|
|
@ -63,7 +63,7 @@ class JSON {
|
|||
|
||||
static const char *tk_name[TK_MAX];
|
||||
|
||||
static String _print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys);
|
||||
static String _print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers);
|
||||
|
||||
static Error _get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
|
||||
static Error _parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
|
||||
|
|
|
@ -1406,6 +1406,7 @@ String Variant::stringify(List<const void *> &stack) const {
|
|||
}
|
||||
str += "}";
|
||||
|
||||
stack.erase(d.id());
|
||||
return str;
|
||||
} break;
|
||||
case POOL_VECTOR2_ARRAY: {
|
||||
|
@ -1485,6 +1486,7 @@ String Variant::stringify(List<const void *> &stack) const {
|
|||
}
|
||||
|
||||
str += "]";
|
||||
stack.erase(arr.id());
|
||||
return str;
|
||||
|
||||
} break;
|
||||
|
|
Loading…
Reference in a new issue