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

Add support for preserving Unicode characters in jsonpatch CLI #127

Merged
merged 1 commit into from
Mar 4, 2021

Conversation

Genzer
Copy link
Contributor

@Genzer Genzer commented Mar 3, 2021

If the JSON content contains some Unicode characters, the jsonpatch
final output will encode the Unicode character using ASCII (i.e
\u0394). This behaviour comes from the module json.dump() governed
by a flag ensure_ascii1.

For example:

/* patch.json */
[{
  "op": "add",
  "path": "/SomeUnicodeSamples",
  "value": "𝒞𝘋𝙴𝓕ĢȞỈ𝕵 đ áê 🤩 äÄöÖüÜß"
}]

After applying the patch on an empty source file {}, this is the
output:

{"SomeUnicodeSamples": "\ud835\udc9e\ud835\ude0b...\u00fc\u00dc\u00df"}

This commits add a flag -u|--preserve-unicode into the jsonpatch CLI
to configure the behaviour of json.dump's ensure_ascii flag.

Using the --preserve-unicode flag, the final output will print the
Unicode characters as-is without any encoding.

If the JSON content contains some Unicode characters, the jsonpatch
final output will encode the Unicode character using ASCII (i.e
`\u0394`). This behaviour comes from the module `json.dump()` governed
by a flag `ensure_ascii`[1].

For example:

```json
/* patch.json */
[{
  "op": "add",
  "path": "/SomeUnicodeSamples",
  "value": "𝒞𝘋𝙴𝓕ĢȞỈ𝕵 đ áê 🤩 äÄöÖüÜß"
}]
```

After applying the patch on an empty source file `{}`, this is the
output:

```json
{"SomeUnicodeSamples": "\ud835\udc9e\ud835\ude0b...\u00fc\u00dc\u00df"}
```

This commit adds a flag `-u|--preserve-unicode` in the jsonpatch CLI
to configure the behaviour of `json.dump`'s `ensure_ascii` flag.

Using the `--preserve-unicode` flag, the cli will print the Unicode
characters as-is without any encoding.

[1]: https://docs.python.org/3/library/json.html#basic-usage
@coveralls
Copy link

coveralls commented Mar 3, 2021

Coverage Status

Coverage remained the same at 65.846% when pulling 974d54f on Genzer:master into dbea3db on stefankoegl:master.

@stefankoegl stefankoegl merged commit cc37e25 into stefankoegl:master Mar 4, 2021
@stefankoegl
Copy link
Owner

Looks good, thanks :)

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

Successfully merging this pull request may close these issues.

None yet

4 participants