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
|
/*
* 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
class JsonNode;
class JsonSerializeFormat;
class JsonStructSerializer: public boost::noncopyable
{
public:
JsonStructSerializer(JsonStructSerializer && other);
virtual ~JsonStructSerializer();
JsonStructSerializer enterStruct(const std::string & fieldName);
JsonNode & get();
JsonSerializeFormat * operator->();
private:
JsonStructSerializer(JsonSerializeFormat & owner_, const std::string & fieldName);
JsonStructSerializer(JsonStructSerializer & parent, const std::string & fieldName);
bool restoreState;
JsonSerializeFormat & owner;
JsonNode * parentNode;
JsonNode * thisNode;
friend class JsonSerializeFormat;
};
class JsonSerializeFormat: public boost::noncopyable
{
public:
///user-provided callback to resolve string identifier
///returns resolved identifier or -1 on error
typedef std::function<si32(const std::string &)> TDecoder;
///user-provided callback to get string identifier
///may assume that object index is valid
typedef std::function<std::string(si32)> TEncoder;
struct LIC
{
LIC(const std::vector<bool> & Standard, const TDecoder & Decoder, const TEncoder & Encoder);
const std::vector<bool> & standard;
const TDecoder & decoder;
const TEncoder & encoder;
std::vector<bool> all, any, none;
};
const bool saving;
JsonSerializeFormat() = delete;
virtual ~JsonSerializeFormat() = default;
JsonNode & getRoot()
{
return * root;
};
JsonNode & getCurrent()
{
return * current;
};
JsonStructSerializer enterStruct(const std::string & fieldName);
template <typename T>
void serializeBool(const std::string & fieldName, const T trueValue, const T falseValue, T & value)
{
bool temp = (value == trueValue);
serializeBool(fieldName, temp);
if(!saving)
value = temp ? trueValue : falseValue;
}
virtual void serializeBool(const std::string & fieldName, bool & value) = 0;
virtual void serializeEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value) = 0;
/** @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::vector<bool> & standard, std::vector<bool> & value) = 0;
/** @brief Complete serialization of Logical identifier condition
*/
virtual void serializeLIC(const std::string & fieldName, LIC & value) = 0;
template <typename T>
void serializeNumericEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const T defaultValue, T & value)
{
si32 temp = value;
serializeIntEnum(fieldName, enumMap, defaultValue, temp);
if(!saving)
value = temp;
};
template <typename T>
void serializeNumeric(const std::string & fieldName, T & value)
{
double temp = value;
serializeFloat(fieldName, temp);
if(!saving)
value = temp;
};
virtual void serializeString(const std::string & fieldName, std::string & value) = 0;
template <typename T>
void serializeId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const T & defaultValue, T & value)
{
const si32 tempDefault = defaultValue.num;
si32 tempValue = value.num;
serializeIntId(fieldName, decoder, encoder, tempDefault, tempValue);
if(!saving)
value = T(tempValue);
}
protected:
JsonNode * root;
JsonNode * current;
JsonSerializeFormat(JsonNode & root_, const bool saving_);
virtual void serializeFloat(const std::string & fieldName, double & value) = 0;
virtual void serializeIntEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const si32 defaultValue, si32 & value) = 0;
virtual void serializeIntId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const si32 defaultValue, si32 & value) = 0;
private:
friend class JsonStructSerializer;
};
|