Move all nlohmann::json to json_helper.cpp
[staging/windowmanager.git] / src / json_helper.cpp
1 #include "json_helper.hpp"
2
3 #include <json.h>
4
5 #include <json.hpp>
6
7 using json = nlohmann::json;
8
9 template <typename T>
10 json_object *to_json_(T const *s) {
11    auto j = json::object({
12       {"id", s->id},
13       {"size", {{"width", s->size.w}, {"height", s->size.h}}},
14       {"dst",
15        {{"width", s->dst_rect.w},
16         {"height", s->dst_rect.h},
17         {"x", s->dst_rect.x},
18         {"y", s->dst_rect.y}}},
19       {"src",
20        {{"width", s->src_rect.w},
21         {"height", s->src_rect.h},
22         {"x", s->src_rect.x},
23         {"y", s->src_rect.y}}},
24       {"visibility", s->visibility},
25       {"opacity", s->opacity},
26       {"orientation", s->orientation},
27    });
28    return json_tokener_parse(j.dump().c_str());
29 }
30
31 json_object *to_json(genivi::surface const *s) { return to_json_(s); }
32
33 json_object *to_json(genivi::layer const *l) { return to_json_(l); }
34
35 json_object *to_json(genivi::screen const *s) {
36    auto o = json_object_new_object();
37    json_object_object_add(o, "id", json_object_new_int(s->id));
38    return o;
39 }
40
41 template <typename T>
42 json_object *to_json_(T const &s) {
43    auto a = json_object_new_array();
44
45    if (!s.empty()) {
46       for (auto const &i : s) {
47          json_object_array_add(a, to_json(i.second.get()));
48       }
49    }
50
51    return a;
52 }
53
54 json_object *to_json(genivi::controller::surface_map_type const &s) {
55    return to_json_(s);
56 }
57
58 json_object *to_json(genivi::controller::layer_map_type const &l) {
59    return to_json_(l);
60 }
61
62 json_object *to_json(genivi::controller::screen_map_type const &s) {
63    return to_json_(s);
64 }
65
66 json_object *to_json(std::vector<uint32_t> const &v) {
67    auto a = json_object_new_array();
68    for (const auto i : v) {
69       json_object_array_add(a, json_object_new_int(i));
70    }
71    return a;
72 }