Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wchar_t/std::wstring json can be created but not accessed #1533

Closed
TheOneRing opened this issue Mar 20, 2019 · 5 comments
Closed

wchar_t/std::wstring json can be created but not accessed #1533

TheOneRing opened this issue Mar 20, 2019 · 5 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@TheOneRing
Copy link

TheOneRing commented Mar 20, 2019

#1031 added support for other string types.

But the following example fails to compile.

#include "json.hpp"

#include <iostream>

using namespace std;
using json = nlohmann::json;


int main(int argc, char *argv[])
{
    json  j{
        {L"foo", L"bar"}
    };

    // error
    cout << j[L"foo"] << endl;


    cout << j.dump() << endl;
    // [[[102,111,111,0],[98,97,114,0]]]
    return 0;
}

with

FAILED: CMakeFiles/json.dir/main.cpp.obj 
C:\PROGRA~2\MICROS~1\2017\WDEXPR~1\VC\Tools\MSVC\1416~1.270\bin\HostX86\x86\cl.exe  /nologo /TP -DQT_CORE_LIB -I. -IC:\Users\hanna\Documents\json -Ijson_autogen\include -IC:\home2\oli\qtdevenv-5.12\include\qt5 -IC:\home2\oli\qtdevenv-5.12\include\qt5\QtCore -IC:\home2\oli\qtdevenv-5.12\.\mkspecs\win32-msvc /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoCMakeFiles\json.dir\main.cpp.obj /FdCMakeFiles\json.dir\ /FS -c C:\Users\hanna\Documents\json\main.cpp
c:\users\hanna\documents\json\json.hpp(16061): error C2664: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &std::map<StringType,nlohmann::basic_json<std::map,std::vector,StringType,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>,std::less<StringType>,std::allocator<std::pair<const StringType,nlohmann::basic_json<std::map,std::vector,StringType,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>>>::operator [](const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)': cannot convert argument 1 from 'T *' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&'
        with
        [
            StringType=std::string
        ]
        and
        [
            T=const wchar_t
        ]
c:\users\hanna\documents\json\json.hpp(16061): note: Reason: cannot convert from 'T *' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
        with
        [
            T=const wchar_t
        ]
c:\users\hanna\documents\json\json.hpp(16061): note: No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Users\hanna\Documents\json\main.cpp(16): note: see reference to function template instantiation 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator []<const wchar_t>(T *)' being compiled
        with
        [
            T=const wchar_t
        ]
C:\Users\hanna\Documents\json\main.cpp(16): note: see reference to function template instantiation 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator []<const wchar_t>(T *)' being compiled
        with
        [
            T=const wchar_t
        ]

I'd expect an static_assert for the creation of a json from wchar_t if you can't use it as a key.

@nlohmann
Copy link
Owner

#1031 only introduced the possibility to parse from a wide string. Wide strings are not supported otherwise. operator[] fails, because object keys are std::strings internally, and there is no overload for std::wstring. I am not sure whether a static assert would be helpful here, because there are already several possible overloads, or did I misunderstand?

The odd dump result shows that wide string literals are interpreted as sequence of integers. Internally, the library uses UTF-8 as encoding. Adding wide strings support outside the parser would mean ad-hoc recoding - not impossible, but a lot of work...

@TheOneRing
Copy link
Author

Id expect that the creation from wchar_t would fail. At least with wchar_t keys.

Yes supporting wstring is a pain, but windows applications basically need to use wchar_t.

@TheOneRing TheOneRing reopened this Mar 20, 2019
@TheOneRing
Copy link
Author

I mean the dump would be unreadable, but it would work if it would transform the wchar_t key in
j[L"foo"] to the [102,111,111,0] from the dump?

@nlohmann
Copy link
Owner

You mean j[ [102,111,111,0] ]? No, that would not work as operator[] only works for std::string.

@stale
Copy link

stale bot commented Apr 20, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 20, 2019
@stale stale bot closed this as completed Apr 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants