-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonGenerator.cpp
52 lines (46 loc) · 1.72 KB
/
JsonGenerator.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
#include <nlohmann/json.hpp>
#include <serial_interface/JsonGenerator.hpp>
#include <serial_interface/TaskIterator.hpp>
#include <serial_protocol/DeletedTaskObject.hpp>
#include <serial_protocol/ProtocolVersionObject.hpp>
#include <serial_protocol/TaskList.hpp>
#include <serial_protocol/TaskObject.hpp>
constexpr int defaultJsonIndent = 4;
template <>
std::string toJsonString<task_tracker_systems::ProtocolVersionObject>(const task_tracker_systems::ProtocolVersionObject &object)
{
auto jsonObject = nlohmann::json::object();
jsonObject["major"] = object.major;
jsonObject["minor"] = object.minor;
jsonObject["patch"] = object.patch;
return jsonObject.dump(defaultJsonIndent);
}
namespace task_tracker_systems
{
void to_json(nlohmann::json &jsonObject, const task_tracker_systems::TaskObject &object)
{
jsonObject["id"] = object.id;
jsonObject["label"] = object.label;
jsonObject["duration"] = object.duration;
}
} // namespace task_tracker_systems
template <>
std::string toJsonString<task_tracker_systems::TaskObject>(const task_tracker_systems::TaskObject &object)
{
auto jsonObject = nlohmann::json::object();
to_json(jsonObject, object);
return jsonObject.dump(defaultJsonIndent);
}
template <>
std::string toJsonString<device::TaskCollection>(const device::TaskCollection &object)
{
nlohmann::json jsonObject(TaskObjectIterator(std::begin(object)), TaskObjectIterator(std::end(object)));
return jsonObject.dump(defaultJsonIndent);
}
template <>
std::string toJsonString<task_tracker_systems::DeletedTaskObject>(const task_tracker_systems::DeletedTaskObject &object)
{
auto jsonObject = nlohmann::json::object();
jsonObject["id"] = object.id;
return jsonObject.dump(defaultJsonIndent);
}