diff options
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/file_access_pack.cpp | 6 | ||||
-rw-r--r-- | core/io/json.cpp | 53 | ||||
-rw-r--r-- | core/io/json.h | 4 | ||||
-rw-r--r-- | core/io/marshalls.cpp | 4 | ||||
-rw-r--r-- | core/io/resource_import.h | 6 |
5 files changed, 50 insertions, 23 deletions
diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index a7eb8ce6a9..c46e645ed8 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -313,9 +313,9 @@ bool FileAccessPack::file_exists(const String &p_name) { return false; } -FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) - : pf(p_file), - f(FileAccess::open(pf.pack, FileAccess::READ)) { +FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) : + pf(p_file), + f(FileAccess::open(pf.pack, FileAccess::READ)) { if (!f) { ERR_EXPLAIN("Can't open pack-referenced file: " + String(pf.pack)); ERR_FAIL_COND(!f); 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) { diff --git a/core/io/json.h b/core/io/json.h index 893a88e264..5e1a89f069 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -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); }; diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index 37320d7a77..7dd3468c48 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -159,8 +159,8 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int r_variant = str; } break; - // math types + // math types case Variant::VECTOR2: { ERR_FAIL_COND_V(len < (int)4 * 2, ERR_INVALID_DATA); @@ -959,8 +959,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo _encode_string(p_variant, buf, r_len); } break; - // math types + // math types case Variant::VECTOR2: { if (buf) { diff --git a/core/io/resource_import.h b/core/io/resource_import.h index 28489b5d34..166fff85cf 100644 --- a/core/io/resource_import.h +++ b/core/io/resource_import.h @@ -90,9 +90,9 @@ public: PropertyInfo option; Variant default_value; - ImportOption(const PropertyInfo &p_info, const Variant &p_default) - : option(p_info), - default_value(p_default) { + ImportOption(const PropertyInfo &p_info, const Variant &p_default) : + option(p_info), + default_value(p_default) { } ImportOption() {} }; |