summaryrefslogtreecommitdiffstats
path: root/src/json_helper.cpp
blob: 07f18a8cc11b29595a5cd78d20e18a5f64c720da (plain)
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
#include "json_helper.hpp"

#include <json.h>

#include <json.hpp>

using json = nlohmann::json;

json_object *to_json(genivi::surface_properties const &s) {
   auto j = json::object({
      {"id", s.id},
      {"size", {{"width", s.size.w}, {"height", s.size.h}}},
      {"dst",
       {{"width", s.dst_rect.w},
        {"height", s.dst_rect.h},
        {"x", s.dst_rect.x},
        {"y", s.dst_rect.y}}},
      {"src",
       {{"width", s.src_rect.w},
        {"height", s.src_rect.h},
        {"x", s.src_rect.x},
        {"y", s.src_rect.y}}},
      {"visibility", s.visibility},
      {"opacity", s.opacity},
      {"orientation", s.orientation},
   });
   return json_tokener_parse(j.dump().c_str());
}

json_object *to_json(genivi::screen const *s) {
   auto o = json_object_new_object();
   json_object_object_add(o, "id", json_object_new_int(s->id));
   return o;
}

template <typename T>
json_object *to_json_(T const &s) {
   auto a = json_object_new_array();

   if (!s.empty()) {
      for (auto const &i : s) {
         json_object_array_add(a, to_json(i.second));
      }
   }

   return a;
}

json_object *to_json(genivi::controller::props_map const &s) {
   return to_json_(s);
}

json_object *to_json(std::vector<uint32_t> const &v) {
   auto a = json_object_new_array();
   for (const auto i : v) {
      json_object_array_add(a, json_object_new_int(i));
   }
   return a;
}