Damir
Damir

Reputation: 56179

How to parse with rapidjson from std::string?

How to parse with rapidjson from std::string ? I am trying like (json string is valid, I checked on jsonlint.com)

Document document;
char * writable = new char[contentString.size() + 1];
std::copy(contentString.begin(), contentString.end(), writable);
writable[contentString.size()] = '\0'; // don't forget the terminating 0
std::cout<<writable<<"\n";
if (document.Parse<0>(writable).HasParseError())
    return 1;

contentString is my json std::string, but when I start I always get error ( return 1). I tried also without size()+1 and '\0' but nothing (desperate measure programming). Can anyone help me ?

Upvotes: 4

Views: 21873

Answers (4)

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Try this for parse std:: string

std::string str = "{ \"hello\" : \"world\" }";
copiedDocument.Parse<0>(str.c_str());

Upvotes: 7

stefanie wang
stefanie wang

Reputation: 71

if contentString is std::string, just try

document.Parse<0>(contentString.c_str()).HasParseError()

if contentString is char *, just try

document.Parse<0>(contentString).HasParseError()

and you'd better post your original code snippet

Upvotes: 5

Alon
Alon

Reputation: 1804

Seems to me there is an error with the string you send json, You are using it correctly, I suggest you try sending it:

const char json[] = "{ \"hello\" : \"world\" }";

And see if it works, if so obviously it's a buffer problem.. you can continue from there I am sure

Upvotes: 2

Daniel Frey
Daniel Frey

Reputation: 56863

Have you tried

if (document.Parse<0>(contentString.c_str()).HasParseError())
    return 1;

?

Because, from the documentation, I fail to see why you want anything "writable"...

Upvotes: 4

Related Questions