static BUFFER_HANDLE createConfigurationPayloadJson()

in iothub_service_client/src/iothub_deviceconfiguration.c [440:556]


static BUFFER_HANDLE createConfigurationPayloadJson(const IOTHUB_DEVICE_CONFIGURATION* configuration)
{
    BUFFER_HANDLE result;

    JSON_Value* root_value = NULL;
    JSON_Object* root_object = NULL;
    JSON_Value* configurationContentJson;
    JSON_Value* configurationLabelsJson;
    JSON_Value* metricsQueriesJson;
    JSON_Value* systemMetricsQueriesJson;

    if (configuration == NULL)
    {
        LogError("configuration cannot be null");
        result = NULL;
    }
    else if (configuration->configurationId == NULL)
    {
        LogError("Configuration id cannot be NULL");
        result = NULL;
    }
    else if (((configuration->content).deviceContent == NULL) && (((configuration->content).modulesContent) == NULL))
    {
        LogError("deviceContent and modulesContent both cannot be NULL");
        result = NULL;
    }
    else if ((root_value = json_value_init_object()) == NULL)
    {
        LogError("json_value_init_object failed");
        result = NULL;
    }
    else if ((root_object = json_value_get_object(root_value)) == NULL)
    {
        LogError("json_value_get_object failed");
        result = NULL;
    }
    else if ((json_object_set_string(root_object, CONFIGURATION_JSON_KEY_CONFIGURATION_ID, configuration->configurationId)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for configurationId");
        result = NULL;
    }
    else if ((json_object_set_string(root_object, CONFIGURATION_JSON_KEY_SCHEMA_VERSION, configuration->schemaVersion)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for schemaVersion");
        result = NULL;
    }
    else if ((json_object_set_string(root_object, CONFIGURATION_JSON_KEY_TARGET_CONDITION, configuration->targetCondition)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for targetCondition");
        result = NULL;
    }
    else if ((json_object_set_number(root_object, CONFIGURATION_JSON_KEY_PRIORITY, configuration->priority)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for priority");
        result = NULL;
    }
    else if ((json_object_set_string(root_object, CONFIGURATION_JSON_KEY_ETAG, configuration->eTag)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for eTag");
        result = NULL;
    }
    else if (((configuration->createdTimeUtc != NULL) && json_object_set_string(root_object, CONFIGURATION_JSON_KEY_CREATED_TIME, configuration->createdTimeUtc)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for createdTimeUtc");
        result = NULL;
    }
    else if (((configuration->lastUpdatedTimeUtc != NULL) && json_object_set_string(root_object, CONFIGURATION_JSON_KEY_CREATED_TIME, configuration->lastUpdatedTimeUtc)) != JSONSuccess)
    {
        LogError("json_object_set_string failed for lastUpdatedTimeUtc");
        result = NULL;
    }
    else if (((configurationLabelsJson = createConfigurationLabelsPayload(&configuration->labels)) != NULL) && ((json_object_set_value(root_object, CONFIGURATION_JSON_KEY_LABELS, configurationLabelsJson)) != JSONSuccess))
    {
        LogError("json_object_set_string failed for configurationLabelsJson");
        result = NULL;
    }
    else if ((((configurationContentJson = createConfigurationContentPayload(&configuration->content)) != NULL) && ((json_object_set_value(root_object, CONFIGURATION_JSON_KEY_CONTENT, configurationContentJson)) != JSONSuccess)))
    {
        LogError("json_object_set_string failed for configurationContentJson");
        result = NULL;
    }
    else if (((metricsQueriesJson = createConfigurationMetricsQueriesPayload(&configuration->metricsDefinition)) != NULL) && ((json_object_set_value(root_object, CONFIGURATION_JSON_KEY_CUSTOM_METRICS, metricsQueriesJson)) != JSONSuccess))
    {
        LogError("json_object_set_string failed for metricsQueriesJson");
        result = NULL;
    }
    else if (((systemMetricsQueriesJson = createConfigurationMetricsQueriesPayload(&configuration->systemMetricsDefinition)) != NULL) && ((json_object_set_value(root_object, CONFIGURATION_JSON_KEY_SYSTEM_METRICS, systemMetricsQueriesJson)) != JSONSuccess))
    {
        LogError("json_object_set_string failed for systemMetricsQueriesJson");
        result = NULL;
    }
    else
    {
        char* serialized_string;
        if ((serialized_string = json_serialize_to_string(root_value)) == NULL)
        {
            LogError("json_serialize_to_string failed");
            result = NULL;
        }
        else
        {
            if ((result = BUFFER_create((const unsigned char*)serialized_string, strlen(serialized_string))) == NULL)
            {
                LogError("Buffer_Create failed");
                result = NULL;
            }
            json_free_serialized_string(serialized_string);
        }
    }

    json_object_clear(root_object);

    if (root_value != NULL)
        json_value_free(root_value);

    return result;
}