DATA_MARSHALLER_RESULT DataMarshaller_SendData_ReportedProperties()

in serializer/src/datamarshaller.c [199:323]


DATA_MARSHALLER_RESULT DataMarshaller_SendData_ReportedProperties(DATA_MARSHALLER_HANDLE dataMarshallerHandle, VECTOR_HANDLE values, unsigned char** destination, size_t* destinationSize)
{
    DATA_MARSHALLER_RESULT result;
    if (
        (dataMarshallerHandle == NULL) ||
        (values == NULL) ||
        (destination == NULL) ||
        (destinationSize == NULL)
        )
    {
        LogError("invalid argument DATA_MARSHALLER_HANDLE dataMarshallerHandle=%p, VECTOR_HANDLE values=%p, unsigned char** destination=%p, size_t* destinationSize=%p",
            dataMarshallerHandle,
            values,
            destination,
            destinationSize);
        result = DATA_MARSHALLER_INVALID_ARG;
    }
    else
    {
        JSON_Value* json = json_value_init_object();
        if (json == NULL)
        {
            LogError("failure calling json_value_init_object");
            result = DATA_MARSHALLER_ERROR;
        }
        else
        {
            JSON_Object* jsonObject = json_object(json);
            if (jsonObject == NULL)
            {
                LogError("failure calling json_object");
                result = DATA_MARSHALLER_ERROR;
            }
            else
            {
                size_t nReportedProperties = VECTOR_size(values), nProcessedProperties = 0;

                for (size_t i = 0;i < nReportedProperties; i++)
                {
                    DATA_MARSHALLER_VALUE* v = *(DATA_MARSHALLER_VALUE**)VECTOR_element(values, i);
                    STRING_HANDLE s = STRING_new();
                    if (s == NULL)
                    {
                        LogError("failure calling STRING_new");
                        i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
                    }
                    else
                    {
                        if (AgentDataTypes_ToString(s, v->Value) != AGENT_DATA_TYPES_OK)
                        {
                            LogError("failure calling AgentDataTypes_ToString");
                            i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
                        }
                        else
                        {
                            JSON_Value * rightSide = json_parse_string(STRING_c_str(s));
                            if (rightSide == NULL)
                            {
                                LogError("failure calling json_parse_string");
                                i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
                            }
                            else
                            {
                                char* leftSide;
                                if (mallocAndStrcpy_s(&leftSide, v->PropertyPath) != 0)
                                {
                                    LogError("failure calling mallocAndStrcpy_s");
                                    json_value_free(rightSide);
                                    i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
                                }
                                else
                                {
                                    char *whereIsSlash;
                                    while ((whereIsSlash = strchr(leftSide, '/')) != NULL)
                                    {
                                        *whereIsSlash = '.';
                                    }

                                    if (json_object_dotset_value(jsonObject, leftSide, rightSide) != JSONSuccess)
                                    {
                                        LogError("failure calling json_object_dotset_value");
                                        json_value_free(rightSide);
                                        i = nReportedProperties;/*forces loop to break, result is set in the "if" following this for*/
                                    }
                                    else
                                    {
                                        /*all is fine with this property... */
                                        nProcessedProperties++;
                                    }
                                    free(leftSide);
                                }
                            }
                        }
                        STRING_delete(s);
                    }
                }

                if (nProcessedProperties != nReportedProperties)
                {
                    result = DATA_MARSHALLER_ERROR;
                    /*all properties have NOT been processed*/
                    /*return result as is*/
                }
                else
                {
                    char* temp = json_serialize_to_string_pretty(json);
                    if (temp == NULL)
                    {
                        LogError("failure calling json_serialize_to_string_pretty ");
                        result = DATA_MARSHALLER_ERROR;
                    }
                    else
                    {
                        *destination = (unsigned char*)temp;
                        *destinationSize = strlen(temp);
                        result = DATA_MARSHALLER_OK;
                        /*all is fine... */
                    }
                }
            }
            json_value_free(json);
        }
    }
    return result;
}