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

UTF-8/Unicode error #982

Closed
frank83413 opened this issue Feb 24, 2018 · 6 comments
Closed

UTF-8/Unicode error #982

frank83413 opened this issue Feb 24, 2018 · 6 comments
Labels

Comments

@frank83413
Copy link

I have some problems with parsing UTF-8
Here is my code

#include <iostream>
#include <string.h> 
#include "nlohmann/json.hpp"
using json = nlohmann::json;

int main()
{
	json j1 = "{\"name\":\"\\u6211\"}"_json;
	json j2 = json::parse(R"({ "name" : "\u6211" })");
        std::cout << j1.dump() << std::endl;
	std::cout << j2.dump() << std::endl;
	system("pause");
	return 0;
}

I think I should get output like :

{"name":"我"}
{"name":"我"}

but I only get:

{"name":"?

If I do auto temp = j1["name"].get<std::string>(); temp will have Invalid character in string.

Are there anyone could help me to fix it?

@JoelStienlet
Copy link

Hello,
I've just copy/paste your code and run it on my fedora 27, looks like it's working, I get:
[stienlet@pc24 JSON]$ ./a.out
{"name":"我"}
{"name":"我"}
My guess is that the fonts are not supported by your terminal, but the program itself works correctly.

@frank83413
Copy link
Author

Thanks for your reply

But I try

std::string a = "{\"name\":\"\u6211\"}";
std::cout << a;

It can cout {"name":"我"} in terminal.

By the way I compile with visual studio 2015 and 2017,using version 3.1.1.
Will compiler affect that?
I just add json-3.1.1\include to include path and include nlohmann/json.hpp. Did I miss any steps?

@nlohmann
Copy link
Owner

Could you try to output j1.dump(-1, ' ', true) and j2.dump(-1, ' ', true)? This should display all characters in an escaped form. If you see \u6211 in the output, you can be sure that the creation of the JSON object works.

But I think this could be a MSVC issue - I am not using it myself, but I heard a lot of issues in the past where the encoding was not set to Unicode, so there were issues parsing, storing or displaying UTF-8.

I tried the code above on macOS, and it works without problems. We also have extensive UTF-8 tests running on MSVC 2015 and MSVC 2017, so this all should work...

@nlohmann nlohmann added the platform: visual studio related to MSVC label Feb 24, 2018
@frank83413
Copy link
Author

Thanks for your reply.

Yes j1.dump(-1, ' ', true) and j2.dump(-1, ' ', true) works!!
"the encoding was not set to Unicode" what does this mean? How to set the encoding to Unicode?

@nlohmann
Copy link
Owner

I do not know. But the value seems to have been successfully stored, so this is just an output issue.

Maybe https://stackoverflow.com/questions/32330970/c11-stdcout-string-literal-in-utf-8-to-windows-cmd-console-visual-stu may help. In any case, I think StackOverflow may help you more, because this seems not to be a library issue.

@frank83413
Copy link
Author

Thank you very much!
I will try.

@nlohmann nlohmann closed this as completed Mar 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants