-
-
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
Does it support chinese character? #1022
Comments
Yes, the library full supports UTF-8. You need to make sure whether your IDE actually uses UTF-8, which is sometimes an issue with MSVC. See also #694. |
You say the application crashed. Could you please provide more information: compiler version, concrete example, library version? |
Thank you for your patience. During servual hours search from internet, I tried some methods on MSVC, but all can't get correct result. I guess the reason is that MSVC treat the "中文" as non-utf-8 character(maybe ASCII, Unicode-16, I'm not sure.) while compile code or/and runing the program. As your metion, I tried to compile the code with MingW, it works fine due to MingW using Utf-8 as default setting. My full code is as below: #include <iostream>
#include <string>
#include "json.hpp"
using namespace std;
using json = nlohmann::json;
int main() {
try
{
json j;
j["chinese"] = "中文";
string s = j.dump();
cout << s.c_str() << endl;
}
catch (const std::exception& e)
{
cout << e.what() << endl;
}
return 0;
} when I run it in Visual Studio 2017, the command line will show as following: Addtional Infomation(by typing 'systeminfo' in command line): Visual Studio Project Setting: |
The error message indicates that #include <string>
#include <iostream>
int main() {
std::string s = "中文";
for (size_t i = 0; i < s.size(); ++i)
{
std::cout << i << " " << std::hex << static_cast<int>(static_cast<uint8_t>(s[i])) << std::endl;
}
} With UTF-8, this should yield:
|
I have to add a flag "/utf-8" to compile the program, otherwise I still get the same error - invalid UTF-8 byte. Reference here: https://msdn.microsoft.com/zh-cn/library/mt708819.aspx. `#include using namespace std; using json = nlohmann::json; int main() {
}` the result as following: 1 b8 2 ad 3 e6 4 96 5 87 {"chinese":"涓枃"} |
You should encode UTF-8 out of source. C++ does not define a text mapping format for the source files, hence why you should keep it in resource files. Adding that utf-8 switch is not really compliant. The standard states:
As such C++ does not have chinese defined in the But yes, this library supports chinese fine in JSON, but the source code is not under this library's control for the character set, it can only rely on the (EDIT: 'encode it' meaning use the |
Thanks for all, I found the root case: I try to write the json string to a file(following below code), "data.json" encoding with UTF-8, and can display chinese character correctly. So, the problem is caused by windows 10 bug or command line's bug. string json_file = "data.json";
ofstream json_output(json_file);
json_output << j;// the json object
json_output.close(); @OvermindDL1 Thank you for your tips. Yes, I did try to change the source code file encoding. |
Even that is not guaranteed to work depending on the compiler (MSVC is especially bad at it). It is better to not depend on encoding at all and instead follow the spec to the letter.
Windows is not UTF-8, it is USC-16 or whatever it's called (essentially a garbage format to be honest). But yes, store UTF-8 in a non-source file and read it in should always work or it is a bug. |
Can we close the issue? |
sure |
Perhapse the solution to display Chinese in Windows Console correct is too late, just put a sulotion from internet in here to help others fix this kind of issues quickly:
#include using namespace std; // for convenience int main() { #ifdef _WIN32
} |
对于std::string类型字符串的处理: 可以在from_json.hpp中下列函数体内添加对字符串是否进行UTF8ToGBK转码的处理: vs下,在上述相应位置打个断点,一看便知。 上述处理过程,不仅是针对Windows(默认GBK)控制台输出UTF8乱码问题,同时也是针对一些程序会输出日志到DebugView等工具中的GBK乱码问题。 在对上述代码进行更改后,要特别注意您的json文件本身是否已经是UTF-8的格式,若是,则不能让to_json.hpp下代码重复进行GBKToUTF8的转换。所以,我在to_json下添加了一个bool变量开关来自行判断何时需要转换。同理,在from_json下也添加了相应的bool开关。 |
This URL https://unicode-table.com/ has changed to https://symbl.cc/ |
I try to input some chinese characters, the application is crashed. The code as following below:
json j;
j["chinese"] = "中文";
string s = j.dump();
cout << s.c_str() << endl;
Can you tell me how to make it works?
The text was updated successfully, but these errors were encountered: