Merge pull request #67 from tripzero/trip
[profile/ivi/automotive-message-broker.git] / lib / jsonhelper.cpp
1 #include "jsonhelper.h"
2
3 #include "abstractpropertytype.h"
4 #include "debugout.h"
5 #include "vehicleproperty.h"
6
7 const char * amb::BasicTypes::UInt16Str = "UInt16";
8 const char * amb::BasicTypes::UInt32Str = "UInt32";
9 const char * amb::BasicTypes::Int16Str = "Int16";
10 const char * amb::BasicTypes::Int32Str = "Int32";
11 const char * amb::BasicTypes::StringStr = "String";
12 const char * amb::BasicTypes::DoubleStr = "Double";
13 const char * amb::BasicTypes::BooleanStr = "Boolean";
14
15 picojson::value amb::gvariantToJson(GVariant* value)
16 {
17         std::string type = g_variant_get_type_string(value);
18         picojson::value v;
19
20         if (type == "i")
21         {
22                 int tempVal = GVS<int>::value(value);
23                 v = picojson::value(static_cast<double>(tempVal));
24         }
25         else if (type == "d")
26         {
27                 v = picojson::value(GVS<double>::value(value));
28         }
29         else if (type == "q")
30         {
31                 v = picojson::value(static_cast<double>(GVS<uint16_t>::value(value)));
32         }
33         else if (type == "n")
34         {
35                 v = picojson::value(static_cast<double>(GVS<int16_t>::value(value)));
36         }
37         else if (type == "y")
38         {
39                 v = picojson::value(static_cast<double>(GVS<char>::value(value)));
40         }
41         else if (type == "u")
42         {
43                 v = picojson::value(static_cast<double>(GVS<uint32_t>::value(value)));
44         }
45         else if (type == "x")
46         {
47                 v = picojson::value(static_cast<double>(GVS<int64_t>::value(value)));
48         }
49         else if (type == "t")
50         {
51                 v = picojson::value(static_cast<double>(GVS<uint64_t>::value(value)));
52         }
53         else if (type == "b")
54         {
55                 v = picojson::value(GVS<bool>::value(value));
56         }
57         else if (type == "s")
58         {
59                 v = picojson::value(g_variant_get_string(value, nullptr));
60         }
61         else
62         {
63                 DebugOut(DebugOut::Error) << "Unsupported type: " << type << endl;
64         }
65
66         return v;
67 }
68
69 const std::string amb::BasicTypes::fromSignature(const string &sig)
70 {
71         if(sig.empty()) return "";
72
73         char c = sig[0];
74
75         if(c == G_VARIANT_CLASS_BOOLEAN)
76                 return BooleanStr;
77
78         else if(c == G_VARIANT_CLASS_BYTE)
79                 return "";
80
81         else if(c == G_VARIANT_CLASS_INT16)
82                 return Int16Str;
83
84         else if(c == G_VARIANT_CLASS_UINT16)
85                 return UInt16Str;
86
87         else if(c == G_VARIANT_CLASS_INT32)
88                 return Int32Str;
89
90         else if(c ==  G_VARIANT_CLASS_UINT32)
91                 return UInt32Str;
92
93         else if(c == G_VARIANT_CLASS_INT64)
94                 return "";
95
96         else if(c == G_VARIANT_CLASS_UINT64)
97                 return "";
98
99         else if(c == G_VARIANT_CLASS_DOUBLE)
100                 return DoubleStr;
101
102         else if(c == G_VARIANT_CLASS_STRING)
103                 return StringStr;
104
105         else if(c == G_VARIANT_CLASS_ARRAY)
106         {
107                 ///TODO support array and map
108                 return "";
109         }
110         return "";
111 }
112
113 std::shared_ptr<AbstractPropertyType> amb::jsonToProperty(const picojson::value &json)
114 {
115         std::string name = json.get("name").to_str();
116         std::string type = json.get("type").to_str();
117
118         auto t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
119
120         if(!t)
121         {
122                 bool ret = VehicleProperty::registerProperty(name, [name, type]() -> AbstractPropertyType* {
123                         if(type == amb::BasicTypes::UInt16Str)
124                         {
125                                 return new BasicPropertyType<uint16_t>(name, 0);
126                         }
127                         else if(type == amb::BasicTypes::Int16Str)
128                         {
129                                 return new BasicPropertyType<int16_t>(name, 0);
130                         }
131                         else if(type == amb::BasicTypes::UInt32Str)
132                         {
133                                 return new BasicPropertyType<uint32_t>(name, 0);
134                         }
135                         else if(type == amb::BasicTypes::Int32Str)
136                         {
137                                 return new BasicPropertyType<int32_t>(name, 0);
138                         }
139                         else if(type == amb::BasicTypes::StringStr)
140                         {
141                                 return new StringPropertyType(name);
142                         }
143                         else if(type == amb::BasicTypes::DoubleStr)
144                         {
145                                 return new BasicPropertyType<double>(name, 0);
146                         }
147                         else if(type == amb::BasicTypes::BooleanStr)
148                         {
149                                 return new BasicPropertyType<bool>(name, false);
150                         }
151                         DebugOut(DebugOut::Warning) << "Unknown or unsupported type: " << type << endl;
152                         return nullptr;
153                 });
154
155                 if(!ret)
156                 {
157                         DebugOut(DebugOut::Error) << "failed to register property: " << name << endl;
158                         return nullptr;
159                 }
160
161                 t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
162         }
163
164         t->fromJson(json);
165
166         return std::shared_ptr<AbstractPropertyType>(t);
167 }
168
169 const string amb::BasicTypes::fromAbstractProperty(AbstractPropertyType *property)
170 {
171         return fromSignature(property->signature());
172 }
173
174
175 GVariant *amb::jsonToGVariant(const picojson::value & value, const std::string& type)
176 {
177         GVariant* v = nullptr;
178
179         if (type == "i") {
180                 v = g_variant_new(type.c_str(), (int32_t)value.get<double>());
181         } else if (type == "d") {
182                 v = g_variant_new(type.c_str(), value.get<double>());
183         } else if (type == "q") {
184                 v = g_variant_new(type.c_str(), (uint16_t)value.get<double>());
185         } else if (type == "n") {
186                 v = g_variant_new(type.c_str(), (int16_t)value.get<double>());
187         } else if (type == "u") {
188                 v = g_variant_new(type.c_str(), (uint32_t)value.get<double>());
189         } else if (type == "x") {
190                 v = g_variant_new(type.c_str(), (int64_t)value.get<double>());
191         } else if (type == "t") {
192                 v = g_variant_new(type.c_str(), (uint64_t)value.get<double>());
193         } else if (type == "b") {
194                 v = g_variant_new(type.c_str(), value.get<bool>());
195         } else if (type == "s") {
196                 v = g_variant_new(type.c_str(), value.get<std::string>().c_str());
197         } else {
198                 DebugOut(DebugOut::Error) << "Unsupported type: " << type << endl;
199         }
200
201         return v;
202 }