1

I have unsigned char array holding data, I want to store this into a Json object. How can it be done? Using jsoncpp and C++11. the statement

rootL1["CurrentValue"] = userInfo->hashCred;

does not work, in turn it is stored as bool in json instead of the unsigned data.

marcinj
  • 48,511
  • 9
  • 79
  • 100
sach
  • 99
  • 2
  • 9

1 Answers1

2

If you can look into documentation of jsoncpp Value class (I assume rootL1 is of this type, and operator[] returns the same type), you will see that there is no unsigned char* conversion constructor which would accept your userInfo->hashCred array. There is one constructor which accepts const char* but there is no implicit conversion from unsigned char* to const char*, the closest one is implicit conversion of pointer to bool which is choosen in your case.

The solution should be to use conversion constructor which accepts const char*. You should think why you need unsigned char* in the first place, maybe you can use an array of type char? If you cant then you can try casting unsigned char* to char*:

rootL1["CurrentValue"] = reinterpret_cast<char *>(userInfo->hashCred)

but this can cause all sorts of problems depending on what you are actually storing in your array and also what jsoncpp is doing under the hood with this data.

I would suggest you treat your hashCred as binary data and uuencode it to char array. Then store it in jsoncpp Value. For details look into here: Binary data JSONCPP

In jsoncpp test code you can also find some examples of how to store binary data as values.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100