3

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.

  • Please [edit] your question and create a [mre] that includes the code to print `output` and the complete output formatted as a separate code block. This [answer](https://stackoverflow.com/a/16690186/10622916) to the linked question "Can a JSON value contain a multiline string" states that you cannot have a literal newline character in your string value inside JSON and that you have to encode it as `\n`. If this does not apply to your use case, [edit] your question to explain this. – Bodo Aug 10 '21 at 18:02
  • @Bodo, I misunderstood 'encoding' to 'json_string()' func call. Using the Jansson API, how can one encode the indication of newline/linefeed in the string? – blah.blah.cat Aug 11 '21 at 07:59
  • A literal newline inside a string value is not allowed in JSON. See https://tools.ietf.org/id/draft-ietf-json-rfc4627bis-08.html#rfc.section.7 "*All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).*" It may not be possible to generate invalid JSON using the Jansson library. Please explain the background in your question. What do you want to achieve? Why do you need invalid JSON output? – Bodo Aug 11 '21 at 12:38
  • I understand. The purpose is to use a similar C code to output the result in JSON fmt on demand; the Jansson library facilitates that. The output that I am dealing with dominantly multi-lined, which is not intuitive to read and causes a lack of brevity for my use case. I have now understood the limitation and will try to figure out alternate approaches and rethink the structure of my 'value's. – blah.blah.cat Aug 11 '21 at 19:18

0 Answers0