0

I have a big character array. Lets say it contains 4500 bytes.

Case #1:

If I write the following code the complete value is not displayed

cout << filecontent << endl;

Case #2:

In this case, it is working perfectly fine.

for(int i=0; i <= size; i++) 
            cout << filecontent[i];

My issue is I want to assign the value to Json::Value. If I do normal assignment operation, it takes only the garbage value.

Why it is so?

As far as my understanding, whenever the actual data contains \0 it treats as the end of the value.

How to overcome this issue?

Dineshkumar
  • 1,468
  • 4
  • 22
  • 49
  • possible duplicate of [How do you construct a std::string with an embedded null?](http://stackoverflow.com/questions/164168/how-do-you-construct-a-stdstring-with-an-embedded-null) – Ivaylo Strandjev Apr 10 '14 at 09:54
  • @IvayloStrandjev when I try with `string actualcontent(filecontent, size);` and trying with actualcontent also not giving me the proper value? – Dineshkumar Apr 10 '14 at 09:56
  • 1
    Assuming OP is referring to http://jsoncpp.sourceforge.net/ then the std::string constructor copies the string to a char*, and throws away the length (I think), so will stop at the first Null. – Douglas Leeder Apr 10 '14 at 10:00
  • jsoncpp doesn't treat NULL as a control character, instead just using it to terminate strings. – Douglas Leeder Apr 10 '14 at 10:04
  • I guess OP will need to find a different Json library. – Douglas Leeder Apr 10 '14 at 10:05
  • I checked the program again. Once it copies to `std::string` that time itself the issue started. Nothing to do with JSONCPP. – Dineshkumar Apr 10 '14 at 10:06
  • But even once you get a std::string with an embedded NULL, it won't help you since JSONCPP only deals with char* so will stop at the first NULL as far as I can see. – Douglas Leeder Apr 10 '14 at 11:12
  • This is a bug in `jsoncpp`, since zero is valid UTF8. [Here is a ticket](https://github.com/open-source-parsers/jsoncpp/issues/176). – cdunn2001 Feb 17 '15 at 05:54

2 Answers2

1

You can create std::string with NULLs in by passing in an explicit length.

This doesn't help you if you are using JsonCPP, since that stores char* internally, even if you pass in a std::string, so loses the length and terminates at the first NULL. Also it doesn't have handling for NULL as a control character so would just put NULL (rather than \u0000) into the JSON.

I think you will need to find a different JSON library to do what you want. The only other alternative I've thought of is to generate template JSON with a stand-in for your data, then do your own replacement and escaping after generation. Of course alternatively you could fix the problem and submit a patch if you wanted.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
0

use write. as in:

cout.write( filecontent, size );

if you need a string, use a stringstream:

stringstream strstr;
strstr.write( filecontent, size );
string ss = strstr.str();

requires header <sstream>

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • How to assign it to other variable, especially `Json::Value` ? – Dineshkumar Apr 10 '14 at 09:53
  • @Prabhu `std::copy`throws the `error C2039: 'iterator_category' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'` – Dineshkumar Apr 10 '14 at 10:13
  • try string::copy instead of std::copy? There is always a crude way to get anything working. Resize Json::Value and do a memcpy :)... – Prabhu Apr 10 '14 at 10:14