Multiline 'values' in json using the Jansson API

180 Views Asked by At

Something in here is not allowing '\n' to be interpreted as a newline control character. Can someone help?

The output, when printed, seems to print

"I want this statement\nto be in newlines,\nbecause I want it\n that way.",

AND NOT

"I want this statement

to be in newlines,

because I want it

that way."

static void
append_object_to_array(json_t* my_json_array)
{
    const char* KEY   = "KEY";
    const char* VALUE = "I want this statement\nto be in newlines,\nbecause I want it\n that way.";

    json_t* my_json_obj = json_object();
    json_object_set_new(my_json_obj , KEY, json_string(VALUE));
    json_array_append_new(my_json_array, my_json_obj);
}

int main()
{
    json_t* json_array = json_array();
    
    append_object_to_array(json_array);
    json_decref(my_json_array);

    char* output = NULL;
    output = json_dumps(my_json_array, JSON_ENCODE_ANY);
/* My other failed attempts at getting this to work
 * output = json_dumps(my_json_array, JSON_ESCAPE_SLASH);
 * output = json_dumps(my_json_array, JSON_INDENT(n));
 */

    char* expected_output = "[{\"KEY\": \"I want this statement\nto be in newlines,\nbecause I want it\n that way.\"}]";
    if (strcmp(expected_output, output) != 0) {
        printf("Expected: %s", expected_output);
        printf("\n");
        printf("Actual: %s", output);
    } else {
        printf("Success");
    }

    return 0;
}

P.S. I have checked the following possible duplicates (I may have missed something), but they don't seem to fit what I am looking for.

0

There are 0 best solutions below