Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse with rapidjson from std::string?

Tags:

c++

rapidjson

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 ?

like image 622
Damir Avatar asked Dec 02 '22 23:12

Damir


2 Answers

Try this for parse std:: string

std::string str = "{ \"hello\" : \"world\" }";
copiedDocument.Parse<0>(str.c_str());
like image 163
Arvind Kanjariya Avatar answered Dec 22 '22 13:12

Arvind Kanjariya


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

like image 26
stefanie wang Avatar answered Dec 22 '22 15:12

stefanie wang