Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate JSON, JSONParseResults and JSONParser into JSON #44806

Merged
merged 1 commit into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "core/debugger/engine_debugger.h"
#include "core/io/file_access_compressed.h"
#include "core/io/file_access_encrypted.h"
#include "core/io/json.h"
#include "core/io/marshalls.h"
#include "core/math/geometry_2d.h"
#include "core/math/geometry_3d.h"
Expand Down Expand Up @@ -2137,80 +2136,6 @@ void _Engine::_bind_methods() {

_Engine *_Engine::singleton = nullptr;

////// _JSON //////

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_PROPERTY(PropertyInfo(Variant::OBJECT, "error", PROPERTY_HINT_NONE, "Error", PROPERTY_USAGE_CLASS_IS_ENUM), "set_error", "get_error");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "error_string"), "set_error_string", "get_error_string");
ADD_PROPERTY(PropertyInfo(Variant::INT, "error_line"), "set_error_line", "get_error_line");
ADD_PROPERTY(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", "indent", "sort_keys", "full_precision"), &_JSON::print, DEFVAL(String()), DEFVAL(false), DEFVAL(false));
ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse);
}

String _JSON::print(const Variant &p_value, const String &p_indent, bool p_sort_keys, bool p_full_precision) {
return JSON::print(p_value, p_indent, p_sort_keys, p_full_precision);
}

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);

if (result->error != OK) {
ERR_PRINT(vformat("Error parsing JSON at line %s: %s", result->error_line, result->error_string));
}
return result;
}

_JSON *_JSON::singleton = nullptr;

////// _EngineDebugger //////

void _EngineDebugger::_bind_methods() {
Expand Down
48 changes: 0 additions & 48 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,54 +656,6 @@ class _Engine : public Object {
_Engine() { singleton = this; }
};

class _JSON;

class JSONParseResult : public RefCounted {
GDCLASS(JSONParseResult, RefCounted);

friend class _JSON;

Error error;
String error_string;
int error_line = -1;

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;

JSONParseResult() {}
};

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, const String &p_indent = "", bool p_sort_keys = false, bool p_full_precision = false);
Ref<JSONParseResult> parse(const String &p_json);

_JSON() { singleton = this; }
};

class _EngineDebugger : public Object {
GDCLASS(_EngineDebugger, Object);

Expand Down
57 changes: 21 additions & 36 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const char *JSON::tk_name[TK_MAX] = {
"EOF",
};

static String _make_indent(const String &p_indent, int p_size) {
String JSON::_make_indent(const String &p_indent, int p_size) {
String indent_text = "";
if (!p_indent.is_empty()) {
for (int i = 0; i < p_size; i++) {
Expand All @@ -55,7 +55,7 @@ static String _make_indent(const String &p_indent, int p_size) {
return indent_text;
}

String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers, bool p_full_precision) {
String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers, bool p_full_precision) {
String colon = ":";
String end_statement = "";

Expand Down Expand Up @@ -100,7 +100,7 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
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, p_markers);
s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(a[i], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
}
s += end_statement + _make_indent(p_indent, p_cur_indent) + "]";
p_markers.erase(a.id());
Expand All @@ -126,9 +126,9 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
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, p_markers);
s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(String(E->get()), p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
s += colon;
s += _print_var(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
s += _stringify(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
}

s += end_statement + _make_indent(p_indent, p_cur_indent) + "}";
Expand All @@ -140,11 +140,6 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
}
}

String JSON::print(const Variant &p_var, const String &p_indent, bool p_sort_keys, bool p_full_precision) {
Set<const void *> markers;
return _print_var(p_var, p_indent, 0, p_sort_keys, markers, p_full_precision);
}

Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) {
while (p_len > 0) {
switch (p_str[index]) {
Expand Down Expand Up @@ -499,7 +494,7 @@ Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index,
return ERR_PARSE_ERROR;
}

Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) {
Error JSON::_parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) {
const char32_t *str = p_json.ptr();
int idx = 0;
int len = p_json.length();
Expand Down Expand Up @@ -530,34 +525,24 @@ 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;
String JSON::stringify(const Variant &p_var, const String &p_indent, bool p_sort_keys, bool p_full_precision) {
Set<const void *> markers;
return _stringify(p_var, p_indent, 0, p_sort_keys, markers, p_full_precision);
}

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;
Error JSON::parse(const String &p_json_string) {
Error err = _parse_string(p_json_string, data, err_str, err_line);
if (err == Error::OK) {
err_line = 0;
}
return err;
}

String JSONParser::get_string() const {
return string;
}
void JSON::_bind_methods() {
ClassDB::bind_method(D_METHOD("stringify", "data", "indent", "sort_keys", "full_precision"), &JSON::stringify, DEFVAL(""), DEFVAL(true), DEFVAL(false));
ClassDB::bind_method(D_METHOD("parse", "json_string"), &JSON::parse);

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);
ClassDB::bind_method(D_METHOD("get_data"), &JSON::get_data);
ClassDB::bind_method(D_METHOD("get_error_line"), &JSON::get_error_line);
ClassDB::bind_method(D_METHOD("get_error_message"), &JSON::get_error_message);
}
38 changes: 16 additions & 22 deletions core/io/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@

#include "core/object/ref_counted.h"
#include "core/variant/variant.h"
class JSON {

class JSON : public RefCounted {
GDCLASS(JSON, RefCounted);

enum TokenType {
TK_CURLY_BRACKET_OPEN,
TK_CURLY_BRACKET_CLOSE,
Expand All @@ -60,39 +63,30 @@ class JSON {
Variant value;
};

static const char *tk_name[TK_MAX];
Variant data;
String err_str;
int err_line = 0;

static String _print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers, bool p_full_precision = false);
static const char *tk_name[];

static String _make_indent(const String &p_indent, int p_size);
static String _stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set<const void *> &p_markers, bool p_full_precision = false);
static Error _get_token(const char32_t *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 char32_t *p_str, int &index, int p_len, int &line, String &r_err_str);
static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str);
static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str);

public:
static String print(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false);
static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);
};

class JSONParser : public RefCounted {
GDCLASS(JSONParser, RefCounted);

Variant data;
String string;
String err_text;
int err_line = 0;
static Error _parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);

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;
String stringify(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false);
Error parse(const String &p_json_string);

Error decode_data(const Variant &p_data, const String &p_indent = "", bool p_sort_keys = true);
String get_string() const;
inline Variant get_data() const { return data; }
inline int get_error_line() const { return err_line; }
inline String get_error_message() const { return err_str; }
};

#endif // JSON_H
9 changes: 1 addition & 8 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ static _OS *_os = nullptr;
static _Engine *_engine = nullptr;
static _ClassDB *_classdb = nullptr;
static _Marshalls *_marshalls = nullptr;
static _JSON *_json = nullptr;
static _EngineDebugger *_engine_debugger = nullptr;

static IP *ip = nullptr;
Expand Down Expand Up @@ -199,7 +198,7 @@ void register_core_types() {
ClassDB::register_class<_Semaphore>();

ClassDB::register_class<XMLParser>();
ClassDB::register_class<JSONParser>();
ClassDB::register_class<JSON>();

ClassDB::register_class<ConfigFile>();

Expand All @@ -212,8 +211,6 @@ void register_core_types() {
ClassDB::register_class<EncodedObjectAsID>();
ClassDB::register_class<RandomNumberGenerator>();

ClassDB::register_class<JSONParseResult>();

ClassDB::register_virtual_class<ResourceImporter>();

ip = IP::create();
Expand All @@ -227,7 +224,6 @@ void register_core_types() {
_engine = memnew(_Engine);
_classdb = memnew(_ClassDB);
_marshalls = memnew(_Marshalls);
_json = memnew(_JSON);
_engine_debugger = memnew(_EngineDebugger);
}

Expand Down Expand Up @@ -256,7 +252,6 @@ void register_core_singletons() {
ClassDB::register_class<TranslationServer>();
ClassDB::register_virtual_class<Input>();
ClassDB::register_class<InputMap>();
ClassDB::register_class<_JSON>();
ClassDB::register_class<Expression>();
ClassDB::register_class<_EngineDebugger>();
ClassDB::register_class<Time>();
Expand All @@ -274,7 +269,6 @@ void register_core_singletons() {
Engine::get_singleton()->add_singleton(Engine::Singleton("TranslationServer", TranslationServer::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("Input", Input::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("InputMap", InputMap::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("JSON", _JSON::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("EngineDebugger", _EngineDebugger::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("Time", Time::get_singleton()));
}
Expand All @@ -286,7 +280,6 @@ void unregister_core_types() {
memdelete(_engine);
memdelete(_classdb);
memdelete(_marshalls);
memdelete(_json);
memdelete(_engine_debugger);

memdelete(_geometry_2d);
Expand Down
6 changes: 6 additions & 0 deletions core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/core_string_names.h"
#include "core/debugger/engine_debugger.h"
#include "core/io/json.h"
#include "core/io/marshalls.h"
#include "core/io/resource.h"
#include "core/math/math_funcs.h"
Expand Down Expand Up @@ -1838,6 +1839,11 @@ String Variant::stringify(List<const void *> &stack) const {
return "";
}

String Variant::to_json_string() const {
JSON json;
return json.stringify(*this);
}

Variant::operator Vector2() const {
if (type == VECTOR2) {
return *reinterpret_cast<const Vector2 *>(_data._mem);
Expand Down
Loading