microsoft/cpprestsdk

[feature request] std::string support for json library

Open

#72 ouverte le 13 févr. 2016

Voir sur GitHub
 (20 commentaires) (8 réactions) (0 assignés)C++ (1 670 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (7 596 stars)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

Description

In my experience most 3rd party libraries use std::string and not std::wstring in their interface. Therefore applications become tied to std::string. Because the ppl/json library uses std::wstring (on windows) this makes integrating the ppl/json library into an applications cumbersome and inefficient. One has to do lots of unnecessary wstring-to-string and string-to-wstring conversions. Could you add support for std::string, at least for the json library?

Minimal example:

    #include <cpprest/json.h>

    std::string SerializeToJson(std::string name, int id) //utf8 coming in and goin out
    {
        utility::stringstream_t stream;
        web::json::value js_obj;
        js_obj[L"id"] = web::json::value::number(id);
        js_obj[L"name"] = web::json::value::string(utility::conversions::utf8_to_utf16(name)); //unneccessary conversion 1

        js_obj.serialize(stream);
        std::wstring wret = stream.str();
        std::string ret = utility::conversions::utf16_to_utf8(wret); //unneccessary conversion 2

        return ret;
    }

    int main() {

        auto r = SerializeToJson("test", 123);
        std::cout << r << std::endl;
    }

Guide contributeur