jjimenezg93
jjimenezg93

Reputation: 162

Rapidjson not reading file

I've searched for this specific error but I didn't find any related question.

I'm trying to use Rapidjson to parse .json files in my C++ project, and this is how I'm doing it (following what RapidJSON tutorial states):

main.cpp

#include "../include/rapidjson/document.h"
...
using namespace rapidjson;
int main() {
    ...
    Document doc;
    doc.Parse("data/entities.json");

    if (doc.HasMember("DreadNought") { ... }
    ...
}

Expected result at if statement is to print something to the Win32 console, but it throws a Rapidjson assert inside document.h.

The point is that it's recognizing rapidjson files, since it finds functions and it doesn't throw any error. However, when I debug, that's what the doc object contains after executing Parse()

{data_={s={length=0 hashcode=0 str=0x00000000 <NULL> } ss={str=0x0124fa30 "" } n={i={i=0 padding=0x0124fa34 "" } ...} ...} }
0x0321ec30 {chunkHead_=0x00000000 <NULL> chunk_capacity_=65536 userBuffer_=0x00000000 ...}

All fields inside the doc object have NULL values. Note that I've tried Parse<0>(), unless I don't know what this means atm, and the result is exactly the same.

It is not a path problem, since I use the same path (data/) for images and other data and they're found.

The only information I get from compiler are some warnings I've not been able to decipher.

1>...\include\rapidjson\encodedstream.h(88): warning C4512: 'rapidjson::EncodedInputStream<rapidjson::UTF8<char>,rapidjson::MemoryStream>' : assignment operator could not be generated
    ...\include\rapidjson\encodedstream.h(68) : see declaration of 'rapidjson::EncodedInputStream<rapidjson::UTF8<char>,rapidjson::MemoryStream>'

1>...\include\rapidjson\document.h(324): warning C4512: 'rapidjson::GenericStringRef<char>' : assignment operator could not be generated
            ...\include\rapidjson\document.h(1119) : see reference to class template instantiation 'rapidjson::GenericStringRef<char>' being compiled
1>          ...\include\rapidjson\document.h(1118) : while compiling class template member function 'rapidjson::GenericMemberIterator<false,Encoding,Allocator> rapidjson::GenericValue<Encoding,Allocator>::FindMember(const char *)'
1>          with
1>          [
1>              Encoding=rapidjson::UTF8<char>
1>  ,            Allocator=rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>
1>          ]
1>          ...\include\rapidjson\document.h(1123) : see reference to function template instantiation 'rapidjson::GenericMemberIterator<false,Encoding,Allocator> rapidjson::GenericValue<Encoding,Allocator>::FindMember(const char *)' being compiled
1>          with
1>          [
1>              Encoding=rapidjson::UTF8<char>
1>  ,            Allocator=rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>
1>          ]
1>          ...\include\rapidjson\document.h(2010) : see reference to class template instantiation 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>' being compiled
1>          ...\src\main.cpp(17) : see reference to class template instantiation 'rapidjson::GenericDocument<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>,rapidjson::CrtAllocator>' being compiled

What I think it's happening reading this warnings is that it doesn't accept a const char *, but this is how it is done in the tutorial I linked, and they don't show any other way.

Thanks in advance.

UPDATE:

It seems it could be a problem with the file, but I'm not sure about that, and don't know how to avoid it. I've created the file with Visual Studio Code and saved it as .json, and I've created it as a .txt from Windows Explorer, same result. I've also downloaded it from JSON Editor Online, and it doesn't work, so I'm almost sure there's another problem I can't get...

Upvotes: 0

Views: 3574

Answers (1)

Milo Yip
Milo Yip

Reputation: 5072

In the API Document::Parse(const char* json), the parameter json is a string containing JSON, not a filename.

You can either

  1. load the JSON file into a string by yourself and then call Parse(); or
  2. use ParseStream() with FileReadStream which wraps FILE*; or
  3. use ParseStream() with IStreamWrapper which wraps ifstream.

For big JSON files, 2 and 3 have advantage of reduced memory usage.

Upvotes: 1

Related Questions