-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_reader.cpp
303 lines (282 loc) · 10.2 KB
/
json_reader.cpp
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
#include "json_reader.h"
#include <sstream>
#include <fstream>
#include <map>
namespace json_reader
{
//------------------input-------------------------
void JsonSerialize(std::istream& input)
{
transport_catalogue::TransportCatalogue trans_cat;
const json::Dict dict = json::Load(input).GetRoot().AsDict();
const auto base_requests = dict.find("base_requests");
if (base_requests != dict.end())
{
MakeBase(trans_cat, base_requests->second.AsArray());
}
const auto render_settings = dict.find("render_settings");
renderer::MapRenderer map_renderer;
if (render_settings != dict.end())
{
SetMapRenderer(trans_cat, map_renderer, render_settings->second.AsDict());
}
const auto routing_settings = dict.find("routing_settings");
transport_router::TransportRouter trans_roter(trans_cat);
if (routing_settings != dict.end())
{
SetRoutingSettings(trans_roter, routing_settings->second.AsDict());
}
serialize::Serializer serializer(trans_cat, map_renderer, trans_roter);
const auto serialization_settings = dict.find("serialization_settings");
if (serialization_settings != dict.end())
{
serializer.Serialize(serialization_settings->second.AsDict().at("file").AsString());
}
}
void MakeBase(transport_catalogue::TransportCatalogue& trans_cat, const json::Array& arr)
{
for (const auto& request : arr)
{
const auto request_type= request.AsDict().find("type");
if (request_type!= request.AsDict().end())
{
if (request_type->second.AsString() == "Stop")
{
ReadStopData(trans_cat, request.AsDict());
}
}
}
for (const auto& request : arr)
{
const auto request_type= request.AsDict().find("type");
if (request_type!= request.AsDict().end())
{
if (request_type->second.AsString() == "Stop")
{
ReadStopDistance(trans_cat, request.AsDict());
}
}
}
for (const auto& request : arr)
{
const auto request_type= request.AsDict().find("type");
if (request_type!= request.AsDict().end())
{
if (request_type->second.AsString() == "Bus")
{
ReadBusData(trans_cat, request.AsDict());
}
}
}
}
void ReadStopData(transport_catalogue::TransportCatalogue& trans_cat, const json::Dict& dict)
{
const std::string name = dict.at("name").AsString();
const auto latitude = dict.at("latitude").AsDouble();
const auto longitude = dict.at("longitude").AsDouble();
trans_cat.AddStopDatabase(name, latitude, longitude);
}
void ReadStopDistance(transport_catalogue::TransportCatalogue& trans_cat, const json::Dict& dict)
{
const std::string from_stop_name = dict.at("name").AsString();
const json::Dict stops = dict.at("road_distances").AsDict();
for (const auto& [to_stop_name, distance] : stops)
{
trans_cat.SetDistanceBetweenStops(from_stop_name, to_stop_name, distance.AsInt());
}
}
void ReadBusData(transport_catalogue::TransportCatalogue& trans_cat, const json::Dict& dict)
{
const std::string bus_name = dict.at("name").AsString();
std::vector<std::string_view> stops;
for (const auto& stop : dict.at("stops").AsArray())
{
stops.emplace_back(stop.AsString());
}
dict.at("stops").AsArray();
const auto is_circular = dict.at("is_roundtrip").AsBool();
trans_cat.AddBusDatabase(bus_name, stops, is_circular);
}
void SetRoutingSettings(transport_router::TransportRouter& trans_roter, const json::Dict& dict)
{
trans_roter.SetRoutingSettings(dict.at("bus_wait_time").AsInt(), dict.at("bus_velocity").AsDouble());
}
//------------------render-------------------------
const svg::Color GetColor(const json::Node& color)
{
if (color.IsString())
{
return svg::Color{ color.AsString() };
}
else if (color.IsArray())
{
if (color.AsArray().size() == 3)
{
return svg::Rgb
{
static_cast<uint8_t>(color.AsArray()[0].AsInt()),
static_cast<uint8_t>(color.AsArray()[1].AsInt()),
static_cast<uint8_t>(color.AsArray()[2].AsInt())
};
}
else if (color.AsArray().size() == 4)
{
return svg::Rgba
{
static_cast<uint8_t>(color.AsArray()[0].AsInt()),
static_cast<uint8_t>(color.AsArray()[1].AsInt()),
static_cast<uint8_t>(color.AsArray()[2].AsInt()),
color.AsArray()[3].AsDouble()
};
}
}
return svg::Color();
}
void SetMapRenderer(const transport_catalogue::TransportCatalogue& trans_cat, renderer::MapRenderer& map_renderer, const json::Dict& dict)//èíèöèàëèçèðóåì ðèñîâàëêó
{
renderer::RenderSettings settings;
settings.width = dict.at("width").AsDouble();
settings.height = dict.at("height").AsDouble();
settings.padding = dict.at("padding").AsDouble();
settings.line_width = dict.at("line_width").AsDouble();
settings.stop_radius = dict.at("stop_radius").AsDouble();
settings.bus_label_font_size = dict.at("bus_label_font_size").AsInt();
settings.bus_label_offset = { dict.at("bus_label_offset").AsArray()[0].AsDouble(), dict.at("bus_label_offset").AsArray()[1].AsDouble() };//ìàññèâ èç äâóõ double
settings.stop_label_font_size = dict.at("stop_label_font_size").AsInt();
settings.stop_label_offset = { dict.at("stop_label_offset").AsArray()[0].AsDouble(), dict.at("stop_label_offset").AsArray()[1].AsDouble() };//ìàññèâ èç äâóõ double
settings.underlayer_color = GetColor(dict.at("underlayer_color"));
settings.underlayer_width = dict.at("underlayer_width").AsDouble();
for (const auto& color : dict.at("color_palette").AsArray())
{
settings.color_palette.emplace_back(GetColor(color));
}
map_renderer.SetRenderSettings(settings);
}
//------------------outnput-------------------------
void JsonDeserialize(std::istream& input, std::ostream& output)
{
transport_catalogue::TransportCatalogue trans_cat;
renderer::MapRenderer map_renderer;
const auto dict = json::Load(input).GetRoot().AsDict();
const auto serialization_settings = dict.find("serialization_settings");
serialize::Deserializer deserializer;
deserializer.DeserializeCatalogAndRenderer(trans_cat, map_renderer, serialization_settings->second.AsDict().at("file").AsString());
transport_router::TransportRouter router(trans_cat);
deserializer.DeserealizeRouter(router, serialization_settings->second.AsDict().at("file").AsString());
const auto stat_requests = dict.find("stat_requests");
if (stat_requests != dict.end())
{
MakeResponse(request_handler::RequestHandler(trans_cat, map_renderer), router, stat_requests->second.AsArray());
}
}
void MakeResponse(const request_handler::RequestHandler& request_handler, transport_router::TransportRouter& trans_roter, const json::Array& arr)
{
json::Array response;
for (const auto& request : arr)
{
const auto request_type= request.AsDict().find("type");
if (request_type!= request.AsDict().end())
{
if (request_type->second.AsString() == "Stop")
{
response.emplace_back(GetStopInfo(request_handler, request.AsDict()));
}
else if (request_type->second.AsString() == "Bus")
{
response.emplace_back(GetBusInfo(request_handler, request.AsDict()));
}
else if (request_type->second.AsString() == "Map")
{
response.emplace_back(GetMapRender(request_handler, request.AsDict()));
}
else if (request_type->second.AsString() == "Route")
{
response.emplace_back(GetRouteInfo(trans_roter, request.AsDict()));
}
}
}
json::Print(json::Document(json::Builder{}.Value(response).Build()), std::cout);
}
const json::Node GetStopInfo(const request_handler::RequestHandler& request_handler, const json::Dict& dict)
{
const std::string stop_name = dict.at("name").AsString();
const auto stop_datd = request_handler.GetStopStat(stop_name);
if (stop_datd == nullptr)
{
return json::Builder{}.StartDict()
.Key("request_id").Value(dict.at("id").AsInt())
.Key("error_message").Value("not found")
.EndDict().Build().AsDict();
}
json::Array buses;
for (auto& bus : stop_datd.value()->buses)
{
buses.push_back(static_cast<std::string>(bus));
}
return json::Builder{}.StartDict()
.Key("buses").Value(buses)
.Key("request_id").Value(dict.at("id").AsInt())
.EndDict().Build();
}
const json::Node GetBusInfo(const request_handler::RequestHandler& request_handler, const json::Dict& dict)
{
const std::string bus_name = dict.at("name").AsString();
const auto bus_data = request_handler.GetBusStat(bus_name);
return (bus_data == nullptr) ? json::Builder{}.StartDict()
.Key("request_id").Value(dict.at("id").AsInt())
.Key("error_message").Value("not found")
.EndDict().Build().AsDict()
:
json::Builder{}.StartDict()
.Key("curvature").Value(bus_data.value()->curvature)
.Key("request_id").Value(dict.at("id").AsInt())
.Key("route_length").Value(static_cast<int>(bus_data.value()->route_length))
.Key("stop_count").Value(bus_data.value()->stops_on_route)
.Key("unique_stop_count").Value(bus_data.value()->unique_stops)
.EndDict().Build();
}
const json::Node GetMapRender(const request_handler::RequestHandler& request_handler, const json::Dict& dict)
{
svg::Document render = request_handler.RenderMap();
std::ostringstream strm;
render.Render(strm);
return json::Builder{}.StartDict()
.Key("map").Value(strm.str())
.Key("request_id").Value(dict.at("id").AsInt())
.EndDict().Build();
}
const json::Node GetRouteInfo(transport_router::TransportRouter& trans_roter, const json::Dict& dict)
{
auto route_data = trans_roter.GetRoute(dict.at("from").AsString(), dict.at("to").AsString());
if (!route_data.find)
{
return json::Builder{}.StartDict()
.Key("request_id").Value(dict.at("id").AsInt())
.Key("error_message").Value("not found")
.EndDict().Build().AsDict();
}
json::Array items;
for (const auto& item : route_data.items)
{
json::Dict items_map;
if (item.type == graph::EdgeType::BUS)
{
items_map["type"] = "Bus";
items_map["bus"] = item.bus_or_stop_name;
items_map["span_count"] = item.span_count;
}
else if(item.type == graph::EdgeType::WAIT)
{
items_map["type"] = "Wait";
items_map["stop_name"] = item.bus_or_stop_name;
}
items_map["time"] = item.time;
items.push_back(items_map);
}
return json::Builder{}.StartDict()
.Key("request_id").Value(dict.at("id").AsInt())
.Key("total_time").Value(route_data.total_time)
.Key("items").Value(items)
.EndDict().Build().AsDict();
}
} // namespace json_reader