Rapidjson没有阅读文件

时间:2016-03-22 17:02:16

标签: c++ visual-studio-2013 rapidjson

我搜索了这个特定错误,但我没有找到任何相关问题。

我正在尝试使用Rapidjson来解析我的C ++项目中的.json文件,这就是我正在做的事情(遵循RapidJSON tutorial状态):

的main.cpp

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

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

if语句的预期结果是将内容打印到Win32控制台,但它会在 Rapidjson 中抛出document.h断言。

重点在于它识别rapidjson个文件,因为它找到了函数并且不会抛出任何错误。但是,当我调试时,这就是执行doc

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 ...}

doc对象中的所有字段都有NULL个值。请注意,我已尝试Parse<0>(),除非我不知道这意味着什么,结果完全相同。

这不是一个路径问题,因为我对图像和其他数据使用相同的路径(data/)并找到它们。

我从编译器获得的唯一信息是一些我无法破译的警告。

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

我认为正在阅读此警告的是它不接受const char *,但这是我在链接的教程中完成的方式,并且它们没有显示任何其他方式。

提前致谢。

更新:

这似乎可能是文件的问题,但我不确定,并且不知道如何避免它。我用Visual Studio Code创建了文件,并将其保存为.json,我从Windows资源管理器中创建了一个.txt,结果相同。我也从JSON Editor Online下载了它,它不起作用,所以我几乎可以肯定我还有另外一个问题......

1 个答案:

答案 0 :(得分:1)

在API Document::Parse(const char* json)中,参数json是一个包含JSON的字符串,而不是文件名。

你可以

  1. 自己将JSON文件加载到字符串中,然后调用Parse();或
  2. 使用ParseStream() with FileReadStream which wraps FILE*;或
  3. 使用ParseStream() with IStreamWrapper which wraps ifstream
  4. 对于大型JSON文件,2和3具有减少内存使用的优势。