ambd: remove redundant code in core
[profile/ivi/automotive-message-broker.git] / lib / mappropertytype.hpp
1 #ifndef _MAPPROPERTYTYPE_H_
2 #define _MAPPROPERTYTYPE_H_
3
4
5 #include "abstractpropertytype.h"
6
7 #include <map>
8 #include <debugout.h>
9 #include "picojson.h"
10
11 template <class N>
12 class MapPropertyType: public AbstractPropertyType
13 {
14 public:
15         MapPropertyType(std::string propertyName):AbstractPropertyType(propertyName){}
16
17         void append(std::string  key, N  value)
18         {
19                 appendPriv(key, value);
20         }
21
22         AbstractPropertyType* copy()
23         {
24                 MapPropertyType<N> *t = new MapPropertyType<N>(name);
25
26                 t->setMap(mMap);
27                 t->timestamp = timestamp;
28                 t->sequence = sequence;
29                 t->sourceUuid = sourceUuid;
30                 t->name = name;
31                 t->zone = zone;
32
33                 return t;
34         }
35
36         std::string toString() const
37         {
38                 std::stringstream str;
39
40                 str<<"{";
41
42                 for(auto itr: mMap)
43                 {
44                         if(str.str() != "{")
45                                 str << ", ";
46
47
48                         str <<"\""<< itr.first <<"\":\""<<itr.second.toString()<<"\"";
49                 }
50
51                 str << "}";
52
53                 return str.str();
54         }
55
56         void fromString(std::string str)
57         {
58                 clear();
59
60                 DebugOut() << str << endl;
61
62                 picojson::value value;
63                 picojson::parse(value, str);
64
65                 std::string picojsonerr = picojson::get_last_error();
66
67                 if(!value.is<picojson::object>() || !picojsonerr.empty())
68                 {
69                         DebugOut(DebugOut::Warning) << "JSon is invalid for MapPropertyType: " << str << endl;
70                         DebugOut(DebugOut::Warning) << picojsonerr << endl;
71                         return;
72                 }
73
74                 const picojson::object& obj = value.get<picojson::object>();
75
76                 DebugOut() << "obj size: " << obj.size() << endl;
77
78                 for(auto i : obj)
79                 {
80                         std::string key = i.first;
81                         N val("");
82                         val.fromString(i.second.to_str());
83                         append(key, val);
84                 }
85         }
86
87         GVariant* toVariant()
88         {
89                 GVariantBuilder params;
90                 g_variant_builder_init(&params, G_VARIANT_TYPE_DICTIONARY);
91                 for(auto itr = mMap.begin(); itr != mMap.end(); itr++)
92                 {
93                         auto &foo = (*itr).first;
94                         g_variant_builder_add(&params,"{sv}",foo.c_str(), (*itr).second.toVariant());
95                 }
96
97                 GVariant* var =  g_variant_builder_end(&params);
98                 g_assert(var);
99                 return var;
100         }
101
102         void fromVariant(GVariant* variant)
103         {
104                 clear();
105                 gsize dictsize = g_variant_n_children(variant);
106                 for (int i=0;i<dictsize;i++)
107                 {
108                         GVariant *childvariant = g_variant_get_child_value(variant,i);
109                         gsize dictvalsize = g_variant_n_children(childvariant);
110                         if (dictvalsize == 2)
111                         {
112                                 //It is a dictionary entry
113                                 GVariant *keyvariant = g_variant_get_child_value(childvariant,0);
114                                 GVariant *valvariant = g_variant_get_child_value(childvariant,1);
115                                 std::string key = g_variant_get_string(keyvariant, nullptr);
116                                 N n = N();
117                                 GVariant *innerValue = nullptr;
118
119                                 DebugOut() << "variantType: " << g_variant_get_type_string(valvariant) << endl;
120
121                                 g_variant_get(valvariant, "v", &innerValue);
122
123                                 DebugOut() << "inner variange: " << g_variant_get_type_string(innerValue) << endl;
124
125                                 n.fromVariant(innerValue);
126                                 appendPriv(key,n);
127                         }
128                 }
129
130         }
131
132         void setMap(std::map<std::string, N> m)
133         {
134                 mMap = m;
135         }
136
137 private:
138
139         void clear()
140         {
141                 mMap.clear();
142         }
143
144         void appendPriv(std::string  key, N  value)
145         {
146                 mMap[key] = value;
147         }
148
149         std::map<std::string, N> mMap;
150 };
151
152
153 #endif