-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: add option to save json without escaping forward slashes #62830
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
base: main
Are you sure you want to change the base?
Changes from all commits
d1f5292
aa7a7b0
675a80b
8a1af64
cbc3186
6de31f8
4a6a0cf
861495b
09e16c8
3ef9972
436bfc2
5d2f8a0
8b676e8
dfc457c
3776f2c
f75c97d
e92db90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,7 @@ def to_json( | |
| indent: int = ..., | ||
| storage_options: StorageOptions = ..., | ||
| mode: Literal["a", "w"] = ..., | ||
| escape_forward_slashes: Literal[True, False] = ..., | ||
| ) -> None: ... | ||
|
|
||
|
|
||
|
|
@@ -133,6 +134,7 @@ def to_json( | |
| indent: int = ..., | ||
| storage_options: StorageOptions = ..., | ||
| mode: Literal["a", "w"] = ..., | ||
| escape_forward_slashes: Literal[True, False] = ..., | ||
| ) -> str: ... | ||
|
|
||
|
|
||
|
|
@@ -151,6 +153,7 @@ def to_json( | |
| indent: int = 0, | ||
| storage_options: StorageOptions | None = None, | ||
| mode: Literal["a", "w"] = "w", | ||
| escape_forward_slashes: Literal[True, False] = True, | ||
| ) -> str | None: | ||
| if orient in ["records", "values"] and index is True: | ||
| raise ValueError( | ||
|
|
@@ -218,6 +221,23 @@ def to_json( | |
| ) as handles: | ||
| handles.handle.write(s) | ||
| else: | ||
| if not escape_forward_slashes: | ||
| pattern = r"\/" | ||
| i = 1 | ||
| new_s = "" | ||
| skip = False | ||
| for letter in s: | ||
| if skip is False: | ||
| if letter + s[i] == pattern: | ||
| new_s = new_s + r"/" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me, this shouldn't be done in python as a post-processing step. Ideally, you should change the behavior on the C implementation. |
||
| skip = True | ||
| else: | ||
| new_s = new_s + letter | ||
| else: | ||
| skip = False | ||
| if i < len(s) - 1: | ||
| i = i + 1 | ||
| return new_s | ||
| return s | ||
| return None | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2286,6 +2286,25 @@ def test_to_json_ea_null(): | |
| assert result == expected | ||
|
|
||
|
|
||
| def test_to_json_escape_forward_slashes(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you create a test that also escapes forward slash? |
||
| df = DataFrame( | ||
| { | ||
| "path1": Series(["/escape/three/slashes"], dtype="str"), | ||
| "path2": Series(["/"], dtype="str"), | ||
| "path3": Series(["ending/"], dtype="str"), | ||
| "path4": Series(["/beginning"], dtype="str"), | ||
| "path5": Series(["/multiples//multiple"], dtype="str"), | ||
| "path6": Series(["//"], dtype="str"), | ||
| } | ||
| ) | ||
| result = df.to_json(orient="records", escape_forward_slashes=False) | ||
| expected1 = '[{"path1":"/escape/three/slashes","path2":"/","path3":"ending/"' | ||
| expected2 = ',"path4":"/beginning","path5":"/multiples//multiple","path6":"//"}]' | ||
| expected = expected1 + expected2 | ||
|
|
||
| assert result == expected | ||
|
|
||
|
|
||
| def test_read_json_lines_rangeindex(): | ||
| # GH 57429 | ||
| data = """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will only apply if you are not writing to a file.