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

using dump() when std::wstring is StringType with VS2017 #836

Closed
TobiSchluter opened this issue Nov 20, 2017 · 4 comments
Closed

using dump() when std::wstring is StringType with VS2017 #836

TobiSchluter opened this issue Nov 20, 2017 · 4 comments
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@TobiSchluter
Copy link

TobiSchluter commented Nov 20, 2017

I may be doing something stupid, but I'm trying to use std::wstring as my string type. If I call json::dump on my wstring'ed json object, it seems to not be able to convert from char to wchar_t if I correctly understand an error message that is too verbose to not be opaque. I quote the errors at the bottom. This is with VisualStudio 2017. I cloned the git repo a few weeks ago, and haven't tried with the latest version, but I didn't see any pertinent checkins in the meantime. I may have missed them of course. Likewise for examples that use a different string type, I also didn't find any closed issues referring to wchar_t or std::wstring.

My declarations look like:

        namespace utility { typedef string_t std::wstring; }
        nlohmann::basic_json<std::map, std::vector, utility::string_t> j = njson::parse(wbody); // here wbody is of type std::wstring

and the call to dump is
utility::string_t dump = j.dump(0); // again utility::string_t is std::wstring.

Is this expected?

Thank you for your good work. The library has really made my life a lot easier.

(The first warning always appears when including the header, and is unrelated.)

1>C:\tobi\Source\json/json.hpp(5961): warning C4819: The file contains a character that cannot be represented in the current code page (932). Save the file in Unicode format to prevent data loss
1>C:\tobi\Source\json/json.hpp(8999): error C2440: '<function-style-cast>': cannot convert from 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>' to 'nlohmann::detail::output_adapter<char>'
1>C:\tobi\Source\json/json.hpp(8999): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\tobi\Source\json/json.hpp(8997): note: while compiling class template member function 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::dump(const int,const char,const bool) const'
1>C:\tobi\Source\source.cpp(57): note: see reference to function template instantiation 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::dump(const int,const char,const bool) const' being compiled
1>C:\tobi\Source\source.cpp(34): note: see reference to class template instantiation 'nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>' being compiled
1>C:\tobi\Source\json/json.hpp(8999): error C2664: 'nlohmann::detail::serializer<nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>::serializer(const nlohmann::detail::serializer<nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>> &)': cannot convert argument 1 from 'const char' to 'const nlohmann::detail::serializer<nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>> &'
1>C:\tobi\Source\json/json.hpp(8999): note: Reason: cannot convert from 'const char' to 'const nlohmann::detail::serializer<nlohmann::basic_json<std::map,std::vector,utility::string_t,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>'
1>C:\tobi\Source\json/json.hpp(8999): note: No constructor could take the source type, or constructor overload resolution was ambiguous

@nlohmann
Copy link
Owner

Currently, std::wstring is not supported by the library. Even though the string type is templated, there are too many assumptions in the code that rely on using std::string. Sorry about that :-/

@TobiSchluter
Copy link
Author

I figured that the lack of documentation on how to use arbitrary string types was no accident, so I didn't bet all my chips on this. I would have been mightily impressed if it worked though!

I don't know your policy, so I'm not going to close this issue myself, but please feel free to do so, no feelings hurt :)

@nlohmann
Copy link
Owner

In #774 there is a discussion how to improve the library toward better support of types. I hope we can find a way to cope with wstrings there.

@nlohmann nlohmann added the solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) label Nov 22, 2017
@nlohmann
Copy link
Owner

nlohmann commented Aug 1, 2021

I added an FAQ entry: https://json.nlohmann.me/home/faq/#wide-string-handling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

2 participants