Search code examples
rapidjson

how to create a node from a string literal in rapidjson?


I want to create a JSON node from a string literal in rapidjson, my code is as follows(which doesn't work of cause):

inline rapidjson::Value to_json(const std::string& myStr) {
    auto result = rapidjson::Value(rapidjson::kStringType);
    result.SetString(myStr);
    return result;
}

I remember that Jackson's APIs are so nice that you can create a String node by TextNode.valueOf(myStr).

Is there a similar way to create a JSON node from string literal ?


Solution

  • You will need an allocator. And if you have defined RAPIDJSON_HAS_STDSTRING=1, you can simply:

    Document d;
    std::string s = "...";
    Value v(s, d.GetAllocator());