简体   繁体   中英

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. The code serializes and de-serializes various objects using json files. In json++, it looks something like this:

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.

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.

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?

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 .

We can use the wstring_convert , see this answer as the reference.

Or with boost .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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