From 7fdacffe53c7735a0c2eaf4c2fc4e96337975b24 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 20 May 2024 03:06:26 +0300 Subject: [PATCH] json-related refactor --- src/assets/assetload_funcs.cpp | 1 + src/coders/binary_json.hpp | 28 ++++++++++++++-------------- src/coders/json.cpp | 6 ++++-- src/coders/json.hpp | 13 ++++--------- src/files/files.cpp | 4 ++-- src/logic/scripting/lua/libjson.cpp | 5 +---- src/window/input.cpp | 8 ++++---- 7 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index ea4560b5..9dae4c78 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -6,6 +6,7 @@ #include "../audio/audio.hpp" #include "../files/files.hpp" #include "../files/engine_paths.hpp" +#include "../coders/commons.hpp" #include "../coders/imageio.hpp" #include "../coders/json.hpp" #include "../coders/GLSLExtension.hpp" diff --git a/src/coders/binary_json.hpp b/src/coders/binary_json.hpp index ba7531bc..c79a7d4d 100644 --- a/src/coders/binary_json.hpp +++ b/src/coders/binary_json.hpp @@ -11,20 +11,20 @@ namespace dynamic { } namespace json { - const int BJSON_END = 0x0; - const int BJSON_TYPE_DOCUMENT = 0x1; - const int BJSON_TYPE_LIST = 0x2; - const int BJSON_TYPE_BYTE = 0x3; - const int BJSON_TYPE_INT16 = 0x4; - const int BJSON_TYPE_INT32 = 0x5; - const int BJSON_TYPE_INT64 = 0x6; - const int BJSON_TYPE_NUMBER = 0x7; - const int BJSON_TYPE_STRING = 0x8; - const int BJSON_TYPE_BYTES = 0x9; - const int BJSON_TYPE_FALSE = 0xA; - const int BJSON_TYPE_TRUE = 0xB; - const int BJSON_TYPE_NULL = 0xC; - const int BJSON_TYPE_CDOCUMENT = 0x1F; + inline constexpr int BJSON_END = 0x0; + inline constexpr int BJSON_TYPE_DOCUMENT = 0x1; + inline constexpr int BJSON_TYPE_LIST = 0x2; + inline constexpr int BJSON_TYPE_BYTE = 0x3; + inline constexpr int BJSON_TYPE_INT16 = 0x4; + inline constexpr int BJSON_TYPE_INT32 = 0x5; + inline constexpr int BJSON_TYPE_INT64 = 0x6; + inline constexpr int BJSON_TYPE_NUMBER = 0x7; + inline constexpr int BJSON_TYPE_STRING = 0x8; + inline constexpr int BJSON_TYPE_BYTES = 0x9; + inline constexpr int BJSON_TYPE_FALSE = 0xA; + inline constexpr int BJSON_TYPE_TRUE = 0xB; + inline constexpr int BJSON_TYPE_NULL = 0xC; + inline constexpr int BJSON_TYPE_CDOCUMENT = 0x1F; extern std::vector to_binary(const dynamic::Map* obj, bool compress=false); extern std::shared_ptr from_binary(const ubyte* src, size_t size); diff --git a/src/coders/json.cpp b/src/coders/json.cpp index 89f06354..19b59726 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -1,5 +1,7 @@ #include "json.hpp" +#include "commons.hpp" + #include "../data/dynamic.hpp" #include "../util/stringutil.hpp" @@ -240,11 +242,11 @@ Value Parser::parseValue() { throw error("unexpected character '"+std::string({next})+"'"); } -std::unique_ptr json::parse(const std::string& filename, const std::string& source) { +dynamic::Map_sptr json::parse(const std::string& filename, const std::string& source) { Parser parser(filename, source); return parser.parse(); } -std::unique_ptr json::parse(const std::string& source) { +dynamic::Map_sptr json::parse(const std::string& source) { return parse("", source); } diff --git a/src/coders/json.hpp b/src/coders/json.hpp index 692117d5..f4379fdc 100644 --- a/src/coders/json.hpp +++ b/src/coders/json.hpp @@ -1,25 +1,20 @@ #ifndef CODERS_JSON_HPP_ #define CODERS_JSON_HPP_ -#include "commons.hpp" #include "binary_json.hpp" #include "../data/dynamic.hpp" #include "../typedefs.hpp" -#include #include -#include -#include -#include namespace json { - std::unique_ptr parse(const std::string& filename, const std::string& source); - std::unique_ptr parse(const std::string& source); + dynamic::Map_sptr parse(const std::string& filename, const std::string& source); + dynamic::Map_sptr parse(const std::string& source); std::string stringify( - const dynamic::Map* obj, - bool nice, + const dynamic::Map* obj, + bool nice, const std::string& indent ); diff --git a/src/files/files.cpp b/src/files/files.cpp index d1ec0ffb..104f21b5 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -1,5 +1,6 @@ #include "files.hpp" +#include "../coders/commons.hpp" #include "../coders/json.hpp" #include "../coders/gzip.hpp" #include "../util/stringutil.hpp" @@ -107,8 +108,7 @@ bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool c std::shared_ptr files::read_json(fs::path filename) { std::string text = files::read_string(filename); try { - auto obj = json::parse(filename.string(), text); - return obj; + return json::parse(filename.string(), text);; } catch (const parsing_error& error) { std::cerr << error.errorLog() << std::endl; throw std::runtime_error("could not to parse "+filename.string()); diff --git a/src/logic/scripting/lua/libjson.cpp b/src/logic/scripting/lua/libjson.cpp index 2c72a75e..d868d2da 100644 --- a/src/logic/scripting/lua/libjson.cpp +++ b/src/logic/scripting/lua/libjson.cpp @@ -25,10 +25,7 @@ static int l_json_stringify(lua_State* L) { static int l_json_parse(lua_State* L) { auto string = lua_tostring(L, 1); auto element = json::parse("", string); - auto value = std::make_unique( - dynamic::Map_sptr(element.release()) - ); - scripting::state->pushvalue(*value); + scripting::state->pushvalue(element); return 1; } diff --git a/src/window/input.cpp b/src/window/input.cpp index bdb8bbcd..50825047 100644 --- a/src/window/input.cpp +++ b/src/window/input.cpp @@ -140,9 +140,9 @@ std::string input_util::to_string(keycode code) { std::string input_util::to_string(mousecode code) { switch (code) { - case mousecode::BUTTON_1: return "LMB"; - case mousecode::BUTTON_2: return "RMB"; - case mousecode::BUTTON_3: return "MMB"; + case mousecode::BUTTON_1: return "LMB"; + case mousecode::BUTTON_2: return "RMB"; + case mousecode::BUTTON_3: return "MMB"; + default: return "unknown button"; } - return "unknown button"; }