-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.cpp
85 lines (66 loc) · 2.19 KB
/
output.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
#include "output.h"
#include <fstream>
#include <iostream>
#include "return_code.h"
void Initialize(GeometryEngineOutput& output)
{
output.return_code = ToInt(ReturnCode::kSuccess);
output.message = "";
output.result.type = "";
output.result.marginline.num_original_points = 0;
output.result.marginline.num_samples = 0;
output.result.marginline.points.clear();
}
void to_json(nlohmann::json& j, const GeometryEngineOutput::Result::Marginline& ml)
{
j = nlohmann::json{ {"num_original_points", ml.num_original_points}, {"num_samples", ml.num_samples}, { "points", ml.points } };
}
void to_json(nlohmann::json& j, const GeometryEngineOutput::Result& r)
{
j = nlohmann::json{ {"type", r.type}, {"marginline", r.marginline} };
}
void to_json(nlohmann::json& j, const GeometryEngineOutput& geo)
{
j = nlohmann::json{ {"return_code", geo.return_code}, {"message", geo.message}, {"result", geo.result}};
}
void from_json(const nlohmann::json& j, GeometryEngineOutput::Result::Marginline& ml)
{
j.at("num_original_points").get_to(ml.num_original_points);
j.at("num_samples").get_to(ml.num_samples);
j.at("points").get_to(ml.points);
}
void from_json(const nlohmann::json& j, GeometryEngineOutput::Result& r)
{
j.at("type").get_to(r.type);
j.at("marginline").get_to(r.marginline);
}
void from_json(const nlohmann::json& j, GeometryEngineOutput& geo)
{
j.at("return_code").get_to(geo.return_code);
j.at("message").get_to(geo.message);
j.at("result").get_to(geo.result);
}
void test_output_json_00()
{
try
{
auto testing_json = "..\\tests\\output_00.json";
auto testing_result_json = "..\\tests\\output_00_result.json";
// serialization testing
std::ifstream ifs(testing_json);
if (!ifs.is_open())
{
throw std::runtime_error("Can't open file.");
}
auto j = nlohmann::json::parse(ifs);
// deserialization testing
auto gei = j.get<GeometryEngineOutput>();
nlohmann::json serialized_gei = gei;
std::ofstream ofs(testing_result_json);
ofs << serialized_gei.dump(4);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}