File: JsonSerializer.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (183 lines) | stat: -rw-r--r-- 5,490 bytes parent folder | download
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
/*
 * JsonSerializer.cpp, 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
 *
 */
#include "StdInc.h"
#include "JsonSerializer.h"

VCMI_LIB_NAMESPACE_BEGIN

JsonSerializer::JsonSerializer(const IInstanceResolver * instanceResolver_, JsonNode & root_):
	JsonTreeSerializer(instanceResolver_, &root_, true, false)
{

}

void JsonSerializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
{
	if(!boost::logic::indeterminate(value))
		currentObject->operator[](fieldName).Bool() = (bool)value;
}

void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
{
	if(!defaultValue || defaultValue.value() != value)
	{
		std::string identifier = encoder(value);
		serializeString(fieldName, identifier);
	}
}

void JsonSerializer::serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue)
{
	if(!defaultValue || !vstd::isAlmostEqual(defaultValue.value(), value))
		currentObject->operator[](fieldName).Float() = value;
}

void JsonSerializer::serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue)
{
	if(!defaultValue || defaultValue.value() != value)
		currentObject->operator[](fieldName).Integer() = value;
}

void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
{
	if(!defaultValue || defaultValue.value() != value)
		currentObject->operator[](fieldName).String() = enumMap.at(value);
}

void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
{
	if(value.empty())
		return;

	JsonVector & data = currentObject->operator[](fieldName).Vector();
	data.reserve(value.size());

	for(const si32 rawId : value)
		data.emplace_back(rawId);
}

void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<std::string> & value)
{
	if(value.empty())
		return;

	JsonVector & data = currentObject->operator[](fieldName).Vector();
	data.reserve(value.size());

	for(const auto & rawId : value)
		data.emplace_back(rawId);
}

void JsonSerializer::serializeInternal(std::string & value)
{
	currentObject->String() = value;
}

void JsonSerializer::serializeInternal(int64_t & value)
{
	currentObject->Integer() = value;
}

void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value)
{
	if(standard == value)
		return;

	writeLICPart(fieldName, "anyOf", encoder, value);
}

void JsonSerializer::serializeLIC(const std::string & fieldName, LICSet & value)
{
	if(value.any != value.standard)
		writeLICPart(fieldName, "anyOf", value.encoder, value.any);

	writeLICPart(fieldName, "allOf", value.encoder, value.all);
	writeLICPart(fieldName, "noneOf", value.encoder, value.none);
}

void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
{
	if(!value.empty())
		currentObject->operator[](fieldName).String() = value;
}

void JsonSerializer::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
{
	if(!defaultValue || value != defaultValue.value())
		currentObject->operator[](fieldName) = value;
}

void JsonSerializer::pushStruct(const std::string & fieldName)
{
	JsonTreeSerializer::pushStruct(fieldName);
	currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
}

void JsonSerializer::pushArray(const std::string & fieldName)
{
	JsonTreeSerializer::pushArray(fieldName);
	currentObject->setType(JsonNode::JsonType::DATA_VECTOR);
}

void JsonSerializer::pushArrayElement(const size_t index)
{
	JsonTreeSerializer::pushArrayElement(index);
	currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
}

void JsonSerializer::resizeCurrent(const size_t newSize, JsonNode::JsonType type)
{
	currentObject->Vector().resize(newSize);
	if(type != JsonNode::JsonType::DATA_NULL)
	{
		for(JsonNode & n : currentObject->Vector())
			if(n.getType() == JsonNode::JsonType::DATA_NULL)
				n.setType(type);
	}
}

void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::vector<bool> & data)
{
	std::vector<std::string> buf;
	buf.reserve(data.size());

	for(si32 idx = 0; idx < data.size(); idx++)
		if(data[idx])
			buf.push_back(encoder(idx));

	writeLICPartBuffer(fieldName, partName, buf);
}

void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::set<si32> & data)
{
	std::vector<std::string> buf;
	buf.reserve(data.size());

	for(const si32 item : data)
		buf.push_back(encoder(item));

	writeLICPartBuffer(fieldName, partName, buf);
}

void JsonSerializer::writeLICPartBuffer(const std::string & fieldName, const std::string & partName, std::vector<std::string> & buffer)
{
	if(!buffer.empty())
	{
		std::sort(buffer.begin(), buffer.end());

		auto & target = currentObject->operator[](fieldName)[partName].Vector();

		for(auto & s : buffer)
			target.emplace_back(s);
	}
}


VCMI_LIB_NAMESPACE_END