-
-
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
from_json<std::wstring> is treated as an array on latest MSVC #2453
Comments
|
Ah, sorry about that. Assumed that since #656 there was general support for
|
The library stores strings internally as |
I also have the same problem. My Json is in |
@Teles1 Can you share a complete example? |
@nlohmann, I wrote a test to show @Teles1 issue (since I stumbled upon the same thing yesterday):
and the output is:
|
Yes, this behavior is known. |
I added an FAQ entry: https://json.nlohmann.me/home/faq/#wide-string-handling |
Why not just get/set a wide string as an integer array? Conversion to Utf8 is unreliable. I have seen this fail. Wide strings are used in C++ programs because they can be used with string parsing and modification functions, whereas Utf8 does not work well for this. We already have a JsonSetWString(nlohmann::json, std::wstring) function but it's awkward and I hate to explain it to the end users. Why not just build this into the library, have it 100% working correctly, and be done with it?
|
I solved the problem by adding a custom constructor for our String and WString class: String::String(nlohmann::json& j3)
{
if (j3.is_string())
{
assign(j3);
return;
}
else if (j3.is_number_float())
{
double f = j3;
(*this).assign(String(f));
}
else if (j3.is_number_unsigned())
{
uint64_t i = j3;
(*this).assign(String(i));
}
else if (j3.is_number_integer())
{
int64_t i = j3;
(*this).assign(String(i));
}
}
WString::WString(nlohmann::json& j3)
{
if (j3.is_array())
{
for (int n = 0; n < j3.size(); ++n)
{
if (not j3[n].is_number_unsigned())
{
clear();
return;
}
(*this) += WChr(j3[n]);
}
}
else
{
String s = String(j3);
if (not s.empty()) assign(WString(s));
}
} It appears that std::wstring gets treated as an array when written to a json object, and it is possible to convert the wide string to UTF-8 if saving a string is preferred, I'm satisfied with the behavior now. |
Where shall we exactly put this ? For instance, this is where my code is failing :
With this error :
|
Are you sure that's the line that's causing the error? That error doesn't make any sense there. |
This is the code that I have been using :
And this is the error that I have gotten : And here is the json :
|
Okay, so that makes more sense than the previous code. The JSON contains a string, and getstd::u16string looks for an array of characters. I'm not sure if there is a way to make this work for JSON that contains strings with characters that are not UTF-8. |
So per my understanding, Wth the current state of the library, it is impossible to read ŞĞÜİ letters while using the nlohmann json library. Is this correct ? |
What is the issue you have?
When using the
get
method with astd::wstring
/std::u8string
etc. thefrom_json(const BasicJsonType& j, ConstructibleArrayType& arr)
instantiation is selected. As thebasic_json
value is correctly identified as a string, thefrom_json
will throw an error.std::string
still works as expected though.Can you provide a small but working code example?
Which compiler and operating system are you using?
Which version of the library did you use?
develop
branchThe text was updated successfully, but these errors were encountered: