Merge pull request #12603 from GodotExplorer/beautify-json

Add indent and sort keys support for JSON.print
This commit is contained in:
Rémi Verschelde 2017-12-06 23:53:05 +01:00 committed by GitHub
commit e6f3253f7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 19 deletions

View file

@ -2694,12 +2694,12 @@ Variant JSONParseResult::get_result() const {
}
void _JSON::_bind_methods() {
ClassDB::bind_method(D_METHOD("print", "value"), &_JSON::print);
ClassDB::bind_method(D_METHOD("print", "value", "indent", "sort_keys"), &_JSON::print, DEFVAL(String()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse);
}
String _JSON::print(const Variant &p_value) {
return JSON::print(p_value);
String _JSON::print(const Variant &p_value, const String &p_indent, bool p_sort_keys) {
return JSON::print(p_value, p_indent, p_sort_keys);
}
Ref<JSONParseResult> _JSON::parse(const String &p_json) {

View file

@ -719,7 +719,7 @@ protected:
public:
static _JSON *get_singleton() { return singleton; }
String print(const Variant &p_value);
String print(const Variant &p_value, const String &p_indent = "", bool p_sort_keys = false);
Ref<JSONParseResult> parse(const String &p_json);
_JSON();

View file

@ -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)
if (i > 0) {
s += ",";
s += _print_var(a[i]);
s += end_statement;
}
s += "]";
s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(a[i], p_indent, p_cur_indent + 1, p_sort_keys);
}
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())
if (E != keys.front()) {
s += ",";
s += _print_var(String(E->get()));
s += ":";
s += _print_var(d[E->get()]);
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) {

View file

@ -64,7 +64,7 @@ class JSON {
static const char *tk_name[TK_MAX];
static String _print_var(const Variant &p_var);
static String _print_var(const Variant &p_var, const String& p_indent, int p_cur_indent, bool p_sort_keys);
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);
@ -72,7 +72,7 @@ class JSON {
static Error _parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
public:
static String print(const Variant &p_var);
static String print(const Variant &p_var, const String& p_indent = "", bool p_sort_keys = true);
static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);
};