diff options
| author | reduz <reduzio@gmail.com> | 2020-11-10 18:31:33 -0300 |
|---|---|---|
| committer | reduz <reduzio@gmail.com> | 2020-11-10 19:31:10 -0300 |
| commit | 5288ff538d75d2ddab257a9e1e40050c9b8fa1cb (patch) | |
| tree | e07e6e9a4d22cce046f03b36d06af28a73fea362 /core/io | |
| parent | a80ec80b57f419d84d196fb46dcb5f194c880001 (diff) | |
| download | redot-engine-5288ff538d75d2ddab257a9e1e40050c9b8fa1cb.tar.gz | |
Create Variant built-in functions.
-Moved Expression to use this, removed its own.
-Eventually GDScript/VisualScript/GDNative need to be moved to this.
-Given the JSON functions were hacked-in, removed them and created a new JSONParser class
-Made sure these functions appear properly in documentation, since they will be removed from GDScript
Diffstat (limited to 'core/io')
| -rw-r--r-- | core/io/json.cpp | 32 | ||||
| -rw-r--r-- | core/io/json.h | 23 |
2 files changed, 54 insertions, 1 deletions
diff --git a/core/io/json.cpp b/core/io/json.cpp index 58bce1cf63..d61c2b8236 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -455,3 +455,35 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int & return err; } + +Error JSONParser::parse_string(const String &p_json_string) { + return JSON::parse(p_json_string, data, err_text, err_line); +} +String JSONParser::get_error_text() const { + return err_text; +} +int JSONParser::get_error_line() const { + return err_line; +} +Variant JSONParser::get_data() const { + return data; +} + +Error JSONParser::decode_data(const Variant &p_data, const String &p_indent, bool p_sort_keys) { + string = JSON::print(p_data, p_indent, p_sort_keys); + data = p_data; + return OK; +} + +String JSONParser::get_string() const { + return string; +} + +void JSONParser::_bind_methods() { + ClassDB::bind_method(D_METHOD("parse_string", "json_string"), &JSONParser::parse_string); + ClassDB::bind_method(D_METHOD("get_error_text"), &JSONParser::get_error_text); + ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParser::get_error_line); + ClassDB::bind_method(D_METHOD("get_data"), &JSONParser::get_data); + ClassDB::bind_method(D_METHOD("decode_data", "data", "indent", "sort_keys"), &JSONParser::decode_data, DEFVAL(""), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("get_string"), &JSONParser::get_string); +} diff --git a/core/io/json.h b/core/io/json.h index 9fc6655fb4..2854d956ec 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -31,8 +31,8 @@ #ifndef JSON_H #define JSON_H +#include "core/object/reference.h" #include "core/variant/variant.h" - class JSON { enum TokenType { TK_CURLY_BRACKET_OPEN, @@ -75,4 +75,25 @@ public: static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line); }; +class JSONParser : public Reference { + GDCLASS(JSONParser, Reference); + + Variant data; + String string; + String err_text; + int err_line = 0; + +protected: + static void _bind_methods(); + +public: + Error parse_string(const String &p_json_string); + String get_error_text() const; + int get_error_line() const; + Variant get_data() const; + + Error decode_data(const Variant &p_data, const String &p_indent = "", bool p_sort_keys = true); + String get_string() const; +}; + #endif // JSON_H |
