Rapidjson 不读取文件
Rapidjson not reading file
我搜索了这个特定的错误,但没有找到任何相关问题。
我正在尝试使用 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 控制台,但它在 document.h
.[=25= 中抛出一个 Rapidjson
断言]
关键是它正在识别 rapidjson
个文件,因为它找到函数并且不会抛出任何错误。但是,当我调试时,这就是执行 Parse()
后 doc
对象包含的内容
{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>()
,除非我不知道atm是什么意思,结果完全一样
这不是路径问题,因为我对图像和其他数据使用了相同的路径 (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 代码创建了文件并将其另存为 .json,并且我从 Windows 资源管理器将其创建为 .txt,结果相同。我也从 JSON Editor Online 下载了它,但它不起作用,所以我几乎可以肯定还有另一个我无法解决的问题...
在APIDocument::Parse(const char* json)
中,参数json
是一个包含[=36=]的字符串,不是文件名。
您可以
- 自己将JSON文件加载成字符串,然后调用
Parse()
;或
- 使用
ParseStream()
with FileReadStream
which wraps FILE*
;或
- 使用
ParseStream()
with IStreamWrapper
which wraps ifstream
.
对于大 JSON 文件,2 和 3 具有减少内存使用的优势。
我搜索了这个特定的错误,但没有找到任何相关问题。
我正在尝试使用 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 控制台,但它在 document.h
.[=25= 中抛出一个 Rapidjson
断言]
关键是它正在识别 rapidjson
个文件,因为它找到函数并且不会抛出任何错误。但是,当我调试时,这就是执行 Parse()
doc
对象包含的内容
{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>()
,除非我不知道atm是什么意思,结果完全一样
这不是路径问题,因为我对图像和其他数据使用了相同的路径 (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 代码创建了文件并将其另存为 .json,并且我从 Windows 资源管理器将其创建为 .txt,结果相同。我也从 JSON Editor Online 下载了它,但它不起作用,所以我几乎可以肯定还有另一个我无法解决的问题...
在APIDocument::Parse(const char* json)
中,参数json
是一个包含[=36=]的字符串,不是文件名。
您可以
- 自己将JSON文件加载成字符串,然后调用
Parse()
;或 - 使用
ParseStream()
withFileReadStream
which wrapsFILE*
;或 - 使用
ParseStream()
withIStreamWrapper
which wrapsifstream
.
对于大 JSON 文件,2 和 3 具有减少内存使用的优势。