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
|
#include "metrics/eye_serdes.hpp"
#include "coding/reader.hpp"
#include "coding/serdes_json.hpp"
#include "coding/write_to_sink.hpp"
#include "coding/writer.hpp"
#include "geometry/point2d.hpp"
#include "base/logging.hpp"
#include <sstream>
#include <string>
#include <type_traits>
namespace
{
struct MapObjectEvent
{
DECLARE_VISITOR(visitor(m_bestPoiType, "best_type"), visitor(m_poiPos, "pos"),
visitor(m_readableName, "readable_name"), visitor(m_defaultName, "default_name"),
visitor(m_event, "event"));
std::string m_bestPoiType;
m2::PointD m_poiPos;
std::string m_defaultName;
std::string m_readableName;
eye::MapObject::Event m_event;
};
} // namespace
namespace eye
{
// static
void Serdes::SerializeInfo(Info const & info, std::vector<int8_t> & result)
{
result.clear();
using Sink = MemWriter<std::vector<int8_t>>;
Sink writer(result);
WriteToSink(writer, static_cast<int8_t>(Info::GetVersion()));
coding::SerializerJson<Sink> ser(writer);
ser(info);
}
// static
void Serdes::DeserializeInfo(std::vector<int8_t> const & bytes, Info & result)
{
MemReader reader(bytes.data(), bytes.size());
NonOwningReaderSource source(reader);
auto version = static_cast<int8_t>(Version::Unknown);
ReadPrimitiveFromSource(source, version);
if (version == static_cast<int8_t>(Version::V0))
{
try
{
// TODO: Use temporary object InfoV0 and implement method to convert it to Info,
// TODO: when InfoV1 will be implemented.
coding::DeserializerJson des(source);
des(result);
}
catch (base::Json::Exception & ex)
{
LOG(LERROR, ("Cannot deserialize eye file. Exception:", ex.Msg(), "Version:", version,
"File content:", std::string(bytes.begin(), bytes.end())));
}
return;
}
MYTHROW(UnknownVersion, ("Unknown data version:", static_cast<int>(version)));
}
// static
void Serdes::SerializeMapObjects(MapObjects const & mapObjects, std::vector<int8_t> & result)
{
result.clear();
using Sink = MemWriter<std::vector<int8_t>>;
Sink writer(result);
std::string const nextLine = "\n";
MapObjectEvent event;
mapObjects.ForEach([&writer, &event, &nextLine](MapObject const & item)
{
for (auto const & poiEvent : item.GetEvents())
{
// Additional scope is added because of the coding::SerializerJson dumps result at destruction.
{
coding::SerializerJson<Sink> ser(writer);
event.m_bestPoiType = item.GetBestType();
event.m_poiPos = item.GetPos();
event.m_defaultName = item.GetDefaultName();
event.m_readableName = item.GetReadableName();
event.m_event = poiEvent;
ser(event);
}
writer.Write(nextLine.data(), nextLine.size());
}
});
}
// static
void Serdes::DeserializeMapObjects(std::vector<int8_t> const & bytes, MapObjects & result)
{
MemReader reader(bytes.data(), bytes.size());
NonOwningReaderSource source(reader);
std::string tmp(bytes.begin(), bytes.end());
std::istringstream is(tmp);
std::string eventString;
MapObjectEvent event;
MapObject poi;
try
{
while (getline(is, eventString))
{
if (eventString.empty())
return;
coding::DeserializerJson des(eventString);
des(event);
poi.SetBestType(event.m_bestPoiType);
poi.SetPos(event.m_poiPos);
poi.SetDefaultName(event.m_defaultName);
poi.SetReadableName(event.m_readableName);
bool found = false;
result.ForEachInRect(poi.GetLimitRect(), [&found, &poi, &event](MapObject const & item)
{
if (item != poi)
return;
if (!found)
found = true;
item.GetEditableEvents().push_back(event.m_event);
});
if (!found)
{
poi.GetEditableEvents().push_back(event.m_event);
result.Add(poi);
}
}
}
catch (base::Json::Exception & ex)
{
LOG(LERROR, ("Cannot deserialize map objects. Exception:", ex.Msg(), ". Event string:",
eventString, ". Content:", std::string(bytes.begin(), bytes.end())));
}
}
// static
void Serdes::SerializeMapObjectEvent(MapObject const & poi, MapObject::Event const & poiEvent,
std::vector<int8_t> & result)
{
result.clear();
using Sink = MemWriter<std::vector<int8_t>>;
Sink writer(result);
coding::SerializerJson<Sink> ser(writer);
std::string const nextLine = "\n";
MapObjectEvent event;
event.m_bestPoiType = poi.GetBestType();
event.m_poiPos = poi.GetPos();
event.m_defaultName = poi.GetDefaultName();
event.m_readableName = poi.GetReadableName();
event.m_event = poiEvent;
ser(event);
writer.Write(nextLine.data(), nextLine.size());
}
} // namespace eye
|