From 08a27663f9db1bdd102181b26fdada019387b3e1 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Tue, 13 Feb 2024 13:18:10 +0200 Subject: [PATCH] Reworked JsonNode constructors to more logical form --- client/render/CAnimation.cpp | 8 ++--- client/render/Graphics.cpp | 2 +- launcher/jsonutils.cpp | 2 +- launcher/updatedialog_moc.cpp | 2 +- lib/CSkillHandler.cpp | 2 +- lib/bonuses/Bonus.cpp | 11 +++---- lib/bonuses/BonusEnum.cpp | 6 ++-- lib/bonuses/BonusList.cpp | 2 +- lib/bonuses/Limiters.cpp | 42 +++++++++++++------------- lib/bonuses/Updaters.cpp | 26 ++++++++-------- lib/campaign/CampaignHandler.cpp | 2 +- lib/filesystem/Filesystem.cpp | 4 +-- lib/json/JsonNode.cpp | 35 +++++++++++++++------ lib/json/JsonNode.h | 24 ++++++++++----- lib/json/JsonUtils.cpp | 34 ++------------------- lib/json/JsonUtils.h | 6 ---- lib/mapObjects/MiscObjects.cpp | 9 ++---- lib/mapObjects/ObjectTemplate.cpp | 12 ++------ lib/mapping/MapFormatJson.cpp | 19 +++++------- lib/serializer/JsonSerializer.cpp | 18 ++--------- lib/spells/CSpellHandler.cpp | 2 +- lobby/LobbyServer.cpp | 2 +- mapeditor/Animation.cpp | 8 ++--- mapeditor/jsonutils.cpp | 2 +- mapeditor/mapsettings/translations.cpp | 6 ++-- 25 files changed, 125 insertions(+), 161 deletions(-) diff --git a/client/render/CAnimation.cpp b/client/render/CAnimation.cpp index 91ca64c0f..93549f333 100644 --- a/client/render/CAnimation.cpp +++ b/client/render/CAnimation.cpp @@ -102,7 +102,7 @@ void CAnimation::initFromJson(const JsonNode & config) std::string basepath; basepath = config["basepath"].String(); - JsonNode base(JsonNode::JsonType::DATA_STRUCT); + JsonNode base; base["margins"] = config["margins"]; base["width"] = config["width"]; base["height"] = config["height"]; @@ -114,7 +114,7 @@ void CAnimation::initFromJson(const JsonNode & config) for(const JsonNode & frame : group["frames"].Vector()) { - JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT); + JsonNode toAdd; JsonUtils::inherit(toAdd, base); toAdd["file"].String() = basepath + frame.String(); source[groupID].push_back(toAdd); @@ -129,7 +129,7 @@ void CAnimation::initFromJson(const JsonNode & config) if (source[group].size() <= frame) source[group].resize(frame+1); - JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT); + JsonNode toAdd; JsonUtils::inherit(toAdd, base); toAdd["file"].String() = basepath + node["file"].String(); source[group][frame] = toAdd; @@ -191,7 +191,7 @@ void CAnimation::init() std::unique_ptr textData(new ui8[stream->getSize()]); stream->read(textData.get(), stream->getSize()); - const JsonNode config((char*)textData.get(), stream->getSize()); + const JsonNode config(reinterpret_cast(textData.get()), stream->getSize()); initFromJson(config); } diff --git a/client/render/Graphics.cpp b/client/render/Graphics.cpp index 023a6ce55..ad8f90d92 100644 --- a/client/render/Graphics.cpp +++ b/client/render/Graphics.cpp @@ -107,7 +107,7 @@ void Graphics::initializeBattleGraphics() if(!CResourceHandler::get(mod)->existsResource(ResourcePath("config/battles_graphics.json"))) continue; - const JsonNode config(mod, JsonPath::builtin("config/battles_graphics.json")); + const JsonNode config(JsonPath::builtin("config/battles_graphics.json"), mod); //initialization of AC->def name mapping if(!config["ac_mapping"].isNull()) diff --git a/launcher/jsonutils.cpp b/launcher/jsonutils.cpp index af4ce0442..4635e9f50 100644 --- a/launcher/jsonutils.cpp +++ b/launcher/jsonutils.cpp @@ -89,7 +89,7 @@ QVariant JsonFromFile(QString filename) } const auto data = file.readAll(); - JsonNode node(data.data(), data.size()); + JsonNode node(reinterpret_cast(data.data()), data.size()); return toVariant(node); } diff --git a/launcher/updatedialog_moc.cpp b/launcher/updatedialog_moc.cpp index 5542fa903..55f4eb94f 100644 --- a/launcher/updatedialog_moc.cpp +++ b/launcher/updatedialog_moc.cpp @@ -67,7 +67,7 @@ UpdateDialog::UpdateDialog(bool calledManually, QWidget *parent): } auto byteArray = response->readAll(); - JsonNode node(byteArray.constData(), byteArray.size()); + JsonNode node(reinterpret_cast(byteArray.constData()), byteArray.size()); loadFromJson(node); }); } diff --git a/lib/CSkillHandler.cpp b/lib/CSkillHandler.cpp index 3c8a253e8..619429dc7 100644 --- a/lib/CSkillHandler.cpp +++ b/lib/CSkillHandler.cpp @@ -168,7 +168,7 @@ std::vector CSkillHandler::loadLegacyData() std::vector legacyData; for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++) { - JsonNode skillNode(JsonNode::JsonType::DATA_STRUCT); + JsonNode skillNode; skillNode["name"].String() = skillNames[id]; for(int level = 1; level < NSecondarySkill::levels.size(); level++) { diff --git a/lib/bonuses/Bonus.cpp b/lib/bonuses/Bonus.cpp index ac3d534a7..35bc72c72 100644 --- a/lib/bonuses/Bonus.cpp +++ b/lib/bonuses/Bonus.cpp @@ -26,7 +26,6 @@ #include "../TerrainHandler.h" #include "../constants/StringConstants.h" #include "../battle/BattleInfo.h" -#include "../json/JsonUtils.h" #include "../modding/ModUtility.h" VCMI_LIB_NAMESPACE_BEGIN @@ -79,13 +78,13 @@ JsonNode CAddInfo::toJsonNode() const { if(size() < 2) { - return JsonUtils::intNode(operator[](0)); + return JsonNode(operator[](0)); } else { - JsonNode node(JsonNode::JsonType::DATA_VECTOR); + JsonNode node; for(si32 value : *this) - node.Vector().push_back(JsonUtils::intNode(value)); + node.Vector().emplace_back(value); return node; } } @@ -143,7 +142,7 @@ static JsonNode additionalInfoToJson(BonusType type, CAddInfo addInfo) switch(type) { case BonusType::SPECIAL_UPGRADE: - return JsonUtils::stringNode(ModUtility::makeFullIdentifier("", "creature", CreatureID::encode(addInfo[0]))); + return JsonNode(ModUtility::makeFullIdentifier("", "creature", CreatureID::encode(addInfo[0]))); default: return addInfo.toJsonNode(); } @@ -151,7 +150,7 @@ static JsonNode additionalInfoToJson(BonusType type, CAddInfo addInfo) JsonNode Bonus::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; // only add values that might reasonably be found in config files root["type"].String() = vstd::findKey(bonusNameMap, type); if(subtype != BonusSubtypeID()) diff --git a/lib/bonuses/BonusEnum.cpp b/lib/bonuses/BonusEnum.cpp index ed5fe7c59..e48d15dcf 100644 --- a/lib/bonuses/BonusEnum.cpp +++ b/lib/bonuses/BonusEnum.cpp @@ -67,13 +67,13 @@ namespace BonusDuration } if(durationNames.size() == 1) { - return JsonUtils::stringNode(durationNames[0]); + return JsonNode(durationNames[0]); } else { - JsonNode node(JsonNode::JsonType::DATA_VECTOR); + JsonNode node; for(const std::string & dur : durationNames) - node.Vector().push_back(JsonUtils::stringNode(dur)); + node.Vector().emplace_back(dur); return node; } } diff --git a/lib/bonuses/BonusList.cpp b/lib/bonuses/BonusList.cpp index 3fbabefe3..752335044 100644 --- a/lib/bonuses/BonusList.cpp +++ b/lib/bonuses/BonusList.cpp @@ -213,7 +213,7 @@ int BonusList::valOfBonuses(const CSelector &select) const JsonNode BonusList::toJsonNode() const { - JsonNode node(JsonNode::JsonType::DATA_VECTOR); + JsonNode node; for(const std::shared_ptr & b : bonuses) node.Vector().push_back(b->toJsonNode()); return node; diff --git a/lib/bonuses/Limiters.cpp b/lib/bonuses/Limiters.cpp index 2e952a56f..e17bf3965 100644 --- a/lib/bonuses/Limiters.cpp +++ b/lib/bonuses/Limiters.cpp @@ -92,7 +92,7 @@ std::string ILimiter::toString() const JsonNode ILimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = toString(); return root; } @@ -127,11 +127,11 @@ std::string CCreatureTypeLimiter::toString() const JsonNode CCreatureTypeLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "CREATURE_TYPE_LIMITER"; - root["parameters"].Vector().push_back(JsonUtils::stringNode(creature->getJsonKey())); - root["parameters"].Vector().push_back(JsonUtils::boolNode(includeUpgrades)); + root["parameters"].Vector().emplace_back(creature->getJsonKey()); + root["parameters"].Vector().emplace_back(includeUpgrades); return root; } @@ -199,16 +199,16 @@ std::string HasAnotherBonusLimiter::toString() const JsonNode HasAnotherBonusLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; std::string typeName = vstd::findKey(bonusNameMap, type); auto sourceTypeName = vstd::findKey(bonusSourceMap, source); root["type"].String() = "HAS_ANOTHER_BONUS_LIMITER"; - root["parameters"].Vector().push_back(JsonUtils::stringNode(typeName)); + root["parameters"].Vector().emplace_back(typeName); if(isSubtypeRelevant) - root["parameters"].Vector().push_back(JsonUtils::stringNode(subtype.toString())); + root["parameters"].Vector().emplace_back(subtype.toString()); if(isSourceRelevant) - root["parameters"].Vector().push_back(JsonUtils::stringNode(sourceTypeName)); + root["parameters"].Vector().emplace_back(sourceTypeName); return root; } @@ -233,11 +233,11 @@ UnitOnHexLimiter::UnitOnHexLimiter(const std::set & applicableHexes): JsonNode UnitOnHexLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "UNIT_ON_HEXES"; for(const auto & hex : applicableHexes) - root["parameters"].Vector().push_back(JsonUtils::intNode(hex)); + root["parameters"].Vector().emplace_back(hex); return root; } @@ -278,11 +278,11 @@ std::string CreatureTerrainLimiter::toString() const JsonNode CreatureTerrainLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "CREATURE_TERRAIN_LIMITER"; auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey(); - root["parameters"].Vector().push_back(JsonUtils::stringNode(terrainName)); + root["parameters"].Vector().emplace_back(terrainName); return root; } @@ -324,10 +324,10 @@ std::string FactionLimiter::toString() const JsonNode FactionLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "FACTION_LIMITER"; - root["parameters"].Vector().push_back(JsonUtils::stringNode(VLC->factions()->getById(faction)->getJsonKey())); + root["parameters"].Vector().emplace_back(VLC->factions()->getById(faction)->getJsonKey()); return root; } @@ -354,11 +354,11 @@ std::string CreatureLevelLimiter::toString() const JsonNode CreatureLevelLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "CREATURE_LEVEL_LIMITER"; - root["parameters"].Vector().push_back(JsonUtils::intNode(minLevel)); - root["parameters"].Vector().push_back(JsonUtils::intNode(maxLevel)); + root["parameters"].Vector().emplace_back(minLevel); + root["parameters"].Vector().emplace_back(maxLevel); return root; } @@ -392,10 +392,10 @@ std::string CreatureAlignmentLimiter::toString() const JsonNode CreatureAlignmentLimiter::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "CREATURE_ALIGNMENT_LIMITER"; - root["parameters"].Vector().push_back(JsonUtils::stringNode(GameConstants::ALIGNMENT_NAMES[vstd::to_underlying(alignment)])); + root["parameters"].Vector().emplace_back(GameConstants::ALIGNMENT_NAMES[vstd::to_underlying(alignment)]); return root; } @@ -450,8 +450,8 @@ void AggregateLimiter::add(const TLimiterPtr & limiter) JsonNode AggregateLimiter::toJsonNode() const { - JsonNode result(JsonNode::JsonType::DATA_VECTOR); - result.Vector().push_back(JsonUtils::stringNode(getAggregator())); + JsonNode result; + result.Vector().emplace_back(getAggregator()); for(const auto & l : limiters) result.Vector().push_back(l->toJsonNode()); return result; diff --git a/lib/bonuses/Updaters.cpp b/lib/bonuses/Updaters.cpp index 5729574c0..5a41aa047 100644 --- a/lib/bonuses/Updaters.cpp +++ b/lib/bonuses/Updaters.cpp @@ -13,7 +13,7 @@ #include "Updaters.h" #include "Limiters.h" -#include "../json/JsonUtils.h" +#include "../json/JsonNode.h" #include "../mapObjects/CGHeroInstance.h" #include "../CStack.h" @@ -39,7 +39,7 @@ std::string IUpdater::toString() const JsonNode IUpdater::toJsonNode() const { - return JsonNode(JsonNode::JsonType::DATA_NULL); + return JsonNode(); } GrowsWithLevelUpdater::GrowsWithLevelUpdater(int valPer20, int stepSize) : valPer20(valPer20), stepSize(stepSize) @@ -69,12 +69,12 @@ std::string GrowsWithLevelUpdater::toString() const JsonNode GrowsWithLevelUpdater::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "GROWS_WITH_LEVEL"; - root["parameters"].Vector().push_back(JsonUtils::intNode(valPer20)); + root["parameters"].Vector().emplace_back(valPer20); if(stepSize > 1) - root["parameters"].Vector().push_back(JsonUtils::intNode(stepSize)); + root["parameters"].Vector().emplace_back(stepSize); return root; } @@ -98,7 +98,7 @@ std::string TimesHeroLevelUpdater::toString() const JsonNode TimesHeroLevelUpdater::toJsonNode() const { - return JsonUtils::stringNode("TIMES_HERO_LEVEL"); + return JsonNode("TIMES_HERO_LEVEL"); } ArmyMovementUpdater::ArmyMovementUpdater(): @@ -141,13 +141,13 @@ std::string ArmyMovementUpdater::toString() const JsonNode ArmyMovementUpdater::toJsonNode() const { - JsonNode root(JsonNode::JsonType::DATA_STRUCT); + JsonNode root; root["type"].String() = "ARMY_MOVEMENT"; - root["parameters"].Vector().push_back(JsonUtils::intNode(base)); - root["parameters"].Vector().push_back(JsonUtils::intNode(divider)); - root["parameters"].Vector().push_back(JsonUtils::intNode(multiplier)); - root["parameters"].Vector().push_back(JsonUtils::intNode(max)); + root["parameters"].Vector().emplace_back(base); + root["parameters"].Vector().emplace_back(divider); + root["parameters"].Vector().emplace_back(multiplier); + root["parameters"].Vector().emplace_back(max); return root; } @@ -183,7 +183,7 @@ std::string TimesStackLevelUpdater::toString() const JsonNode TimesStackLevelUpdater::toJsonNode() const { - return JsonUtils::stringNode("TIMES_STACK_LEVEL"); + return JsonNode("TIMES_STACK_LEVEL"); } std::string OwnerUpdater::toString() const @@ -193,7 +193,7 @@ std::string OwnerUpdater::toString() const JsonNode OwnerUpdater::toJsonNode() const { - return JsonUtils::stringNode("BONUS_OWNER_UPDATER"); + return JsonNode("BONUS_OWNER_UPDATER"); } std::shared_ptr OwnerUpdater::createUpdatedBonus(const std::shared_ptr & b, const CBonusSystemNode & context) const diff --git a/lib/campaign/CampaignHandler.cpp b/lib/campaign/CampaignHandler.cpp index 5fa914573..5aed7fe91 100644 --- a/lib/campaign/CampaignHandler.cpp +++ b/lib/campaign/CampaignHandler.cpp @@ -46,7 +46,7 @@ void CampaignHandler::readCampaign(Campaign * ret, const std::vector & inpu } else // text format (json) { - JsonNode jsonCampaign((const char*)input.data(), input.size()); + JsonNode jsonCampaign(reinterpret_cast(input.data()), input.size()); readHeaderFromJson(*ret, jsonCampaign, filename, modName, encoding); for(auto & scenario : jsonCampaign["scenarios"].Vector()) diff --git a/lib/filesystem/Filesystem.cpp b/lib/filesystem/Filesystem.cpp index 1fa7e9994..7b4821c5c 100644 --- a/lib/filesystem/Filesystem.cpp +++ b/lib/filesystem/Filesystem.cpp @@ -117,7 +117,7 @@ void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const Json if (filename) { auto configData = CResourceHandler::get("initial")->load(JsonPath::builtin(URI))->readAll(); - const JsonNode configInitial(reinterpret_cast(configData.first.get()), configData.second); + const JsonNode configInitial(reinterpret_cast(configData.first.get()), configData.second); filesystem->addLoader(new CMappedFileLoader(mountPoint, configInitial), false); } } @@ -212,7 +212,7 @@ void CResourceHandler::load(const std::string &fsConfigURI, bool extractArchives { auto fsConfigData = get("initial")->load(JsonPath::builtin(fsConfigURI))->readAll(); - const JsonNode fsConfig(reinterpret_cast(fsConfigData.first.get()), fsConfigData.second); + const JsonNode fsConfig(reinterpret_cast(fsConfigData.first.get()), fsConfigData.second); addFilesystem("data", ModScope::scopeBuiltin(), createFileSystem("", fsConfig["filesystem"], extractArchives)); } diff --git a/lib/json/JsonNode.cpp b/lib/json/JsonNode.cpp index 612eed572..6ed988792 100644 --- a/lib/json/JsonNode.cpp +++ b/lib/json/JsonNode.cpp @@ -56,18 +56,33 @@ class CModHandler; static const JsonNode nullNode; -JsonNode::JsonNode(JsonType Type) -{ - setType(Type); -} - -JsonNode::JsonNode(const std::byte *data, size_t datasize) - :JsonNode(reinterpret_cast(data), datasize) +JsonNode::JsonNode(bool boolean) + :data(boolean) {} -JsonNode::JsonNode(const char *data, size_t datasize) +JsonNode::JsonNode(int32_t number) + :data(static_cast(number)) +{} + +JsonNode::JsonNode(uint32_t number) + :data(static_cast(number)) +{} + +JsonNode::JsonNode(int64_t number) + :data(number) +{} + +JsonNode::JsonNode(double number) + :data(number) +{} + +JsonNode::JsonNode(const std::string & string) + :data(string) +{} + +JsonNode::JsonNode(const std::byte *data, size_t datasize) { - JsonParser parser(data, datasize); + JsonParser parser(reinterpret_cast(data), datasize); *this = parser.parse(""); } @@ -79,7 +94,7 @@ JsonNode::JsonNode(const JsonPath & fileURI) *this = parser.parse(fileURI.getName()); } -JsonNode::JsonNode(const std::string & idx, const JsonPath & fileURI) +JsonNode::JsonNode(const JsonPath & fileURI, const std::string & idx) { auto file = CResourceHandler::get(idx)->load(fileURI)->readAll(); diff --git a/lib/json/JsonNode.h b/lib/json/JsonNode.h index 138fbb918..f9a235734 100644 --- a/lib/json/JsonNode.h +++ b/lib/json/JsonNode.h @@ -37,24 +37,32 @@ public: }; private: - using JsonData = std::variant; + using JsonData = std::variant; JsonData data; public: /// free to use metadata fields std::string meta; - // meta-flags like override + /// meta-flags like override std::vector flags; - //Create empty node - JsonNode(JsonType Type = JsonType::DATA_NULL); - //Create tree from Json-formatted input - explicit JsonNode(const char * data, size_t datasize); + JsonNode() = default; + + /// Create single node with specified value + explicit JsonNode(bool boolean); + explicit JsonNode(int32_t number); + explicit JsonNode(uint32_t number); + explicit JsonNode(int64_t number); + explicit JsonNode(double number); + explicit JsonNode(const std::string & string); + + /// Create tree from Json-formatted input explicit JsonNode(const std::byte * data, size_t datasize); - //Create tree from JSON file + + /// Create tree from JSON file explicit JsonNode(const JsonPath & fileURI); - explicit JsonNode(const std::string & modName, const JsonPath & fileURI); + explicit JsonNode(const JsonPath & fileURI, const std::string & modName); explicit JsonNode(const JsonPath & fileURI, bool & isValidSyntax); bool operator == (const JsonNode &other) const; diff --git a/lib/json/JsonUtils.cpp b/lib/json/JsonUtils.cpp index 42826cf8c..643ef1488 100644 --- a/lib/json/JsonUtils.cpp +++ b/lib/json/JsonUtils.cpp @@ -273,7 +273,7 @@ JsonNode JsonUtils::intersect(const JsonNode & a, const JsonNode & b, bool prune if(a.getType() == JsonNode::JsonType::DATA_STRUCT && b.getType() == JsonNode::JsonType::DATA_STRUCT) { // intersect individual properties - JsonNode result(JsonNode::JsonType::DATA_STRUCT); + JsonNode result; for(const auto & property : a.Struct()) { if(vstd::contains(b.Struct(), property.first)) @@ -313,7 +313,7 @@ JsonNode JsonUtils::difference(const JsonNode & node, const JsonNode & base) if(node.getType() == JsonNode::JsonType::DATA_STRUCT && base.getType() == JsonNode::JsonType::DATA_STRUCT) { // subtract individual properties - JsonNode result(JsonNode::JsonType::DATA_STRUCT); + JsonNode result; for(const auto & property : node.Struct()) { if(vstd::contains(base.Struct(), property.first)) @@ -370,38 +370,10 @@ JsonNode JsonUtils::assembleFromFiles(const std::string & filename) std::unique_ptr textData(new ui8[stream->getSize()]); stream->read(textData.get(), stream->getSize()); - JsonNode section(reinterpret_cast(textData.get()), stream->getSize()); + JsonNode section(reinterpret_cast(textData.get()), stream->getSize()); merge(result, section); } return result; } -DLL_LINKAGE JsonNode JsonUtils::boolNode(bool value) -{ - JsonNode node; - node.Bool() = value; - return node; -} - -DLL_LINKAGE JsonNode JsonUtils::floatNode(double value) -{ - JsonNode node; - node.Float() = value; - return node; -} - -DLL_LINKAGE JsonNode JsonUtils::stringNode(const std::string & value) -{ - JsonNode node; - node.String() = value; - return node; -} - -DLL_LINKAGE JsonNode JsonUtils::intNode(si64 value) -{ - JsonNode node; - node.Integer() = value; - return node; -} - VCMI_LIB_NAMESPACE_END diff --git a/lib/json/JsonUtils.h b/lib/json/JsonUtils.h index ec590e1d1..33d01f888 100644 --- a/lib/json/JsonUtils.h +++ b/lib/json/JsonUtils.h @@ -88,12 +88,6 @@ namespace JsonUtils /// get schema by json URI: vcmi:# /// example: schema "vcmi:settings" is used to check user settings DLL_LINKAGE const JsonNode & getSchema(const std::string & URI); - - /// for easy construction of JsonNodes; helps with inserting primitives into vector node - DLL_LINKAGE JsonNode boolNode(bool value); - DLL_LINKAGE JsonNode floatNode(double value); - DLL_LINKAGE JsonNode stringNode(const std::string & value); - DLL_LINKAGE JsonNode intNode(si64 value); } VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index f83fbe362..943ba9356 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -220,13 +220,10 @@ void CGMine::serializeJsonOptions(JsonSerializeFormat & handler) { if(handler.saving) { - JsonNode node(JsonNode::JsonType::DATA_VECTOR); + JsonNode node; for(const auto & resID : abandonedMineResources) - { - JsonNode one(JsonNode::JsonType::DATA_STRING); - one.String() = GameConstants::RESOURCE_NAMES[resID.getNum()]; - node.Vector().push_back(one); - } + node.Vector().emplace_back(GameConstants::RESOURCE_NAMES[resID.getNum()]); + handler.serializeRaw("possibleResources", node, std::nullopt); } else diff --git a/lib/mapObjects/ObjectTemplate.cpp b/lib/mapObjects/ObjectTemplate.cpp index 6fced7254..b5170bef8 100644 --- a/lib/mapObjects/ObjectTemplate.cpp +++ b/lib/mapObjects/ObjectTemplate.cpp @@ -354,11 +354,7 @@ void ObjectTemplate::writeJson(JsonNode & node, const bool withTerrain) const JsonVector & data = node["allowedTerrains"].Vector(); for(auto type : allowedTerrains) - { - JsonNode value(JsonNode::JsonType::DATA_STRING); - value.String() = VLC->terrainTypeHandler->getById(type)->getJsonKey(); - data.push_back(value); - } + data.push_back(JsonNode(VLC->terrainTypeHandler->getById(type)->getJsonKey())); } } @@ -398,13 +394,11 @@ void ObjectTemplate::writeJson(JsonNode & node, const bool withTerrain) const for(size_t i=0; i < height; i++) { - JsonNode lineNode(JsonNode::JsonType::DATA_STRING); - - std::string & line = lineNode.String(); + std::string line; line.resize(width); for(size_t j=0; j < width; j++) line[j] = tileToChar(usedTiles[height - 1 - i][width - 1 - j]); - mask.push_back(lineNode); + mask.emplace_back(line); } if(printPriority != 0) diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index 9046234c1..ba7709e70 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -545,13 +545,10 @@ void CMapFormatJson::writeTeams(JsonSerializer & handler) for(const std::set & teamData : teamsData) { - JsonNode team(JsonNode::JsonType::DATA_VECTOR); + JsonNode team; for(const PlayerColor & player : teamData) - { - JsonNode member(JsonNode::JsonType::DATA_STRING); - member.String() = GameConstants::PLAYER_COLOR_NAMES[player.getNum()]; - team.Vector().push_back(std::move(member)); - } + team.Vector().emplace_back(GameConstants::PLAYER_COLOR_NAMES[player.getNum()]); + dest.Vector().push_back(std::move(team)); } handler.serializeRaw("teams", dest, std::nullopt); @@ -586,7 +583,7 @@ void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & void CMapFormatJson::writeTriggeredEvents(JsonSerializer & handler) { - JsonNode triggeredEvents(JsonNode::JsonType::DATA_STRUCT); + JsonNode triggeredEvents; for(const auto & event : mapHeader->triggeredEvents) writeTriggeredEvent(event, triggeredEvents[event.identifier]); @@ -657,7 +654,7 @@ void CMapFormatJson::writeDisposedHeroes(JsonSerializeFormat & handler) auto definition = definitions->enterStruct(type); - JsonNode players(JsonNode::JsonType::DATA_VECTOR); + JsonNode players; definition->serializeIdArray("availableFor", hero.players); } } @@ -812,7 +809,7 @@ JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename) auto data = loader.load(resource)->readAll(); - JsonNode res(reinterpret_cast(data.first.get()), data.second); + JsonNode res(reinterpret_cast(data.first.get()), data.second); return res; } @@ -1329,7 +1326,7 @@ void CMapSaverJson::writeTerrain() void CMapSaverJson::writeObjects() { logGlobal->trace("Saving objects"); - JsonNode data(JsonNode::JsonType::DATA_STRUCT); + JsonNode data; JsonSerializer handler(mapObjectResolver.get(), data); @@ -1343,7 +1340,7 @@ void CMapSaverJson::writeObjects() if(map->grailPos.valid()) { - JsonNode grail(JsonNode::JsonType::DATA_STRUCT); + JsonNode grail; grail["type"].String() = "grail"; grail["x"].Float() = map->grailPos.x; diff --git a/lib/serializer/JsonSerializer.cpp b/lib/serializer/JsonSerializer.cpp index 05b8e34a2..b699c2ee9 100644 --- a/lib/serializer/JsonSerializer.cpp +++ b/lib/serializer/JsonSerializer.cpp @@ -60,11 +60,7 @@ void JsonSerializer::serializeInternal(const std::string & fieldName, std::vecto data.reserve(value.size()); for(const si32 rawId : value) - { - JsonNode jsonElement(JsonNode::JsonType::DATA_STRING); - jsonElement.String() = encoder(rawId); - data.push_back(std::move(jsonElement)); - } + data.emplace_back(rawId); } void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector & value) @@ -76,11 +72,7 @@ void JsonSerializer::serializeInternal(const std::string & fieldName, std::vecto data.reserve(value.size()); for(const auto & rawId : value) - { - JsonNode jsonElement(JsonNode::JsonType::DATA_STRING); - jsonElement.String() = rawId; - data.push_back(std::move(jsonElement)); - } + data.emplace_back(rawId); } void JsonSerializer::serializeInternal(std::string & value) @@ -183,11 +175,7 @@ void JsonSerializer::writeLICPartBuffer(const std::string & fieldName, const std auto & target = currentObject->operator[](fieldName)[partName].Vector(); for(auto & s : buffer) - { - JsonNode val(JsonNode::JsonType::DATA_STRING); - std::swap(val.String(), s); - target.push_back(std::move(val)); - } + target.emplace_back(s); } } diff --git a/lib/spells/CSpellHandler.cpp b/lib/spells/CSpellHandler.cpp index df92f869a..97df5943e 100644 --- a/lib/spells/CSpellHandler.cpp +++ b/lib/spells/CSpellHandler.cpp @@ -582,7 +582,7 @@ std::vector CSpellHandler::loadLegacyData() { do { - JsonNode lineNode(JsonNode::JsonType::DATA_STRUCT); + JsonNode lineNode; const auto id = legacyData.size(); diff --git a/lobby/LobbyServer.cpp b/lobby/LobbyServer.cpp index 56f8bba3d..45af54a1a 100644 --- a/lobby/LobbyServer.cpp +++ b/lobby/LobbyServer.cpp @@ -58,7 +58,7 @@ NetworkConnectionPtr LobbyServer::findGameRoom(const std::string & gameRoomID) c void LobbyServer::sendMessage(const NetworkConnectionPtr & target, const JsonNode & json) { - target->sendPacket(json.toBytes(true)); + target->sendPacket(json.toBytes()); } void LobbyServer::sendAccountCreated(const NetworkConnectionPtr & target, const std::string & accountID, const std::string & accountCookie) diff --git a/mapeditor/Animation.cpp b/mapeditor/Animation.cpp index 5aff952da..16019a65a 100644 --- a/mapeditor/Animation.cpp +++ b/mapeditor/Animation.cpp @@ -599,7 +599,7 @@ void Animation::init() std::unique_ptr textData(new ui8[stream->getSize()]); stream->read(textData.get(), stream->getSize()); - const JsonNode config((char*)textData.get(), stream->getSize()); + const JsonNode config(reinterpret_cast(textData.get()), stream->getSize()); initFromJson(config); } @@ -610,7 +610,7 @@ void Animation::initFromJson(const JsonNode & config) std::string basepath; basepath = config["basepath"].String(); - JsonNode base(JsonNode::JsonType::DATA_STRUCT); + JsonNode base; base["margins"] = config["margins"]; base["width"] = config["width"]; base["height"] = config["height"]; @@ -622,7 +622,7 @@ void Animation::initFromJson(const JsonNode & config) for(const JsonNode & frame : group["frames"].Vector()) { - JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT); + JsonNode toAdd; JsonUtils::inherit(toAdd, base); toAdd["file"].String() = basepath + frame.String(); source[groupID].push_back(toAdd); @@ -637,7 +637,7 @@ void Animation::initFromJson(const JsonNode & config) if (source[group].size() <= frame) source[group].resize(frame+1); - JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT); + JsonNode toAdd; JsonUtils::inherit(toAdd, base); toAdd["file"].String() = basepath + node["file"].String(); source[group][frame] = toAdd; diff --git a/mapeditor/jsonutils.cpp b/mapeditor/jsonutils.cpp index 4e1a4e58c..7c8f946ac 100644 --- a/mapeditor/jsonutils.cpp +++ b/mapeditor/jsonutils.cpp @@ -96,7 +96,7 @@ QVariant JsonFromFile(QString filename) } else { - JsonNode node(data.data(), data.size()); + JsonNode node(reinterpret_cast(data.data()), data.size()); return toVariant(node); } } diff --git a/mapeditor/mapsettings/translations.cpp b/mapeditor/mapsettings/translations.cpp index ae5476a89..dd3f6e234 100644 --- a/mapeditor/mapsettings/translations.cpp +++ b/mapeditor/mapsettings/translations.cpp @@ -24,7 +24,7 @@ void Translations::cleanupRemovedItems(CMap & map) for(auto & translations : map.translations.Struct()) { - auto updateTranslations = JsonNode(JsonNode::JsonType::DATA_STRUCT); + JsonNode updateTranslations; for(auto & s : translations.second.Struct()) { for(auto part : QString::fromStdString(s.first).split('.')) @@ -44,7 +44,7 @@ void Translations::cleanupRemovedItems(CMap & map, const std::string & pattern) { for(auto & translations : map.translations.Struct()) { - auto updateTranslations = JsonNode(JsonNode::JsonType::DATA_STRUCT); + JsonNode updateTranslations; for(auto & s : translations.second.Struct()) { if(s.first.find(pattern) == std::string::npos) @@ -171,7 +171,7 @@ void Translations::on_supportedCheck_toggled(bool checked) } ui->translationsTable->blockSignals(true); ui->translationsTable->setRowCount(0); - translation = JsonNode(JsonNode::JsonType::DATA_NULL); + translation.clear(); ui->translationsTable->blockSignals(false); ui->translationsTable->setEnabled(false); }