diff options
author | Geequlim <geequlim@gmail.com> | 2017-11-03 15:32:58 +0800 |
---|---|---|
committer | Geequlim <geequlim@gmail.com> | 2017-11-07 11:52:45 +0800 |
commit | f7168bf217f810ba7da310953c043448135d0718 (patch) | |
tree | 0feed4a6cc25f049d248685c4d3b15f2e6c9ec00 /core/io/json.cpp | |
parent | c880302754352dce1e44bf00bf9b1ac20e71cd1d (diff) |
Add indent and sort keys support for JSON.print
Diffstat (limited to 'core/io/json.cpp')
-rw-r--r-- | core/io/json.cpp | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/core/io/json.cpp b/core/io/json.cpp index 2e9170bc34..ddfc792cc7 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -43,7 +43,25 @@ const char *JSON::tk_name[TK_MAX] = { "EOF", }; -String JSON::_print_var(const Variant &p_var) { +static String _make_indent(const String& p_indent, int p_size) { + + String indent_text = ""; + if (!p_indent.empty()) { + for (int i = 0; i < p_size; i++) + indent_text += p_indent; + } + return indent_text; +} + +String JSON::_print_var(const Variant &p_var, const String& p_indent, int p_cur_indent, bool p_sort_keys) { + + String colon = ":"; + String end_statement = ""; + + if (!p_indent.empty()) { + colon += " "; + end_statement += "\n"; + } switch (p_var.get_type()) { @@ -57,41 +75,50 @@ String JSON::_print_var(const Variant &p_var) { case Variant::ARRAY: { String s = "["; + s += end_statement; Array a = p_var; for (int i = 0; i < a.size(); i++) { - if (i > 0) - s += ", "; - s += _print_var(a[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 += "]"; + s += end_statement + _make_indent(p_indent, p_cur_indent) + "]"; return s; }; case Variant::DICTIONARY: { String s = "{"; + s += end_statement; Dictionary d = p_var; List<Variant> keys; d.get_key_list(&keys); + if (p_sort_keys) + keys.sort(); + for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { - if (E != keys.front()) - s += ", "; - s += _print_var(String(E->get())); - s += ":"; - s += _print_var(d[E->get()]); + if (E != keys.front()) { + 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 += colon; + s += _print_var(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys); } - s += "}"; + s += end_statement + _make_indent(p_indent, p_cur_indent) + "}"; return s; }; default: return "\"" + String(p_var).json_escape() + "\""; } } -String JSON::print(const Variant &p_var) { +String JSON::print(const Variant &p_var, const String& p_indent, bool p_sort_keys) { - return _print_var(p_var); + return _print_var(p_var, p_indent, 0, p_sort_keys); } Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) { |