2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2024-02-11 23:09:01 +02:00
|
|
|
|
|
|
|
#include "../filesystem/ResourcePath.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class JsonNode;
|
|
|
|
using JsonMap = std::map<std::string, JsonNode>;
|
|
|
|
using JsonVector = std::vector<JsonNode>;
|
|
|
|
|
2024-02-19 17:46:26 +02:00
|
|
|
struct DLL_LINKAGE JsonParsingSettings
|
2024-02-13 18:08:35 +02:00
|
|
|
{
|
|
|
|
enum class JsonFormatMode
|
|
|
|
{
|
|
|
|
JSON, // strict implementation of json format
|
|
|
|
JSONC, // json format that also allows comments that start from '//'
|
2024-02-13 19:07:51 +02:00
|
|
|
JSON5 // Partial support of 'json5' format
|
2024-02-13 18:08:35 +02:00
|
|
|
};
|
|
|
|
|
2024-02-13 19:07:51 +02:00
|
|
|
JsonFormatMode mode = JsonFormatMode::JSON5;
|
2024-02-13 18:08:35 +02:00
|
|
|
|
|
|
|
/// Maximum depth of elements
|
|
|
|
uint32_t maxDepth = 30;
|
|
|
|
|
|
|
|
/// If set to true, parser will throw on any encountered error
|
|
|
|
bool strict = false;
|
|
|
|
};
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
class DLL_LINKAGE JsonNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class JsonType
|
|
|
|
{
|
|
|
|
DATA_NULL,
|
|
|
|
DATA_BOOL,
|
|
|
|
DATA_FLOAT,
|
|
|
|
DATA_STRING,
|
|
|
|
DATA_VECTOR,
|
|
|
|
DATA_STRUCT,
|
|
|
|
DATA_INTEGER
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2024-02-13 13:18:10 +02:00
|
|
|
using JsonData = std::variant<std::monostate, bool, double, std::string, JsonVector, JsonMap, int64_t>;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
JsonData data;
|
|
|
|
|
2024-02-13 14:34:16 +02:00
|
|
|
/// Mod-origin of this particular field
|
|
|
|
std::string modScope;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 15:20:08 +02:00
|
|
|
bool overrideFlag = false;
|
2024-02-13 19:16:35 +02:00
|
|
|
|
2024-02-13 15:20:08 +02:00
|
|
|
public:
|
2024-02-13 13:18:10 +02:00
|
|
|
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);
|
2024-02-29 17:33:00 +02:00
|
|
|
explicit JsonNode(const char * string);
|
2024-02-13 13:18:10 +02:00
|
|
|
explicit JsonNode(const std::string & string);
|
|
|
|
|
|
|
|
/// Create tree from Json-formatted input
|
2024-02-02 01:27:19 +02:00
|
|
|
explicit JsonNode(const std::byte * data, size_t datasize);
|
2024-02-13 18:08:35 +02:00
|
|
|
explicit JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings);
|
2024-02-13 13:18:10 +02:00
|
|
|
|
|
|
|
/// Create tree from JSON file
|
2023-10-19 16:19:09 +02:00
|
|
|
explicit JsonNode(const JsonPath & fileURI);
|
2024-02-13 22:19:24 +02:00
|
|
|
explicit JsonNode(const JsonPath & fileURI, const JsonParsingSettings & parserSettings);
|
2024-02-13 13:18:10 +02:00
|
|
|
explicit JsonNode(const JsonPath & fileURI, const std::string & modName);
|
2023-10-19 16:19:09 +02:00
|
|
|
explicit JsonNode(const JsonPath & fileURI, bool & isValidSyntax);
|
|
|
|
|
2024-02-13 19:16:35 +02:00
|
|
|
bool operator==(const JsonNode & other) const;
|
|
|
|
bool operator!=(const JsonNode & other) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 14:34:16 +02:00
|
|
|
const std::string & getModScope() const;
|
|
|
|
void setModScope(const std::string & metadata, bool recursive = true);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 15:20:08 +02:00
|
|
|
void setOverrideFlag(bool value);
|
|
|
|
bool getOverrideFlag() const;
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
/// 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;
|
|
|
|
bool isString() const;
|
|
|
|
bool isVector() const;
|
|
|
|
bool isStruct() const;
|
|
|
|
/// true if node contains not-null data that cannot be extended via merging
|
|
|
|
/// used for generating common base node from multiple nodes (e.g. bonuses)
|
|
|
|
bool containsBaseData() const;
|
|
|
|
bool isCompact() const;
|
|
|
|
/// removes all data from node and sets type to null
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/// returns bool or bool equivalent of string value if 'success' is true, or false otherwise
|
|
|
|
bool TryBoolFromString(bool & success) const;
|
|
|
|
|
|
|
|
/// 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<int> > >();
|
|
|
|
template<typename Type>
|
|
|
|
Type convertTo() const;
|
|
|
|
|
|
|
|
//operator [], for structs only - get child node by name
|
|
|
|
JsonNode & operator[](const std::string & child);
|
|
|
|
const JsonNode & operator[](const std::string & child) const;
|
|
|
|
|
|
|
|
JsonNode & operator[](size_t child);
|
2024-02-13 19:16:35 +02:00
|
|
|
const JsonNode & operator[](size_t child) const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-12 01:22:16 +02:00
|
|
|
std::string toCompactString() const;
|
|
|
|
std::string toString() const;
|
|
|
|
std::vector<std::byte> toBytes() const;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 19:16:35 +02:00
|
|
|
template<typename Handler>
|
|
|
|
void serialize(Handler & h)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-13 14:34:16 +02:00
|
|
|
h & modScope;
|
2024-02-13 15:20:08 +02:00
|
|
|
|
2024-02-13 19:16:35 +02:00
|
|
|
if(h.version >= Handler::Version::JSON_FLAGS)
|
2024-02-13 15:20:08 +02:00
|
|
|
{
|
|
|
|
h & overrideFlag;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<std::string> oldFlags;
|
|
|
|
h & oldFlags;
|
|
|
|
}
|
2023-10-22 17:36:41 +02:00
|
|
|
h & data;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace JsonDetail
|
|
|
|
{
|
|
|
|
|
2024-02-13 13:51:24 +02:00
|
|
|
inline void convert(bool & value, const JsonNode & node)
|
|
|
|
{
|
|
|
|
value = node.Bool();
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 13:51:24 +02:00
|
|
|
template<typename T>
|
|
|
|
auto convert(T & value, const JsonNode & node) -> std::enable_if_t<std::is_integral_v<T>>
|
|
|
|
{
|
|
|
|
value = node.Integer();
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 13:51:24 +02:00
|
|
|
template<typename T>
|
|
|
|
auto convert(T & value, const JsonNode & node) -> std::enable_if_t<std::is_floating_point_v<T>>
|
|
|
|
{
|
|
|
|
value = node.Float();
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 13:51:24 +02:00
|
|
|
inline void convert(std::string & value, const JsonNode & node)
|
|
|
|
{
|
|
|
|
value = node.String();
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 19:16:35 +02:00
|
|
|
template<typename Type>
|
|
|
|
void convert(std::map<std::string, Type> & value, const JsonNode & node)
|
2024-02-13 13:51:24 +02:00
|
|
|
{
|
|
|
|
value.clear();
|
2024-02-13 19:16:35 +02:00
|
|
|
for(const JsonMap::value_type & entry : node.Struct())
|
2024-02-13 13:51:24 +02:00
|
|
|
value.insert(entry.first, entry.second.convertTo<Type>());
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 19:16:35 +02:00
|
|
|
template<typename Type>
|
2024-02-13 13:51:24 +02:00
|
|
|
void convert(std::set<Type> & value, const JsonNode & node)
|
|
|
|
{
|
|
|
|
value.clear();
|
|
|
|
for(const JsonVector::value_type & entry : node.Vector())
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-13 13:51:24 +02:00
|
|
|
value.insert(entry.convertTo<Type>());
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-02-13 19:16:35 +02:00
|
|
|
template<typename Type>
|
2024-02-13 13:51:24 +02:00
|
|
|
void convert(std::vector<Type> & value, const JsonNode & node)
|
|
|
|
{
|
|
|
|
value.clear();
|
|
|
|
for(const JsonVector::value_type & entry : node.Vector())
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-13 13:51:24 +02:00
|
|
|
value.push_back(entry.convertTo<Type>());
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
Type JsonNode::convertTo() const
|
|
|
|
{
|
2024-02-13 13:51:24 +02:00
|
|
|
Type result;
|
|
|
|
JsonDetail::convert(result, *this);
|
|
|
|
return result;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|