1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Rename toJson to toString/toCompactString for consistency

This commit is contained in:
Ivan Savenko 2024-02-12 01:22:16 +02:00
parent a2b8eaf7fb
commit 54796c7c56
22 changed files with 45 additions and 36 deletions

View File

@ -243,7 +243,7 @@ int main(int argc, char * argv[])
// Initialize logging based on settings
logConfig->configure();
logGlobal->debug("settings = %s", settings.toJsonNode().toJson());
logGlobal->debug("settings = %s", settings.toJsonNode().toString());
// Some basic data validation to produce better error messages in cases of incorrect install
auto testFile = [](std::string filename, std::string message)

View File

@ -254,7 +254,7 @@ void ClientCommandManager::handleGetConfigCommand()
const boost::filesystem::path filePath = contentOutPath / (name + ".json");
std::ofstream file(filePath.c_str());
file << object.toJson();
file << object.toString();
}
}
}
@ -358,7 +358,7 @@ void ClientCommandManager::handleBonusesCommand(std::istringstream & singleWordB
auto format = [outputFormat](const BonusList & b) -> std::string
{
if(outputFormat == "json")
return b.toJsonNode().toJson(true);
return b.toJsonNode().toCompactString();
std::ostringstream ss;
ss << b;

View File

@ -273,7 +273,7 @@ void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection>
void GlobalLobbyClient::sendMessage(const JsonNode & data)
{
networkConnection->sendPacket(data.toBytes(true));
networkConnection->sendPacket(data.toBytes());
}
void GlobalLobbyClient::sendOpenPublicRoom()
@ -362,5 +362,5 @@ void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & ne
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
toSend["gameRoomID"] = settings["lobby"]["roomID"];
netConnection->sendPacket(toSend.toBytes(true));
netConnection->sendPacket(toSend.toBytes());
}

View File

@ -116,7 +116,7 @@ JsonNode toJson(QVariant object)
void JsonToFile(QString filename, QVariant object)
{
std::fstream file(qstringToPath(filename).c_str(), std::ios::out | std::ios_base::binary);
file << toJson(object).toJson();
file << toJson(object).toString();
}
}

View File

@ -90,7 +90,7 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
JsonUtils::minimize(savedConf, schema);
std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin(dataFilename))->c_str(), std::ofstream::out | std::ofstream::trunc);
file << savedConf.toJson();
file << savedConf.toString();
}
JsonNode & SettingsStorage::getNode(const std::vector<std::string> & path)

View File

@ -1201,7 +1201,7 @@ void CTownHandler::initializeRequirements()
{
logMod->error("Unexpected length of town buildings requirements: %d", node.Vector().size());
logMod->error("Entry contains: ");
logMod->error(node.toJson());
logMod->error(node.toString());
}
auto index = VLC->identifiers()->getIdentifier(requirement.town->getBuildingScope(), node[0]);

View File

@ -72,7 +72,7 @@ si32 CAddInfo::operator[](size_type pos) const
std::string CAddInfo::toString() const
{
return toJsonNode().toJson(true);
return toJsonNode().toCompactString();
}
JsonNode CAddInfo::toJsonNode() const

View File

@ -553,7 +553,7 @@ std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability)
if (!parseBonus(ability, b.get()))
{
// caller code can not handle this case and presumes that returned bonus is always valid
logGlobal->error("Failed to parse bonus! Json config was %S ", ability.toJson());
logGlobal->error("Failed to parse bonus! Json config was %S ", ability.toString());
b->type = BonusType::NONE;
return b;
}
@ -577,7 +577,7 @@ static BonusParams convertDeprecatedBonus(const JsonNode &ability)
{
if(vstd::contains(deprecatedBonusSet, ability["type"].String()))
{
logMod->warn("There is deprecated bonus found:\n%s\nTrying to convert...", ability.toJson());
logMod->warn("There is deprecated bonus found:\n%s\nTrying to convert...", ability.toString());
auto params = BonusParams(ability["type"].String(),
ability["subtype"].isString() ? ability["subtype"].String() : "",
ability["subtype"].isNumber() ? ability["subtype"].Integer() : -1);
@ -589,11 +589,11 @@ static BonusParams convertDeprecatedBonus(const JsonNode &ability)
params.targetType = BonusSource::SECONDARY_SKILL;
}
logMod->warn("Please, use this bonus:\n%s\nConverted successfully!", params.toJson().toJson());
logMod->warn("Please, use this bonus:\n%s\nConverted successfully!", params.toJson().toString());
return params;
}
else
logMod->error("Cannot convert bonus!\n%s", ability.toJson());
logMod->error("Cannot convert bonus!\n%s", ability.toString());
}
BonusParams ret;
ret.isConverted = false;

View File

@ -410,19 +410,27 @@ JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer)
return ::resolvePointer(*this, jsonPointer);
}
std::vector<std::byte> JsonNode::toBytes(bool compact) const
std::vector<std::byte> JsonNode::toBytes() const
{
std::string jsonString = toJson(compact);
std::string jsonString = toString();
auto dataBegin = reinterpret_cast<const std::byte*>(jsonString.data());
auto dataEnd = dataBegin + jsonString.size();
std::vector<std::byte> result(dataBegin, dataEnd);
return result;
}
std::string JsonNode::toJson(bool compact) const
std::string JsonNode::toCompactString() const
{
std::ostringstream out;
JsonWriter writer(out, compact);
JsonWriter writer(out, true);
writer.writeNode(*this);
return out.str();
}
std::string JsonNode::toString() const
{
std::ostringstream out;
JsonWriter writer(out, false);
writer.writeNode(*this);
return out.str();
}

View File

@ -116,8 +116,9 @@ public:
JsonNode & operator[](size_t child);
const JsonNode & operator[](size_t child) const;
std::string toJson(bool compact = false) const;
std::vector<std::byte> toBytes(bool compact = false) const;
std::string toCompactString() const;
std::string toString() const;
std::vector<std::byte> toBytes() const;
template <typename Handler> void serialize(Handler &h)
{

View File

@ -23,7 +23,7 @@ struct Bonus;
struct Component;
class CStackBasicDescriptor;
class DLL_LINKAGE JsonRandom : public GameCallbackHolder
class JsonRandom : public GameCallbackHolder
{
public:
using Variables = std::map<std::string, int>;
@ -46,7 +46,7 @@ private:
public:
using GameCallbackHolder::GameCallbackHolder;
struct DLL_LINKAGE RandomStackInfo
struct RandomStackInfo
{
std::vector<const CCreature *> allowedCreatures;
si32 minAmount;

View File

@ -134,7 +134,7 @@ bool JsonUtils::validate(const JsonNode & node, const std::string & schemaName,
{
logMod->warn("Data in %s is invalid!", dataName);
logMod->warn(log);
logMod->trace("%s json: %s", dataName, node.toJson(true));
logMod->trace("%s json: %s", dataName, node.toCompactString());
}
return log.empty();
}

View File

@ -1049,7 +1049,7 @@ void CMapLoaderJson::MapObjectLoader::construct()
if(typeName.empty())
{
logGlobal->error("Object type missing");
logGlobal->debug(configuration.toJson());
logGlobal->debug(configuration.toString());
return;
}
@ -1069,7 +1069,7 @@ void CMapLoaderJson::MapObjectLoader::construct()
else if(subtypeName.empty())
{
logGlobal->error("Object subtype missing");
logGlobal->debug(configuration.toJson());
logGlobal->debug(configuration.toString());
return;
}

View File

@ -512,7 +512,7 @@ void CModHandler::afterLoad(bool onlyEssential)
if(!onlyEssential)
{
std::fstream file(CResourceHandler::get()->getResourceName(ResourcePath("config/modSettings.json"))->c_str(), std::ofstream::out | std::ofstream::trunc);
file << modSettings.toJson();
file << modSettings.toString();
}
}

View File

@ -833,7 +833,7 @@ CSpell * CSpellHandler::loadFromJson(const std::string & scope, const JsonNode &
{
logMod->warn("Spell %s has old target condition format. Expected configuration: ", spell->getNameTranslated());
spell->targetCondition = spell->convertTargetCondition(immunities, absoluteImmunities, limiters, absoluteLimiters);
logMod->warn("\n\"targetCondition\" : %s", spell->targetCondition.toJson());
logMod->warn("\n\"targetCondition\" : %s", spell->targetCondition.toString());
}
}
else

View File

@ -122,7 +122,7 @@ JsonNode toJson(QVariant object)
void JsonToFile(QString filename, QVariant object)
{
std::fstream file(qstringToPath(filename).c_str(), std::ios::out | std::ios_base::binary);
file << toJson(object).toJson();
file << toJson(object).toString();
}
}

View File

@ -181,7 +181,7 @@ MainWindow::MainWindow(QWidget* parent) :
// Initialize logging based on settings
logConfig->configure();
logGlobal->debug("settings = %s", settings.toJsonNode().toJson());
logGlobal->debug("settings = %s", settings.toJsonNode().toString());
// Some basic data validation to produce better error messages in cases of incorrect install
auto testFile = [](std::string filename, std::string message) -> bool

View File

@ -76,7 +76,7 @@ bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m) const
if(response.getType() != JsonNode::JsonType::DATA_BOOL)
{
logMod->error("Invalid API response from script %s.", script->getName());
logMod->debug(response.toJson(true));
logMod->debug(response.toCompactString());
return false;
}
return response.Bool();
@ -116,7 +116,7 @@ bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m, const Ef
if(response.getType() != JsonNode::JsonType::DATA_BOOL)
{
logMod->error("Invalid API response from script %s.", script->getName());
logMod->debug(response.toJson(true));
logMod->debug(response.toCompactString());
return false;
}
return response.Bool();

View File

@ -45,7 +45,7 @@ void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnecti
JsonNode message;
message["type"].String() = "leaveGameRoom";
message["accountID"].String() = proxy.first;
controlConnection->sendPacket(message.toBytes(true));
controlConnection->sendPacket(message.toBytes());
break;
}
}
@ -122,7 +122,7 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
toSend["gameRoomID"].String() = owner.uuid;
toSend["accountID"] = settings["lobby"]["accountID"];
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
connection->sendPacket(toSend.toBytes(true));
connection->sendPacket(toSend.toBytes());
}
else
{
@ -137,7 +137,7 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
toSend["gameRoomID"].String() = owner.uuid;
toSend["guestAccountID"].String() = guestAccountID;
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
connection->sendPacket(toSend.toBytes(true));
connection->sendPacket(toSend.toBytes());
proxyConnections[guestAccountID] = connection;
owner.onNewConnection(connection);

View File

@ -165,7 +165,7 @@ void JsonComparer::checkEqualJson(const JsonNode & actual, const JsonNode & expe
}
else
{
check(false, "type mismatch. \n expected:\n"+expected.toJson(true)+"\n actual:\n" +actual.toJson(true));
check(false, "type mismatch. \n expected:\n"+expected.toCompactString()+"\n actual:\n" +actual.toCompactString());
}
}

View File

@ -102,7 +102,7 @@ static JsonNode getFromArchive(CZipLoader & archive, const std::string & archive
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
{
auto s = data.toJson();
auto s = data.toString();
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())

View File

@ -84,7 +84,7 @@ void MapServiceMock::saveMap(const std::unique_ptr<CMap> & map, boost::filesyste
void MapServiceMock::addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
{
auto s = data.toJson();
auto s = data.toString();
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())