Skip to content

UTF-8/Unicode escape and dump #330

Closed

Description

@esproof

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"}

Activity

  1. changed the title [-]UTF-8/Unicode escape and dump[/-] [+]UTF-8/Unicode escaping and dumping[/+] on Oct 14, 2016
  2. changed the title [-]UTF-8/Unicode escaping and dumping[/-] [+]UTF-8/Unicode escape and dump[/+] on Oct 14, 2016
  3. nlohmann commented on Oct 15, 2016

    @nlohmann
    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.

  4. added this to the Release 3.0.0 milestone on Dec 12, 2016
  5. added a commit that references this issue on Jul 12, 2017
  6. self-assigned this
    on Jul 12, 2017
  7. nlohmann commented on Jul 12, 2017

    @nlohmann
    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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions