std::string addJsonWrapperObject()

in docker_images/c/wrapper/glue/GlueUtils.cpp [86:144]


std::string addJsonWrapperObject(std::string old_root_string, std::string wrapperDotname)
{
    JSON_Value* old_root_value = NULL;
    JSON_Value* new_root_value = NULL;
    char *new_string = NULL;
    
    try
    {
        JSON_Object* new_root_object;
        if ((old_root_value = json_parse_string(old_root_string.c_str())) == NULL)
        {
            throw new std::runtime_error(PARSON_ERROR);
        }
        else if ((new_root_value = json_value_init_object()) == NULL)
        {
            throw new std::runtime_error(PARSON_ERROR);
        }
        else if ((new_root_object = json_value_get_object(new_root_value)) == NULL)
        {
            throw new std::runtime_error(PARSON_ERROR);
        }
        else if (json_object_dotset_value(new_root_object, wrapperDotname.c_str(), old_root_value) != JSONSuccess)
        {
            throw new std::runtime_error(PARSON_ERROR);
        }

        new_string = json_serialize_to_string(new_root_value);
        std::string result = new_string;
        json_free_serialized_string(new_string);
        new_string = NULL;

        json_value_free(new_root_value); //implicitly frees old_root_value and new_root_object as well
        new_root_value = NULL;

        return result;
    }
    catch (...)
    {
        if (new_string)
        {
            json_free_serialized_string(new_string);
            new_string = NULL;
        }

        if (old_root_value)
        {
            json_value_free(old_root_value);
            old_root_value = NULL;
        }

        if (new_root_value)
        {
            json_value_free(new_root_value); //implicitly frees new_root_object as well
            new_root_value = NULL;
        }

        throw;
    }
}