diff options
Diffstat (limited to 'src/cloud/car_info_serializer.c')
-rw-r--r-- | src/cloud/car_info_serializer.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/cloud/car_info_serializer.c b/src/cloud/car_info_serializer.c index a9bf9f3..3ee74f6 100644 --- a/src/cloud/car_info_serializer.c +++ b/src/cloud/car_info_serializer.c @@ -21,8 +21,10 @@ #include <json-glib/json-glib.h> #include "log.h" #include "cloud/car_info.h" +#include "string.h" #define JSON_SCHEMA_CAR_ID "carId" +#define JSON_SCHEMA_RESPONSE_CAR_ID "id" #define JSON_SCHEMA_CAR_NAME "carName" #define JSON_SCHEMA_CAR_IP "carIp" #define JSON_SCHEMA_AP_MAC "apMac" @@ -72,12 +74,17 @@ car_info_t **car_info_serializer_deserialize_array(const char *json_data, int *s JsonArray *array = json_node_get_array(root); if (!array) { - _E("Json config is invalid!"); + _E("Json is invalid!"); g_object_unref(parser); return NULL; } *size = json_array_get_length(array); + + if (*size == 0) { + return NULL; + } + car_info_t **car_info_array = g_malloc(*size * sizeof(car_info_t *)); json_array_foreach_element(array, car_info_array_iterate_cb, car_info_array); @@ -86,13 +93,13 @@ car_info_t **car_info_serializer_deserialize_array(const char *json_data, int *s return car_info_array; } -static JsonNode *parse_string(JsonParser *parser, const char *config_json) +static JsonNode *parse_string(JsonParser *parser, const char *json) { GError *err = NULL; - if (!json_parser_load_from_data(parser, config_json, -1, &err)) + if (!json_parser_load_from_data(parser, json, -1, &err)) { - _E("Function \"json_parser_load_from_data()\" failed with message: ", err->message); + _E("Function \"json_parser_load_from_data()\" failed with message: %s", err->message); g_error_free(err); return NULL; } @@ -108,8 +115,8 @@ static void car_info_array_iterate_cb(JsonArray *array, guint index, JsonNode *e car_info_array[index] = car_info_create(); - if (json_object_has_member(entry, JSON_SCHEMA_CAR_ID) && - car_info_set_car_id(car_info_array[index], json_object_get_string_member(entry, JSON_SCHEMA_CAR_ID)) != 0) + if (json_object_has_member(entry, JSON_SCHEMA_RESPONSE_CAR_ID) && + car_info_set_car_id(car_info_array[index], json_object_get_string_member(entry, JSON_SCHEMA_RESPONSE_CAR_ID)) != 0) { _E("Couldn't set car id!"); } |