Skip to content

jsoncpp discards everything after zero character ('\0') #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vdudouyt opened this issue Apr 10, 2015 · 1 comment · Fixed by #241
Closed

jsoncpp discards everything after zero character ('\0') #240

vdudouyt opened this issue Apr 10, 2015 · 1 comment · Fixed by #241
Assignees
Labels

Comments

@vdudouyt
Copy link

Hi!

Here I'm trying to build a system that relies on storing some of it's internal structures in JSON format. Some of fields may contain the binary data, and I mentioned that everything after '\0' is getting discarded - just as in old good "plain C" strings:

int main()
{
   Json::FastWriter writer;
   Json::Value root;
   root["some_binary_value"] = std::string("\1\2\0\3\4", 5);
   std::cout << writer.write(root);
}
$ ./json_test
{"some_binary_value":"\u0001\u0002"}

Could you please let me know if that's something that you'd like to be fixed?
Thank you very much for your time, and sorry for bothering you with a such sort of things!

Valentin

cdunn2001 added a commit to cdunn2001/jsoncpp that referenced this issue Apr 11, 2015
We had already fixed Value to hold UTF-8 properly, but only the newer
StreamWriter was writing UTF-8 properly.

Old FasterWriter etc. were using asCString() instead of asString() in
Value::writeValue().

Hopefully this change does not break any existing code. Seems unlikely.

issue open-source-parsers#240
@cdunn2001
Copy link
Contributor

The new StreamWriter has the fix already:

#include <json/json.h>
#include <iostream>
#include <memory>

int main()
{
  std::unique_ptr<Json::StreamWriter> const writer(
    Json::StreamWriterBuilder().newStreamWriter());
  Json::Value root;
  root["some_binary_value"] = std::string("\1\2\0\3\4", 5);
  writer->write(root, &std::cout);
}
{
    "some_binary_value" : "\u0001\u0002\u0000\u0003\u0004"
}

More conveniently, use writeString().

However, you're right: It does not work for some of the old (deprecated) Writers. That's easy to fix, and I guess it's not likely to break anyone.

@cdunn2001 cdunn2001 added the bug label Apr 11, 2015
@cdunn2001 cdunn2001 self-assigned this Apr 11, 2015
cdunn2001 added a commit to cdunn2001/jsoncpp that referenced this issue Apr 11, 2015
We had already fixed Value to hold UTF-8 properly, but only the newer
StreamWriter was writing UTF-8 properly.

Old FasterWriter etc. were using asCString() instead of asString() in
Value::writeValue().

Hopefully this change does not break any existing code. Seems unlikely.

issue open-source-parsers#240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants