简体   繁体   English

将 json++ 实现转换为使用 rapidJSON:处理字符串和 wstring 的混合

[英]converting json++ implementation to use rapidJSON: handling mixture of string and wstring

I'm converting some code that originally used the json++ library to now use rapidJSON.我正在将一些最初使用 json++ 库的代码转换为现在使用 rapidJSON。 The code serializes and de-serializes various objects using json files.该代码使用 json 文件对各种对象进行序列化和反序列化。 In json++, it looks something like this:在 json++ 中,它看起来像这样:

serialize:连载:

string normal = "normal";
wstring wide = L"wide";
JSON::Object json;
    
json["normal"] = normal;
json["wide"] = wide;

de-serialize:反序列化:

string normalCopy = json["normal"].as_string();
wstring wideCopy = json["wide"].as_wstring();

I haven't found a simple way to serialize and deserialize mixed strings using rapidJSON.我还没有找到一种使用 rapidJSON 序列化和反序列化混合字符串的简单方法。

Here are two examples:这里有两个例子:

    #include <rapidjson/document.h>
    #include <rapidjson/writer.h>
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    using namespace rapidjson;
    
    int main()
    {
        string normalstr = "This is a normal string";
        wstring widestr = L"This is a wide string";
    
        Document doc;
        auto& alloc = doc.GetAllocator();
    
        Value val(kObjectType);
    
        val.AddMember("normal", normalstr, alloc);
        val.AddMember("wide", widestr, alloc); // <-- cannot convert
    
        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        val.Accept(writer);
    
        ostringstream jsonOutput;
    
        jsonOutput << buffer.GetString();
    
        cout << jsonOutput.str() << endl;
    }

and

    #include <rapidjson/document.h>
    #include <rapidjson/writer.h>
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    using namespace rapidjson;
    
    int main()
    {
        string normalstr = "This is a normal string";
        wstring widestr = L"This is a wide string";
    
        Document doc;
        auto& alloc = doc.GetAllocator();
    
        GenericValue<UTF16<> > val(kObjectType);
    
        val.AddMember(L"normal", normalstr, alloc); // <-- cannot convert
        val.AddMember(L"wide", widestr, alloc);
    
        GenericStringBuffer<UTF16<> > buffer;
        Writer<GenericStringBuffer<UTF16<> >, UTF16<>> writer(buffer);
        val.Accept(writer);
    
        ostringstream jsonOutput;
    
        jsonOutput << buffer.GetString();
    
        cout << jsonOutput.str() << endl;
    }

Based on my understanding, RapidJSON is set up to work either exclusively with std::string (UTF-8) or with std::wstring (UTF-16) when dealing at object-level granularity.根据我的理解,RapidJSON 设置为在处理对象级粒度时专门与 std::string (UTF-8) 或 std::wstring (UTF-16) 一起使用。

Do I need to convert from wstring to string when I have both types in the object I want to serialize or is there something available in the API that I'm not aware of?当我想要序列化的 object 中有两种类型时,我是否需要从 wstring 转换为字符串,或者 API 中是否有我不知道的可用内容?

I think you need to use conversion here, I have go through the doc and src of RapidJSON, and confirmed that we can't mix GenericValue with Value .我认为您需要在这里使用转换,我通过 RapidJSON 的 doc 和 src 有 go ,并确认我们不能将GenericValueValue混合。

We can use the wstring_convert , see this answer as the reference.我们可以使用wstring_convert ,将此答案作为参考。

Or with boost .或与boost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM