diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/convert_json.cpp | 19 | ||||
-rw-r--r-- | src/convert_json.h | 2 | ||||
-rw-r--r-- | src/mods.cpp | 12 | ||||
-rw-r--r-- | src/remoteplayer.cpp | 13 | ||||
-rw-r--r-- | src/script/lua_api/l_util.cpp | 17 | ||||
-rw-r--r-- | src/serverlist.cpp | 7 |
6 files changed, 47 insertions, 23 deletions
diff --git a/src/convert_json.cpp b/src/convert_json.cpp index 1343903b4..bfd7f39c4 100644 --- a/src/convert_json.cpp +++ b/src/convert_json.cpp @@ -46,12 +46,15 @@ Json::Value fetchJsonValue(const std::string &url, return Json::Value(); } Json::Value root; - Json::Reader reader; std::istringstream stream(fetch_result.data); - if (!reader.parse(stream, root)) { + Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = false; + std::string errs; + + if (!Json::parseFromStream(builder, stream, &root, &errs)) { errorstream << "URL: " << url << std::endl; - errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages(); + errorstream << "Failed to parse json data " << errs << std::endl; if (fetch_result.data.size() > 100) { errorstream << "Data (" << fetch_result.data.size() << " bytes) printed to warningstream." << std::endl; @@ -64,3 +67,13 @@ Json::Value fetchJsonValue(const std::string &url, return root; } + +std::string fastWriteJson(const Json::Value &value) +{ + std::ostringstream oss; + Json::StreamWriterBuilder builder; + builder["indentation"] = ""; + std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); + writer->write(value, &oss); + return oss.str(); +} diff --git a/src/convert_json.h b/src/convert_json.h index d19e70851..d8825acdc 100644 --- a/src/convert_json.h +++ b/src/convert_json.h @@ -23,3 +23,5 @@ with this program; if not, write to the Free Software Foundation, Inc., Json::Value fetchJsonValue(const std::string &url, std::vector<std::string> *extra_headers); + +std::string fastWriteJson(const Json::Value &value); diff --git a/src/mods.cpp b/src/mods.cpp index 405cf9c79..dae492339 100644 --- a/src/mods.cpp +++ b/src/mods.cpp @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "subgame.h" #include "settings.h" #include "porting.h" +#include "convert_json.h" static bool parseDependsLine(std::istream &is, std::string &dep, std::set<char> &symbols) @@ -374,7 +375,7 @@ bool ModMetadata::save(const std::string &root_path) } bool w_ok = fs::safeWriteToFile(root_path + DIR_DELIM + m_mod_name, - Json::FastWriter().write(json)); + fastWriteJson(json)); if (w_ok) { m_modified = false; @@ -393,11 +394,14 @@ bool ModMetadata::load(const std::string &root_path) return false; } - Json::Reader reader; Json::Value root; - if (!reader.parse(is, root)) { + Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = false; + std::string errs; + + if (!Json::parseFromStream(builder, is, &root, &errs)) { errorstream << "ModMetadata[" << m_mod_name << "]: failed read data " - "(Json decoding failure)." << std::endl; + "(Json decoding failure). Message: " << errs << std::endl; return false; } diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp index c108ad3f1..f8f31d928 100644 --- a/src/remoteplayer.cpp +++ b/src/remoteplayer.cpp @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" // strlcpy #include "server.h" #include "settings.h" +#include "convert_json.h" /* RemotePlayer @@ -76,8 +77,8 @@ void RemotePlayer::serializeExtraAttributes(std::string &output) json_root[attr.first] = attr.second; } - Json::FastWriter writer; - output = writer.write(json_root); + output = fastWriteJson(json_root); + m_sao->setExtendedAttributeModified(false); } @@ -120,9 +121,13 @@ void RemotePlayer::deSerialize(std::istream &is, const std::string &playername, try { const std::string &extended_attributes = args.get("extended_attributes"); - Json::Reader reader; + std::istringstream iss(extended_attributes); + Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = false; + std::string errs; + Json::Value attr_root; - reader.parse(extended_attributes, attr_root); + Json::parseFromStream(builder, iss, &attr_root, &errs); const Json::Value::Members attr_list = attr_root.getMemberNames(); for (const auto &it : attr_list) { diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index dffbc66d1..5128e79e6 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <json/json.h> #include "cpp_api/s_security.h" #include "porting.h" +#include "convert_json.h" #include "debug.h" #include "log.h" #include "tool.h" @@ -95,12 +96,14 @@ int ModApiUtil::l_parse_json(lua_State *L) Json::Value root; { - Json::Reader reader; std::istringstream stream(jsonstr); - if (!reader.parse(stream, root)) { - errorstream << "Failed to parse json data " - << reader.getFormattedErrorMessages(); + Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = false; + std::string errs; + + if (!Json::parseFromStream(builder, stream, &root, &errs)) { + errorstream << "Failed to parse json data " << errs << std::endl; size_t jlen = strlen(jsonstr); if (jlen > 100) { errorstream << "Data (" << jlen @@ -145,11 +148,9 @@ int ModApiUtil::l_write_json(lua_State *L) std::string out; if (styled) { - Json::StyledWriter writer; - out = writer.write(root); + out = root.toStyledString(); } else { - Json::FastWriter writer; - out = writer.write(root); + out = fastWriteJson(root); } lua_pushlstring(L, out.c_str(), out.size()); return 1; diff --git a/src/serverlist.cpp b/src/serverlist.cpp index a59a89369..7d3ab4bbb 100644 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -185,8 +185,8 @@ const std::string serializeJson(const std::vector<ServerListSpec> &serverlist) list.append(it); } root["list"] = list; - Json::FastWriter writer; - return writer.write(root); + + return fastWriteJson(root); } @@ -249,10 +249,9 @@ void sendAnnounce(AnnounceAction action, server["lag"] = lag; } - Json::FastWriter writer; HTTPFetchRequest fetch_request; fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce"); - fetch_request.post_fields["json"] = writer.write(server); + fetch_request.post_fields["json"] = fastWriteJson(server); fetch_request.multipart = true; httpfetch_async(fetch_request); } |