1 #include "jsonhelper.h"
3 #include "abstractpropertytype.h"
5 #include "vehicleproperty.h"
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";
15 picojson::value amb::gvariantToJson(GVariant* value)
17 std::string type = g_variant_get_type_string(value);
22 int tempVal = GVS<int>::value(value);
23 v = picojson::value(static_cast<double>(tempVal));
27 v = picojson::value(GVS<double>::value(value));
31 v = picojson::value(static_cast<double>(GVS<uint16_t>::value(value)));
35 v = picojson::value(static_cast<double>(GVS<int16_t>::value(value)));
39 v = picojson::value(static_cast<double>(GVS<char>::value(value)));
43 v = picojson::value(static_cast<double>(GVS<uint32_t>::value(value)));
47 v = picojson::value(static_cast<double>(GVS<int64_t>::value(value)));
51 v = picojson::value(static_cast<double>(GVS<uint64_t>::value(value)));
55 v = picojson::value(GVS<bool>::value(value));
59 v = picojson::value(g_variant_get_string(value, nullptr));
63 DebugOut(DebugOut::Error) << "Unsupported type: " << type << endl;
69 const std::string amb::BasicTypes::fromSignature(const string &sig)
71 if(sig.empty()) return "";
75 if(c == G_VARIANT_CLASS_BOOLEAN)
78 else if(c == G_VARIANT_CLASS_BYTE)
81 else if(c == G_VARIANT_CLASS_INT16)
84 else if(c == G_VARIANT_CLASS_UINT16)
87 else if(c == G_VARIANT_CLASS_INT32)
90 else if(c == G_VARIANT_CLASS_UINT32)
93 else if(c == G_VARIANT_CLASS_INT64)
96 else if(c == G_VARIANT_CLASS_UINT64)
99 else if(c == G_VARIANT_CLASS_DOUBLE)
102 else if(c == G_VARIANT_CLASS_STRING)
105 else if(c == G_VARIANT_CLASS_ARRAY)
107 ///TODO support array and map
113 std::shared_ptr<AbstractPropertyType> amb::jsonToProperty(const picojson::value &json)
115 std::string name = json.get("name").to_str();
116 std::string type = json.get("type").to_str();
118 auto t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
122 bool ret = VehicleProperty::registerProperty(name, [name, type]() -> AbstractPropertyType* {
123 if(type == amb::BasicTypes::UInt16Str)
125 return new BasicPropertyType<uint16_t>(name, 0);
127 else if(type == amb::BasicTypes::Int16Str)
129 return new BasicPropertyType<int16_t>(name, 0);
131 else if(type == amb::BasicTypes::UInt32Str)
133 return new BasicPropertyType<uint32_t>(name, 0);
135 else if(type == amb::BasicTypes::Int32Str)
137 return new BasicPropertyType<int32_t>(name, 0);
139 else if(type == amb::BasicTypes::StringStr)
141 return new StringPropertyType(name);
143 else if(type == amb::BasicTypes::DoubleStr)
145 return new BasicPropertyType<double>(name, 0);
147 else if(type == amb::BasicTypes::BooleanStr)
149 return new BasicPropertyType<bool>(name, false);
151 DebugOut(DebugOut::Warning) << "Unknown or unsupported type: " << type << endl;
157 DebugOut(DebugOut::Error) << "failed to register property: " << name << endl;
161 t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
166 return std::shared_ptr<AbstractPropertyType>(t);
169 const string amb::BasicTypes::fromAbstractProperty(AbstractPropertyType *property)
171 return fromSignature(property->signature());
175 GVariant *amb::jsonToGVariant(const picojson::value & value, const std::string& type)
177 GVariant* v = nullptr;
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());
198 DebugOut(DebugOut::Error) << "Unsupported type: " << type << endl;