const double get_double_from_object(json_object *obj, const char *key);
const int get_boolean_from_object(json_object *obj, const char *key);
json_object *get_object_from_object(json_object *obj, const char *key);
+
+int get_property(json_object *obj, const char *key, int type, bool mandatory,
+ void *data);
#endif /* __CORE_COMMON_H__ */
return tmp;
}
+
+int get_property(json_object *obj, const char *key,
+ int type, bool mandatory, void *data)
+{
+ int ret = 0;
+ int val_int;
+ double val_double;
+ const char *val_str;
+
+ switch (type) {
+ case DATA_TYPE_INT:
+ val_int = get_int_from_object(obj, key);
+ if (val_int < 0)
+ ret = val_int;
+ else
+ *(int *)data = val_int;
+ break;
+ case DATA_TYPE_DOUBLE:
+ val_double = get_double_from_object(obj, key);
+ if (val_double < 0)
+ ret = -EINVAL;
+ else
+ *(double *)data = val_double;
+ break;
+ case DATA_TYPE_STRING:
+ val_str = get_string_from_object(obj, key);
+ if (!val_str)
+ ret = -EINVAL;
+ else
+ snprintf((char *)data, BUFF_MAX, "%s", val_str);
+ break;
+ case DATA_TYPE_BOOLEAN:
+ val_int = get_int_from_object(obj, key);
+ if (val_int < 0)
+ ret = val_int;
+ else
+ *(int *)data = val_int;
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+ if (ret < 0) {
+ if (mandatory) {
+ _E("Failed to get property of '%s'\n", key);
+ } else {
+ _D("There is no property of '%s'\n", key);
+ ret = 0;
+ }
+ }
+
+ return ret;
+}