/* * JsonNode.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #pragma once class JsonNode; typedef std::map JsonMap; typedef std::vector JsonVector; struct Bonus; class ResourceID; class DLL_LINKAGE JsonNode { public: enum JsonType { DATA_NULL, DATA_BOOL, DATA_FLOAT, DATA_STRING, DATA_VECTOR, DATA_STRUCT, DATA_INTEGER }; private: union JsonData { bool Bool; double Float; std::string* String; JsonVector* Vector; JsonMap* Struct; si64 Integer; }; JsonType type; JsonData data; public: /// free to use metadata field std::string meta; //Create empty node JsonNode(JsonType Type = DATA_NULL); //Create tree from Json-formatted input explicit JsonNode(const char * data, size_t datasize); //Create tree from JSON file explicit JsonNode(ResourceID && fileURI); explicit JsonNode(const ResourceID & fileURI); explicit JsonNode(ResourceID && fileURI, bool & isValidSyntax); //Copy c-tor JsonNode(const JsonNode ©); ~JsonNode(); void swap(JsonNode &b); JsonNode& operator =(JsonNode node); bool operator == (const JsonNode &other) const; bool operator != (const JsonNode &other) const; void setMeta(std::string metadata, bool recursive = true); /// Convert node to another type. Converting to nullptr will clear all data void setType(JsonType Type); JsonType getType() const; bool isNull() const; bool isNumber() const; /// removes all data from node and sets type to null void clear(); /// non-const accessors, node will change type on type mismatch bool & Bool(); double & Float(); si64 & Integer(); std::string & String(); JsonVector & Vector(); JsonMap & Struct(); /// const accessors, will cause assertion failure on type mismatch bool Bool() const; ///float and integer allowed double Float() const; ///only integer allowed si64 Integer() const; const std::string & String() const; const JsonVector & Vector() const; const JsonMap & Struct() const; /// returns resolved "json pointer" (string in format "/path/to/node") const JsonNode & resolvePointer(const std::string & jsonPointer) const; JsonNode & resolvePointer(const std::string & jsonPointer); /// convert json tree into specified type. Json tree must have same type as Type /// Valid types: bool, string, any numeric, map and vector /// example: convertTo< std::map< std::vector > >(); template Type convertTo() const; //operator [], for structs only - get child node by name JsonNode & operator[](std::string child); const JsonNode & operator[](std::string child) const; std::string toJson() const; template void serialize(Handler &h, const int version) { h & meta; h & type; switch (type) { break; case DATA_NULL: break; case DATA_BOOL: h & data.Bool; break; case DATA_FLOAT: h & data.Float; break; case DATA_STRING: h & data.String; break; case DATA_VECTOR: h & data.Vector; break; case DATA_STRUCT: h & data.Struct; } if(version >= 770) { if(type == DATA_INTEGER) h & data.Integer; } } }; namespace JsonUtils { /** * @brief parse short bonus format, excluding type * @note sets duration to Permament */ DLL_LINKAGE void parseTypedBonusShort(const JsonVector &source, std::shared_ptr dest); /// DLL_LINKAGE std::shared_ptr parseBonus(const JsonVector &ability_vec); DLL_LINKAGE std::shared_ptr parseBonus(const JsonNode &ability); DLL_LINKAGE bool parseBonus(const JsonNode &ability, Bonus *placement); DLL_LINKAGE void unparseBonus (JsonNode &node, const std::shared_ptr& bonus); DLL_LINKAGE void resolveIdentifier(si32 &var, const JsonNode &node, std::string name); DLL_LINKAGE void resolveIdentifier(const JsonNode &node, si32 &var); /** * @brief recursively merges source into dest, replacing identical fields * struct : recursively calls this function * arrays : each entry will be merged recursively * values : value in source will replace value in dest * null : if value in source is present but set to null it will delete entry in dest * @note this function will destroy data in source */ DLL_LINKAGE void merge(JsonNode & dest, JsonNode & source); /** * @brief recursively merges source into dest, replacing identical fields * struct : recursively calls this function * arrays : each entry will be merged recursively * values : value in source will replace value in dest * null : if value in source is present but set to null it will delete entry in dest * @note this function will preserve data stored in source by creating copy */ DLL_LINKAGE void mergeCopy(JsonNode & dest, JsonNode source); /** @brief recursively merges descendant into copy of base node * Result emulates inheritance semantic * * */ DLL_LINKAGE void inherit(JsonNode & descendant, const JsonNode & base); /** * @brief generate one Json structure from multiple files * @param files - list of filenames with parts of json structure */ DLL_LINKAGE JsonNode assembleFromFiles(std::vector files); DLL_LINKAGE JsonNode assembleFromFiles(std::vector files, bool & isValid); /// This version loads all files with same name (overridden by mods) DLL_LINKAGE JsonNode assembleFromFiles(std::string filename); /** * @brief removes all nodes that are identical to default entry in schema * @param node - JsonNode to minimize * @param schemaName - name of schema to use * @note for minimizing data must be valid against given schema */ DLL_LINKAGE void minimize(JsonNode & node, std::string schemaName); /// opposed to minimize, adds all missing, required entries that have default value DLL_LINKAGE void maximize(JsonNode & node, std::string schemaName); /** * @brief validate node against specified schema * @param node - JsonNode to check * @param schemaName - name of schema to use * @param dataName - some way to identify data (printed in console in case of errors) * @returns true if data in node fully compilant with schema */ DLL_LINKAGE bool validate(const JsonNode & node, std::string schemaName, std::string dataName); /// get schema by json URI: vcmi:# /// example: schema "vcmi:settings" is used to check user settings DLL_LINKAGE const JsonNode & getSchema(std::string URI); } namespace JsonDetail { // conversion helpers for JsonNode::convertTo (partial template function instantiation is illegal in c++) template struct JsonConvImpl; template struct JsonConvImpl { static T convertImpl(const JsonNode & node) { return T((int)node.Float()); } }; template struct JsonConvImpl { static T convertImpl(const JsonNode & node) { return node.Float(); } }; template struct JsonConverter { static Type convert(const JsonNode & node) { ///this should be triggered only for numeric types and enums static_assert(boost::mpl::or_, std::is_enum, boost::is_class >::value, "Unsupported type for JsonNode::convertTo()!"); return JsonConvImpl, boost::is_class >::value >::convertImpl(node); } }; template struct JsonConverter > { static std::map convert(const JsonNode & node) { std::map ret; for (const JsonMap::value_type & entry : node.Struct()) { ret.insert(entry.first, entry.second.convertTo()); } return ret; } }; template struct JsonConverter > { static std::set convert(const JsonNode & node) { std::set ret; for(const JsonVector::value_type & entry : node.Vector()) { ret.insert(entry.convertTo()); } return ret; } }; template struct JsonConverter > { static std::vector convert(const JsonNode & node) { std::vector ret; for (const JsonVector::value_type & entry: node.Vector()) { ret.push_back(entry.convertTo()); } return ret; } }; template<> struct JsonConverter { static std::string convert(const JsonNode & node) { return node.String(); } }; template<> struct JsonConverter { static bool convert(const JsonNode & node) { return node.Bool(); } }; } // namespace JsonDetail template Type JsonNode::convertTo() const { return JsonDetail::JsonConverter::convert(*this); }