-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
BUG - json dump field with unicode -> array of ints (instead of string) #1197
Comments
Hm. It works when you remove the wide string literal: json result;
result["name"] = "אינעל דינק";
std::cout << result.dump(4) << std::endl; -> {
"name": "אינעל דינק"
} To get the code points escaped, you need to use the std::cout << result.dump(4, ' ', true) << std::endl; -> {
"name": "\u05d0\u05d9\u05e0\u05e2\u05dc \u05d3\u05d9\u05e0\u05e7"
} |
Wide strings are not supported by the library. An array is created, because under the hood this constructor is chosen: template<typename BasicJsonType, typename T, std::size_t N,
enable_if_t<not std::is_constructible<typename BasicJsonType::string_t, T (&)[N]>::value, int> = 0>
void to_json(BasicJsonType& j, T (&arr)[N])
{
external_constructor<value_t::array>::construct(j, arr);
} I am afraid that right now there is not much that we can do. The library uses UTF-8 internally, and to process wide strings, we would need to translate UTF-16 or UTF-32 to UTF-8. While we have some code for this in the input adapters (allowing to parse from wide strings), it is currently not planned to also have a conversion between different string types. Well... Of course, pull requests would be more than welcome! |
thanks @nlohmann for the comments. I'm using a workaround for now (until this will be supported in this library)
->
|
The simple "this library only supports UTF8" is a good policy, and adding support to this library for std::wstring doesn't feel right. It may be better to try to disable the array overload so that it fails to compile if you try to use a std::wstring with this library. |
What is wrong with also supporting UTF-16 and UTF-32 (even though we only store UTF-8 internally)? |
On the face of it, supporting this would be a good thing. My biggest worry (but not opposition) is that this will distract this library from focusing on the core idea of doing JSON well by having to deal with bugs/issues that come about from supporting multiple encodings properly. For example:
I'm a very-biased adherent to "UTF-8 Everywhere", so take my advice with that grain of salt. If people ever suggest to me anything but string+utf8, I say don't do that. But I understand others do not share that view. Handling encoding issues sounds like the job of a dedicated library for that specific purpose and I don't think it's worth nlohmann::json's time to tackle it -- but maybe it's far easier than I'm painting it out to be! I'm not opposed, just skeptical. |
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. |
But supporting unicode sequences from char16_t and char32_t cannot be misinterpreted. wstring is just a wchar_t container with no associated encoding. |
The library currently treats |
I added an FAQ entry: https://json.nlohmann.me/home/faq/#wide-string-handling |
I'm expecting the following code:
to output:
However, it treats the unicode string as json array, outputs instead:
BTW Thanks for this project. cool stuff happen here.
The text was updated successfully, but these errors were encountered: