Repository: Textualize/rich

Issue:
[BUG] print_json produces invalid JSON with long values
**Read the docs**
✔️ : You might find a solution to your problem in the [docs](https://rich.readthedocs.io/en/latest/introduction.html) -- consider using the search function there.

**Describe the bug**
`print_json` produces invalid JSON when input includes values wider than console: 
```console
$ python repro.py | jq
parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 3, column 3
```
**To Reproduce**
repro.py:
```python
import rich

data = {'long_str': "x" * 1000, 'long_int': int(2e128)}
rich.reconfigure(width=100)
rich.print_json(data=data)
```

**Platform**
Reproduced in alacritty on macOS and Arch.

**Diagnose**
`diagnose`:
```
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=101 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 51                                                            │
│    is_alt_screen = False                                                         │
│ is_dumb_terminal = False                                                         │
│   is_interactive = True                                                          │
│       is_jupyter = False                                                         │
│      is_terminal = True                                                          │
│   legacy_windows = False                                                         │
│         no_color = False                                                         │
│          options = ConsoleOptions(                                               │
│                        size=ConsoleDimensions(width=101, height=51),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=101,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=51,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=101, height=51)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 101                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
```
`_windows`:

```
platform="Linux"
WindowsConsoleFeatures(vt=False, truecolor=False)
```

```console
$ pip freeze | grep rich
48:rich==10.13.0
```
**Did I help?**

If I was able to resolve your problem, consider [sponsoring](https://github.com/sponsors/willmcgugan) my work on Rich, or [buy me a coffee](https://ko-fi.com/willmcgugan) to say thanks.



Discussion:

Resolving patch:
diff --git a/rich/console.py b/rich/console.py
--- a/rich/console.py
+++ b/rich/console.py
@@ -1679,7 +1679,7 @@ def print_json(
                 default=default,
                 sort_keys=sort_keys,
             )
-        self.print(json_renderable)
+        self.print(json_renderable, soft_wrap=True)
 
     def update_screen(
         self,


Test patch:
diff --git a/tests/test_rich_print.py b/tests/test_rich_print.py
--- a/tests/test_rich_print.py
+++ b/tests/test_rich_print.py
@@ -1,4 +1,5 @@
 import io
+import json
 
 import rich
 from rich.console import Console
@@ -38,6 +39,27 @@ def test_rich_print_json():
     assert result == expected
 
 
+def test_rich_print_json_round_trip():
+    data = ["x" * 100, 2e128]
+    console = rich.get_console()
+    with console.capture() as capture:
+        rich.print_json(data=data, indent=4)
+    result = capture.get()
+    print(repr(result))
+    result_data = json.loads(result)
+    assert result_data == data
+
+
+def test_rich_print_json_no_truncation():
+    console = rich.get_console()
+    with console.capture() as capture:
+        rich.print_json(f'["{"x" * 100}", {int(2e128)}]', indent=4)
+    result = capture.get()
+    print(repr(result))
+    assert ("x" * 100) in result
+    assert str(int(2e128)) in result
+
+
 def test_rich_print_X():
     console = rich.get_console()
     output = io.StringIO()