#18 Support strings containing Null characters

open
nobody
None
5
2014-02-04
2013-04-05
Stefan Wehner
No

This is a patch for the bug "Incorrect parsing of strings with null characters (\u0000) - ID: 3525583".
It basically includes the length of a string in Json::Value as part of the union, and in value and writer either the the pair (char*, length) or the c++ string are used.
It also modifies the writer to write \0 as \u0000.

The end result is that you can parse strings like "NUL: \u0000 end" which will return:
* Json::Value::asString() returns "NUL: \0 end" (stringn actually containing a \0 character
* Is written (writer.write or toStyledString) escaping the \u0000 character.

We're using this for JSONs containing blobs of binary data and it seems to work fine.
Please let me know where it'd best to add some tests for it - I'd more than happy to take a closer look at that.

The patch is done against 0.6.0-rc2 (r191).

Let me know what you think - Hopefully you can include this in the code :)

Discussion

  • Mike Gelfand
    Mike Gelfand
    2014-01-25

    Great patch, Stefan. One issue though, you have lost reverse solidus escaping, so additional condition should be added to isCharacterToEscape to make it look like
    static bool isCharacterToEscape(char ch)
    {
    return ( ch >= 0 && ch <= 0x1F ) || ( ch == '\"' ) || ( ch == '\' );
    }
    There also were some changes (using r275 now) which prevent clean patch application, but nothing one couldn't deal with.