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 escape and dump #330

Closed
esproof opened this issue Oct 14, 2016 · 2 comments
Closed

UTF-8/Unicode escape and dump #330

esproof opened this issue Oct 14, 2016 · 2 comments

Comments

@esproof
Copy link

esproof commented Oct 14, 2016

example:

json js = json::parse(R"({ "escaped" : "\u6211\u662f\u4e2d\u56fd\u4eba" })");
std::cout << js.dump() << std::endl;

result (as utf-8):
{"escaped":"我是中国人"}

question:

How can I get the same result as the entered string?
such as:
{"escaped":"\u6211\u662f\u4e2d\u56fd\u4eba"}

@esproof esproof changed the title UTF-8/Unicode escape and dump UTF-8/Unicode escaping and dumping Oct 14, 2016
@esproof esproof changed the title UTF-8/Unicode escaping and dumping UTF-8/Unicode escape and dump Oct 14, 2016
@nlohmann
Copy link
Owner

There is currently now feature implemented to do this. It could be added as additional parameter to the dump function. In Python's json.dump function, there is a parameter ensure_ascii for this:

If ensure_ascii is true (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only. If ensure_ascii is false, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.

Internally, function escape_string needs to be changed for this. It currently only escapes control characters to \uxxxx.

@nlohmann nlohmann added this to the Release 3.0.0 milestone Dec 12, 2016
@nlohmann nlohmann modified the milestones: Release 3.0.1, Release 3.0.0 Jan 16, 2017
nlohmann added a commit that referenced this issue Jul 12, 2017
add ensure_ascii parameter to dump. #330
@nlohmann nlohmann modified the milestones: Release 3.0.0, Release 3.0.1 Jul 12, 2017
@nlohmann nlohmann self-assigned this Jul 12, 2017
@nlohmann
Copy link
Owner

The feature is implemented with #654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants