refactor(json): migrate from nlohmann to rapidjson

Replace nlohmann::json with rapidjson across localization, hitbox manager, server world, and sensitive filter. Add json_utils helper for converting rapidjson documents to maps, and improve parse error handling.
This commit is contained in:
2026-08-04 13:17:45 +08:00
parent ef3be12b64
commit 5e7c6ec891
8 changed files with 132 additions and 33 deletions

54
src/tools/json_utils.cpp Normal file
View File

@@ -0,0 +1,54 @@
#include "Cubed/tools/json_utils.hpp"
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <string>
#include <unordered_map>
namespace Tools {
using rapidjson::Document;
using rapidjson::Value;
namespace {
std::string serialize_value(const Value& v, rapidjson::StringBuffer& buf) {
rapidjson::Writer<rapidjson::StringBuffer> w(buf);
v.Accept(w);
return buf.GetString();
}
} // namespace
std::unordered_map<std::string, std::string> doc_to_map(const Document& doc) {
std::unordered_map<std::string, std::string> m;
if (!doc.IsObject())
return m;
for (auto it = doc.MemberBegin(); it != doc.MemberEnd(); ++it) {
const std::string KEY(it->name.GetString(), it->name.GetStringLength());
const Value& v = it->value;
std::string val;
if (v.IsString()) {
val.assign(v.GetString(), v.GetStringLength());
} else if (v.IsInt()) {
val = std::to_string(v.GetInt());
} else if (v.IsUint()) {
val = std::to_string(v.GetUint());
} else if (v.IsInt64()) {
val = std::to_string(v.GetInt64());
} else if (v.IsUint64()) {
val = std::to_string(v.GetUint64());
} else if (v.IsDouble()) {
val = std::to_string(v.GetDouble());
} else if (v.IsBool()) {
val = v.GetBool() ? "true" : "false";
} else if (v.IsNull()) {
val = "null";
} else {
rapidjson::StringBuffer buf;
val = serialize_value(v, buf);
}
m.emplace(KEY, val);
}
return m;
}
} // namespace Tools

View File

@@ -2,18 +2,25 @@
#include <queue>
#include <utf8cpp/utf8.h>
using nlohmann::json;
using namespace rapidjson;
namespace Cubed {
SensitiveFilter::SensitiveFilter() {
};
void SensitiveFilter::load(const nlohmann::json& j) {
void SensitiveFilter::load(const Document& doc) {
trie.clear();
trie.emplace_back();
for (const auto& item : j["words"]) {
auto str = item.get<std::string>();
if (!doc.HasMember("words")) {
return;
}
const Value& words = doc["words"];
for (SizeType i = 0; i < words.Size(); ++i) {
const auto& w = words[i];
if (!w.IsString()) {
continue;
}
std::string str(w.GetString(), w.GetStringLength());
std::u32string word;
utf8::utf8to32(str.begin(), str.end(), std::back_inserter(word));
insert(word);