[Solved] Rapidjson IsString assertion failure

Hey there. Android studio project for android.
ndk = r13b
min sdk version = 15
target sdk version = 19

rapidjson::Document d;
    std::string file = "res/data.json";

    // TODO: try
    // TODO: std::string file = "data.json";

    // TODO: try
    // TODO: std::string content = FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(file.c_str()));

    std::string content = FileUtils::getInstance()->getStringFromFile(file.c_str());
    log("file content: %s", content.c_str());

    d.Parse<0>(content.c_str());

    if (d.HasParseError()) {
        log("\nerror test(offset %u): %d\n", (unsigned)d.GetErrorOffset(), d.GetParseError());
        log("error");
    } else
    {
        log("no error");
    }
    log("parsed json: %s", d.GetString());

And the data.json is : {"level":{"three":["ABE","ABY","ACE","ACT","ADD","ADO","AFT","AGA"],"four":["hdgs","asdjkas"]}} and it’s a valid json.(validation confirmed)
Well the file content is loaded, logged but without any parse error (d.HasParseError returns false meaning there is no error) d.Parse<0>(content.c_str()); fails with message json/document.h:1681: const Ch* rapidjson::GenericValue<Encoding, Allocator>::GetString() const [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::GenericValue<Encoding, Allocator>::Ch = char]: assertion "IsString()" failed
Any help is appreciated. (apk tested on both simulator and real device and both fail)

GetString() only works for rapidjson values, not documents. You need to use a string buffer to get the complete string of a document, like this:

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
    
log("parsed json: %s", buffer.GetString());

(As described in the rapidjson documentation)

1 Like

it should be d->value.GetString();

Document d;
if (!d.Parse(file.c_str()).HasParseError())//parse and check at the same time
{

}

rapidjson::Value::ConstMemberIterator level = d.FindMember("level");
if (level != document.MemberEnd())//if level is not found it will be ignored, not crash