1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
/*
* JsonSerializeFormat.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
#include "../constants/IdentifierBase.h"
#include "../json/JsonNode.h"
#include "../modding/IdentifierStorage.h"
#include "../modding/ModScope.h"
#include "../VCMI_Lib.h"
VCMI_LIB_NAMESPACE_BEGIN
class JsonSerializeFormat;
class JsonStructSerializer;
class JsonArraySerializer;
class DLL_LINKAGE IInstanceResolver
{
public:
virtual ~IInstanceResolver(){};
virtual si32 decode(const std::string & identifier) const = 0;
virtual std::string encode(si32 identifier) const = 0;
};
class DLL_LINKAGE JsonSerializeHelper: public boost::noncopyable
{
public:
JsonSerializeHelper(JsonSerializeHelper && other) noexcept;
virtual ~JsonSerializeHelper();
JsonSerializeFormat * operator->();
protected:
JsonSerializeHelper(JsonSerializeFormat * owner_);
JsonSerializeFormat * owner;
private:
bool restoreState;
};
class DLL_LINKAGE JsonStructSerializer: public JsonSerializeHelper
{
public:
JsonStructSerializer(JsonStructSerializer && other) noexcept;
protected:
JsonStructSerializer(JsonSerializeFormat * owner_);
friend class JsonSerializeFormat;
friend class JsonArraySerializer;
};
class DLL_LINKAGE JsonArraySerializer: public JsonSerializeHelper
{
public:
JsonArraySerializer(JsonArraySerializer && other) noexcept;
JsonStructSerializer enterStruct(const size_t index);
JsonArraySerializer enterArray(const size_t index);
template <typename Container>
void syncSize(Container & c, JsonNode::JsonType type = JsonNode::JsonType::DATA_NULL);
///Anything int64-convertible <-> Json integer
template <typename T>
void serializeInt(const size_t index, T & value);
///String <-> Json string
void serializeString(const size_t index, std::string & value);
///vector of anything int-convertible <-> Json vector of integers
template<typename T>
void serializeArray(std::vector<T> & value)
{
syncSize(value, JsonNode::JsonType::DATA_STRUCT);
for(size_t idx = 0; idx < size(); idx++)
serializeInt(idx, value[idx]);
}
///vector of strings <-> Json vector of strings
void serializeArray(std::vector<std::string> & value)
{
syncSize(value, JsonNode::JsonType::DATA_STRUCT);
for(size_t idx = 0; idx < size(); idx++)
serializeString(idx, value[idx]);
}
///vector of anything with custom serializing function <-> Json vector of structs
template <typename Element>
void serializeStruct(std::vector<Element> & value, std::function<void(JsonSerializeFormat&, Element&)> serializer)
{
syncSize(value, JsonNode::JsonType::DATA_STRUCT);
for(size_t idx = 0; idx < size(); idx++)
{
auto s = enterStruct(idx);
serializer(*owner, value[idx]);
}
}
///vector of serializable <-> Json vector of structs
template <typename Element>
void serializeStruct(std::vector<Element> & value)
{
serializeStruct<Element>(value, [](JsonSerializeFormat & h, Element & e){e.serializeJson(h);});
}
void resize(const size_t newSize);
void resize(const size_t newSize, JsonNode::JsonType type);
size_t size() const;
protected:
JsonArraySerializer(JsonSerializeFormat * owner_);
friend class JsonSerializeFormat;
private:
const JsonNode * thisNode;
void serializeInt64(const size_t index, int64_t & value);
};
class DLL_LINKAGE JsonSerializeFormat: public boost::noncopyable
{
public:
///user-provided callback to resolve string identifier
///returns resolved identifier or -1 on error
using TDecoder = std::function<si32(const std::string &)>;
///user-provided callback to get string identifier
///may assume that object index is valid
using TEncoder = std::function<std::string(si32)>;
struct LICSet
{
LICSet(const std::set<si32> & Standard, TDecoder Decoder, TEncoder Encoder);
const std::set<si32> & standard;
const TDecoder decoder;
const TEncoder encoder;
std::set<si32> all;
std::set<si32> any;
std::set<si32> none;
};
const bool saving;
const bool updating;
JsonSerializeFormat() = delete;
virtual ~JsonSerializeFormat() = default;
virtual const JsonNode & getCurrent() = 0;
JsonStructSerializer enterStruct(const std::string & fieldName);
JsonArraySerializer enterArray(const std::string & fieldName);
///Anything comparable <-> Json bool
template <typename T>
void serializeBool(const std::string & fieldName, T & value, const T trueValue, const T falseValue, const T defaultValue)
{
boost::logic::tribool temp(boost::logic::indeterminate);
if(value == defaultValue)
;//leave as indeterminate
else if(value == trueValue)
temp = true;
else if(value == falseValue)
temp = false;
serializeInternal(fieldName, temp);
if(!saving)
{
if(boost::logic::indeterminate(temp))
value = defaultValue;
else
value = temp ? trueValue : falseValue;
}
}
///bool <-> Json bool
void serializeBool(const std::string & fieldName, bool & value);
void serializeBool(const std::string & fieldName, bool & value, const bool defaultValue);
///tribool <-> Json bool
void serializeBool(const std::string & fieldName, boost::logic::tribool & value)
{
serializeInternal(fieldName, value);
};
/** @brief Restrictive ("anyOf") simple serialization of Logical identifier condition, simple deserialization (allOf=anyOf)
*
* @param fieldName
* @param decoder resolve callback, should report errors itself and do not throw
* @param encoder encode callback, should report errors itself and do not throw
* @param value target value, must be resized properly
*
*/
virtual void serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value) = 0;
template<typename T>
void serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<T> & standard, std::set<T> & value)
{
std::set<int32_t> standardInt;
std::set<int32_t> valueInt;
for (auto entry : standard)
standardInt.insert(entry.getNum());
for (auto entry : value)
valueInt.insert(entry.getNum());
serializeLIC(fieldName, decoder, encoder, standardInt, valueInt);
value.clear();
for (auto entry : valueInt)
value.insert(T(entry));
}
/** @brief Complete serialization of Logical identifier condition.
* Assumes that all values are allowed by default, and standard contains them
*/
virtual void serializeLIC(const std::string & fieldName, LICSet & value) = 0;
///String <-> Json string
virtual void serializeString(const std::string & fieldName, std::string & value) = 0;
///si32-convertible enum <-> Json string enum
template <typename T>
void serializeEnum(const std::string & fieldName, T & value, const std::vector<std::string> & enumMap)
{
doSerializeInternal<T, T, si32>(fieldName, value, std::nullopt, enumMap);
};
///si32-convertible enum <-> Json string enum
template <typename T, typename U>
void serializeEnum(const std::string & fieldName, T & value, const U & defaultValue, const std::vector<std::string> & enumMap)
{
doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, enumMap);
};
template <typename T, typename U, typename C>
void serializeEnum(const std::string & fieldName, T & value, const U & defaultValue, const C & enumMap)
{
std::vector<std::string> enumMapCopy;
std::copy(std::begin(enumMap), std::end(enumMap), std::back_inserter(enumMapCopy));
doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, enumMapCopy);
};
///Anything double-convertible <-> Json double
template <typename T>
void serializeFloat(const std::string & fieldName, T & value)
{
doSerializeInternal<T, T, double>(fieldName, value, std::nullopt);
};
///Anything double-convertible <-> Json double
template <typename T, typename U>
void serializeFloat(const std::string & fieldName, T & value, const U & defaultValue)
{
doSerializeInternal<T, U, double>(fieldName, value, defaultValue);
};
///Anything int64-convertible <-> Json integer
///no default value
template <typename T>
void serializeInt(const std::string & fieldName, T & value)
{
doSerializeInternal<T, T, si64>(fieldName, value, std::nullopt);
};
///Anything int64-convertible <-> Json integer
///custom default value
template <typename T, typename U>
void serializeInt(const std::string & fieldName, T & value, const U & defaultValue)
{
doSerializeInternal<T, U, si64>(fieldName, value, defaultValue);
};
///Anything int64-convertible <-> Json integer
///default value is std::nullopt
template<typename T>
void serializeInt(const std::string & fieldName, std::optional<T> & value)
{
dispatchOptional<T, si64>(fieldName, value);
};
///si32-convertible identifier <-> Json string
template <typename T, typename U>
void serializeId(const std::string & fieldName, T & value, const U & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
{
doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, decoder, encoder);
}
///si32-convertible identifier <-> Json string
template <typename IdentifierType, typename IdentifierTypeBase = IdentifierType>
void serializeId(const std::string & fieldName, IdentifierType & value, const IdentifierTypeBase & defaultValue = IdentifierType::NONE)
{
static_assert(std::is_base_of_v<IdentifierBase, IdentifierType>, "This method can only serialize Identifier classes!");
if (saving)
{
if (value != defaultValue)
{
std::string fieldValue = IdentifierType::encode(value.getNum());
serializeString(fieldName, fieldValue);
}
}
else
{
std::string fieldValue;
serializeString(fieldName, fieldValue);
if (!fieldValue.empty())
{
VLC->identifiers()->requestIdentifier(ModScope::scopeGame(), IdentifierType::entityType(), fieldValue, [&value](int32_t index){
value = IdentifierType(index);
});
}
else
{
value = IdentifierType(defaultValue);
}
}
}
///si32-convertible identifier vector <-> Json array of string
template <typename T, typename E = T>
void serializeIdArray(const std::string & fieldName, std::vector<T> & value)
{
if (saving)
{
std::vector<std::string> fieldValue;
for(const T & vitem : value)
fieldValue.push_back(E::encode(vitem.getNum()));
serializeInternal(fieldName, fieldValue);
}
else
{
std::vector<std::string> fieldValue;
serializeInternal(fieldName, fieldValue);
value.resize(fieldValue.size());
for(size_t i = 0; i < fieldValue.size(); ++i)
{
VLC->identifiers()->requestIdentifier(ModScope::scopeGame(), E::entityType(), fieldValue[i], [&value, i](int32_t index){
value[i] = T(index);
});
}
}
}
///si32-convertible identifier set <-> Json array of string
template <typename T, typename U = T>
void serializeIdArray(const std::string & fieldName, std::set<T> & value)
{
if (saving)
{
std::vector<std::string> fieldValue;
for(const T & vitem : value)
fieldValue.push_back(U::encode(vitem.getNum()));
serializeInternal(fieldName, fieldValue);
}
else
{
std::vector<std::string> fieldValue;
serializeInternal(fieldName, fieldValue);
for(size_t i = 0; i < fieldValue.size(); ++i)
{
VLC->identifiers()->requestIdentifier(ModScope::scopeGame(), U::entityType(), fieldValue[i], [&value](int32_t index){
value.insert(T(index));
});
}
}
}
///si32-convertible instance identifier <-> Json string
template <typename T>
void serializeInstance(const std::string & fieldName, T & value, const T & defaultValue)
{
const TDecoder decoder = std::bind(&IInstanceResolver::decode, instanceResolver, _1);
const TEncoder encoder = std::bind(&IInstanceResolver::encode, instanceResolver, _1);
if (saving)
{
if (value != defaultValue)
{
std::string fieldValue = encoder(value.getNum());
serializeString(fieldName, fieldValue);
}
}
else
{
std::string fieldValue;
serializeString(fieldName, fieldValue);
if (!fieldValue.empty())
value = T(decoder(fieldValue));
else
value = T(defaultValue);
}
}
///any serializable object <-> Json struct
template <typename T>
void serializeStruct(const std::string & fieldName, T & value)
{
auto guard = enterStruct(fieldName);
value.serializeJson(*this);
}
virtual void serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue) = 0;
protected:
JsonSerializeFormat(const IInstanceResolver * instanceResolver_, const bool saving_, const bool updating_);
///bool <-> Json bool, indeterminate is default
virtual void serializeInternal(const std::string & fieldName, boost::logic::tribool & value) = 0;
///Numeric Id <-> String Id
virtual void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder) = 0;
///Numeric Id vector <-> String Id vector
virtual void serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder) = 0;
///Numeric <-> Json double
virtual void serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue) = 0;
///Numeric <-> Json integer
virtual void serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue) = 0;
///Enum/Numeric <-> Json string enum
virtual void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) = 0;
///String vector <-> Json string vector
virtual void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) = 0;
virtual void pop() = 0;
virtual void pushStruct(const std::string & fieldName) = 0;
virtual void pushArray(const std::string & fieldName) = 0;
virtual void pushArrayElement(const size_t index) = 0;
virtual void pushField(const std::string & fieldName) = 0;
virtual void resizeCurrent(const size_t newSize, JsonNode::JsonType type){};
virtual void serializeInternal(std::string & value) = 0;
virtual void serializeInternal(int64_t & value) = 0;
void readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, std::set<si32> & value) const;
private:
const IInstanceResolver * instanceResolver;
template<typename VType, typename DVType, typename IType, typename... Args>
void doSerializeInternal(const std::string & fieldName, VType & value, const std::optional<DVType> & defaultValue, Args... args)
{
const std::optional<IType> tempDefault = defaultValue ? std::optional<IType>(static_cast<IType>(defaultValue.value())) : std::nullopt;
auto temp = static_cast<IType>(value);
serializeInternal(fieldName, temp, tempDefault, args...);
if(!saving)
value = static_cast<VType>(temp);
}
template<typename VType, typename IType, typename... Args>
void dispatchOptional(const std::string & fieldName, std::optional<VType> & value, Args... args)
{
if(saving)
{
if(value)
{
auto temp = static_cast<IType>(value.value());
pushField(fieldName);
serializeInternal(temp, args...);
pop();
}
}
else
{
pushField(fieldName);
if(getCurrent().getType() == JsonNode::JsonType::DATA_NULL)
{
value = std::nullopt;
}
else
{
IType temp = IType();
serializeInternal(temp, args...);
value = std::make_optional(temp);
}
pop();
}
}
friend class JsonSerializeHelper;
friend class JsonStructSerializer;
friend class JsonArraySerializer;
};
template <typename Container>
void JsonArraySerializer::syncSize(Container & c, JsonNode::JsonType type)
{
if(owner->saving)
resize(c.size(), type);
else
c.resize(size());
}
template <typename T>
void JsonArraySerializer::serializeInt(const size_t index, T & value)
{
auto temp = static_cast<int64_t>(value);
serializeInt64(index, temp);
if (!owner->saving)
value = static_cast<T>(temp);
};
VCMI_LIB_NAMESPACE_END
|