-
-
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
JSON to wstring? #1921
Comments
As long as you are following the C++ standard, You might also override using json = nlohmann::basic_json<std::map, std::vector, QString>; |
This is the output (part of it..) we get from OneDrive when enumerating the root for example: When that is converted to a wstring, the output is actually fine. I printed out (to a PLog debug output) the actual JSONs we got back from OneDrive (since we dig into it via response["values"].at(i)) and all JSONs up to the top output the incorrect characters. |
Simplest example:
|
Windows console might not correctly handle UTF8 for Alternatively, you can print out the hex values like the following code (live code) and decode it (e.g. using this service) to see if the characters are not corrupted: #include <nlohmann/json.hpp>
#include <iostream>
int main(void)
{
nlohmann::json j = {{"happy", "_9_Pi\u00e8ces jointes"}};
std::string str = j.dump();
std::cout << std::hex;
for(const uint8_t& ch: str)
{
std::cout << static_cast<int>(ch) << " ";
}
std::cout << std::endl;
return 0;
} The output of above code on GCC is:
|
This is file output, not console, via PLog library. It outputs the correct
data if the string is printed.
It also prints the correct data if I convert the json["happy"] to wstring.
Kind regards, Dejan.
…On Sun, 2 Feb 2020, 18:26 Isaac Nickaein ***@***.*** wrote:
Windows console might not correctly handle UTF8 for std::string. Can you
write the output string to a file instead, and check its contents with an
IDE that supports UTF8 (e.g.g vscode)?
Alternatively, you can print out the hex values like the following code (live
code <https://wandbox.org/permlink/msVEqpkYYAxM3KPP>) and decode it (e.g. using
this service <https://onlineutf8tools.com/convert-hexadecimal-to-utf8>)
to see if the characters are not corrupted:
#include <nlohmann/json.hpp>
#include <iostream>
int main(void)
{
nlohmann::json j = {{"happy", "_9_Pi\u00e8ces jointes"}};
std::string str = j.dump();
std::cout << std::hex;
for(const uint8_t& ch: str)
{
std::cout << static_cast<int>(ch) << " ";
}
std::cout << std::endl;
return 0;
}
The output of above code on GCC is:
c3 a8 63 65 73 20 6a 6f 69 6e 74 65 73 22 7d
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1921?email_source=notifications&email_token=AKD4NRMG6T2QM25KKYNO6T3RA363LA5CNFSM4KOKREX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKR4EOI#issuecomment-581157433>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKD4NRKNTEVGEFVD4A7DTW3RA363LANCNFSM4KOKREXQ>
.
|
Also, MSVC might not interpret the input file as UTF8 so any string literal could be corrupted before the parsing kicks in. More info and the solutions: https://stackoverflow.com/questions/840065 Can you try the above code on MSVC with a string you have issue with and compare it to the output of live code? |
Your code throws an exception in MSVC: The problem is definitely not the editor for the original issue - we get the string data from OneDrive as a JSON body, and use json::parse to get a JSON object. |
The online compile gives proper string output: (I added std::cout << str;) This is what MSVC outputs for the dump: Now if I convert that to a wstring that is not an issue - I get the correct output. I am not sure where the issue is now :) |
I got my hands on a MSVC compiler and tried the example code (#1921 (comment)) with
In addition, to simulate receiving the JSON string, I replaced the following line, nlohmann::json j = {{"happy", "_9_Pi\u00e8ces jointes"}}; with: std::ifstream fin("input.txt");
auto j = nlohmann::json::parse(fin); where
This also outputs the exact bytes as above case. Note that in this case, there is no need for
|
UR right. It seems we need to change JSONs to wstring ourselves :( |
Please see the solution |
Is there a way to do:
wstring val = json["Name"];
?
The problem I am facing is that MSVC does not use UTF8 strings in std::string, and even something as simple as French accented letters turn into gibberish.
I know I can convert json["Name"] TO wstring, but the issue would remain :(
I tried both 3.1.1 and the latest 3.7.3, using MSVC 2017/2019.
Q2: I reckon u8string is not supported yet?
The text was updated successfully, but these errors were encountered: