diff options
author | karroffel <therzog@mail.de> | 2017-09-15 15:40:27 +0200 |
---|---|---|
committer | karroffel <therzog@mail.de> | 2017-09-15 15:40:27 +0200 |
commit | 308d20aba5f9dfa8725866c118780be04744622f (patch) | |
tree | d8fae39b5852cceb253cc9073929b01963af789e | |
parent | 6b729726e0c848e2a60eb72782315a883526c913 (diff) |
added JSON singleton
There was no way to access JSON functionality in scripting languages
apart from GDScript because the JSON class wasn't exposed to ClassDB.
-rw-r--r-- | core/bind/core_bind.cpp | 74 | ||||
-rw-r--r-- | core/bind/core_bind.h | 46 | ||||
-rw-r--r-- | core/register_core_types.cpp | 6 |
3 files changed, 126 insertions, 0 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index d0acd04497..b1393d2818 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -33,6 +33,7 @@ #include "geometry.h" #include "io/file_access_compressed.h" #include "io/file_access_encrypted.h" +#include "io/json.h" #include "io/marshalls.h" #include "os/keyboard.h" #include "os/os.h" @@ -2600,3 +2601,76 @@ _Engine *_Engine::singleton = NULL; _Engine::_Engine() { singleton = this; } + +void JSONParseResult::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_error"), &JSONParseResult::get_error); + ClassDB::bind_method(D_METHOD("get_error_string"), &JSONParseResult::get_error_string); + ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParseResult::get_error_line); + ClassDB::bind_method(D_METHOD("get_result"), &JSONParseResult::get_result); + + ClassDB::bind_method(D_METHOD("set_error", "error"), &JSONParseResult::set_error); + ClassDB::bind_method(D_METHOD("set_error_string", "error_string"), &JSONParseResult::set_error_string); + ClassDB::bind_method(D_METHOD("set_error_line", "error_line"), &JSONParseResult::set_error_line); + ClassDB::bind_method(D_METHOD("set_result", "result"), &JSONParseResult::set_result); + + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "error", PROPERTY_HINT_NONE, "Error", PROPERTY_USAGE_CLASS_IS_ENUM), "set_error", "get_error"); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "error_string"), "set_error_string", "get_error_string"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "error_line"), "set_error_line", "get_error_line"); + ADD_PROPERTYNZ(PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "set_result", "get_result"); +} + +void JSONParseResult::set_error(Error p_error) { + error = p_error; +} + +Error JSONParseResult::get_error() const { + return error; +} + +void JSONParseResult::set_error_string(const String &p_error_string) { + error_string = p_error_string; +} + +String JSONParseResult::get_error_string() const { + return error_string; +} + +void JSONParseResult::set_error_line(int p_error_line) { + error_line = p_error_line; +} + +int JSONParseResult::get_error_line() const { + return error_line; +} + +void JSONParseResult::set_result(const Variant &p_result) { + result = p_result; +} + +Variant JSONParseResult::get_result() const { + return result; +} + +void _JSON::_bind_methods() { + ClassDB::bind_method(D_METHOD("print", "value"), &_JSON::print); + ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse); +} + +String _JSON::print(const Variant &p_value) { + return JSON::print(p_value); +} + +Ref<JSONParseResult> _JSON::parse(const String &p_json) { + Ref<JSONParseResult> result; + result.instance(); + + result->error = JSON::parse(p_json, result->result, result->error_string, result->error_line); + + return result; +} + +_JSON *_JSON::singleton = NULL; + +_JSON::_JSON() { + singleton = this; +} diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 0578c2b80f..00fbeef8ec 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -660,4 +660,50 @@ public: _Engine(); }; +class _JSON; + +class JSONParseResult : public Reference { + GDCLASS(JSONParseResult, Reference) + + friend class _JSON; + + Error error; + String error_string; + int error_line; + + Variant result; + +protected: + static void _bind_methods(); + +public: + void set_error(Error p_error); + Error get_error() const; + + void set_error_string(const String &p_error_string); + String get_error_string() const; + + void set_error_line(int p_error_line); + int get_error_line() const; + + void set_result(const Variant &p_result); + Variant get_result() const; +}; + +class _JSON : public Object { + GDCLASS(_JSON, Object) + +protected: + static void _bind_methods(); + static _JSON *singleton; + +public: + static _JSON *get_singleton() { return singleton; } + + String print(const Variant &p_value); + Ref<JSONParseResult> parse(const String &p_json); + + _JSON(); +}; + #endif // CORE_BIND_H diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 27c31127a4..0e34a3eea5 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -68,6 +68,7 @@ static _Engine *_engine = NULL; static _ClassDB *_classdb = NULL; static _Marshalls *_marshalls = NULL; static TranslationLoaderPO *resource_format_po = NULL; +static _JSON *_json = NULL; static IP *ip = NULL; @@ -162,6 +163,8 @@ void register_core_types() { ClassDB::register_class<AStar>(); ClassDB::register_class<EncodedObjectAsID>(); + ClassDB::register_class<JSONParseResult>(); + ip = IP::create(); _geometry = memnew(_Geometry); @@ -172,6 +175,7 @@ void register_core_types() { _engine = memnew(_Engine); _classdb = memnew(_ClassDB); _marshalls = memnew(_Marshalls); + _json = memnew(_JSON); } void register_core_settings() { @@ -193,6 +197,7 @@ void register_core_singletons() { ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("TranslationServer", TranslationServer::get_singleton())); ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Input", Input::get_singleton())); ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("InputMap", InputMap::get_singleton())); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("JSON", _JSON::get_singleton())); } void unregister_core_types() { @@ -203,6 +208,7 @@ void unregister_core_types() { memdelete(_engine); memdelete(_classdb); memdelete(_marshalls); + memdelete(_json); memdelete(_geometry); |