static Eina_Value * azy_value_deserialize_array_json(cJSON *object) { Eina_Value *arr; const Eina_Value_Type *type; int x = 0; cJSON *child; child = cJSON_GetArrayItem(object, 0); if (!child) return eina_value_array_new(EINA_VALUE_TYPE_STRING, 0); type = _azy_value_type_get(child); arr = eina_value_array_new(type, 0); for (; x < cJSON_GetArraySize(object); x++) { if (x) { child = cJSON_GetArrayItem(object, x); if(_azy_value_type_get(child) != type) { EINA_LOG_ERR("We have different type of data in array."); continue; } } if (type == EINA_VALUE_TYPE_ARRAY) { Eina_Value_Array inner_array; Eina_Value *data = azy_value_deserialize_array_json(child); if (!data) goto error; eina_value_get(data, &inner_array); eina_value_array_append(arr, inner_array); } else if (type == EINA_VALUE_TYPE_STRUCT) { Eina_Value_Struct st; Eina_Value *data = azy_value_deserialize_struct_json(child); if (!data) goto error; eina_value_get(data, &st); eina_value_array_append(arr, st); } else { if (!azy_value_deserialize_basic_json(child, arr)) goto error; } } return arr; error: eina_value_free(arr); return NULL; }
static void _fill_receive_array_of_string_int_with_size(Eldbus_Message *msg, int size, const char *array[]) { Eina_Value *value_struct, *value_array; int i; unsigned offset; Eina_Value_Struct_Member main_members[2]; Eina_Value_Struct_Member sub_members[] = { EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_STRING, sub_struct, txt), EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT, sub_struct, num) }; Eina_Value_Struct_Desc desc_sub_struct = { EINA_VALUE_STRUCT_DESC_VERSION, NULL, // no special operations sub_members, 2, sizeof(sub_struct) }; Eina_Value_Struct_Desc desc_struct = { EINA_VALUE_STRUCT_DESC_VERSION, NULL, // no special operations main_members, 2, 0//will be set below }; offset = _type_offset(sizeof(int), sizeof(Eina_Value_Array)); main_members[0].name = "size"; main_members[0].type = EINA_VALUE_TYPE_INT, 0; main_members[0].offset = 0; main_members[1].name = "array"; main_members[1].type = EINA_VALUE_TYPE_ARRAY; main_members[1].offset = offset; desc_struct.size = offset + sizeof(Eina_Value_Array); value_struct = eina_value_struct_new(&desc_struct); eina_value_struct_set(value_struct, "size", size); value_array = eina_value_array_new(EINA_VALUE_TYPE_STRUCT, size); for (i = 0; i < size; i++) { Eina_Value *value_sub_struct = eina_value_struct_new(&desc_sub_struct); Eina_Value_Struct st; eina_value_struct_set(value_sub_struct, "txt", array[i]); eina_value_struct_set(value_sub_struct, "num", i); eina_value_get(value_sub_struct, &st); eina_value_array_append(value_array, st); eina_value_free(value_sub_struct); } eina_value_struct_value_set(value_struct, "array", value_array); eldbus_message_from_eina_value("ia(si)", msg, value_struct); eina_value_free(value_struct); eina_value_free(value_array); }
static Eina_Value * azy_value_deserialize_basic_json(cJSON *object, Eina_Value *arr) { switch (object->type) { case cJSON_False: case cJSON_True: if (!arr) return azy_value_util_bool_new(object->valueint); eina_value_array_append(arr, object->valueint); return (Eina_Value*)1; case cJSON_Number: { double d = object->valuedouble; //this is some crazy from cJSON.c for int detection if ((fabs(((double)object->valueint) - d) <= __DBL_EPSILON__) && (d <= __INT_MAX__) && (d >= -__INT_MAX__ - 1)) { if (!arr) return azy_value_util_int_new(object->valueint); eina_value_array_append(arr, object->valueint); } else { if (!arr) return azy_value_util_double_new(object->valuedouble); eina_value_array_append(arr, object->valuedouble); } return (Eina_Value*)1; } case cJSON_String: if (!arr) return azy_value_util_string_new(object->valuestring); eina_value_array_append(arr, object->valuestring); return (Eina_Value*)1; default: break; } return NULL; }