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 :)
Great patch, Stefan. One issue though, you have lost reverse solidus escaping, so additional condition should be added to
isCharacterToEscape
to make it look likestatic 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.
The patch I'm currently using could be found at https://github.com/mikedld/jsoncpp-mirror/commit/303a259a33610769bd6b572c601c1154a4ee0e4c (against r276).
As of versions
1.5.0
and0.9.0
, UTF-8 with nulls is now supported at: https://github.com/open-source-parsers/jsoncppPlease let us know if you see a problem. We were not able to accept the patch because we had to maintain binary-compatibility. Nice work though.