1from pathlib
import Path
2from json
import loads, dumps
3from typing
import Any, Callable, Optional, Union
6from .highlighter
import JSONHighlighter, NullHighlighter
10 """A renderable which pretty prints JSON.
13 json (str): JSON encoded data.
14 indent (Union[None, int, str], optional): Number of characters to indent by. Defaults to 2.
15 highlight (bool, optional): Enable highlighting. Defaults to True.
16 skip_keys (bool, optional): Skip keys not of a basic type. Defaults to False.
17 ensure_ascii (bool, optional): Escape all non-ascii characters. Defaults to False.
18 check_circular (bool, optional): Check for circular references. Defaults to True.
19 allow_nan (bool, optional): Allow NaN and Infinity values. Defaults to True.
20 default (Callable, optional): A callable that converts values that can not be encoded
21 in to something that can be JSON encoded. Defaults to None.
22 sort_keys (bool, optional): Sort dictionary keys. Defaults to False.
28 indent: Union[
None, int, str] = 2,
29 highlight: bool =
True,
30 skip_keys: bool =
False,
31 ensure_ascii: bool =
False,
32 check_circular: bool =
True,
33 allow_nan: bool =
True,
34 default: Optional[Callable[[Any], Any]] =
None,
35 sort_keys: bool =
False,
42 ensure_ascii=ensure_ascii,
43 check_circular=check_circular,
49 self.
text = highlighter(json)
50 self.
text.no_wrap =
True
51 self.
text.overflow =
None
57 indent: Union[
None, int, str] = 2,
58 highlight: bool =
True,
59 skip_keys: bool =
False,
60 ensure_ascii: bool =
False,
61 check_circular: bool =
True,
62 allow_nan: bool =
True,
63 default: Optional[Callable[[Any], Any]] =
None,
64 sort_keys: bool =
False,
66 """Encodes a JSON object from arbitrary data.
69 data (Any): An object that may be encoded in to JSON
70 indent (Union[None, int, str], optional): Number of characters to indent by. Defaults to 2.
71 highlight (bool, optional): Enable highlighting. Defaults to True.
72 default (Callable, optional): Optional callable which will be called for objects that cannot be serialized. Defaults to None.
73 skip_keys (bool, optional): Skip keys not of a basic type. Defaults to False.
74 ensure_ascii (bool, optional): Escape all non-ascii characters. Defaults to False.
75 check_circular (bool, optional): Check for circular references. Defaults to True.
76 allow_nan (bool, optional): Allow NaN and Infinity values. Defaults to True.
77 default (Callable, optional): A callable that converts values that can not be encoded
78 in to something that can be JSON encoded. Defaults to None.
79 sort_keys (bool, optional): Sort dictionary keys. Defaults to False.
82 JSON: New JSON object from the given data.
84 json_instance:
"JSON" = cls.__new__(cls)
89 ensure_ascii=ensure_ascii,
90 check_circular=check_circular,
105if __name__ ==
"__main__":
114 help=
"path to file, or - for stdin",
121 help=
"Number of spaces in an indent",
136 except Exception
as error:
None __init__(self, str json, Union[None, int, str] indent=2, bool highlight=True, bool skip_keys=False, bool ensure_ascii=False, bool check_circular=True, bool allow_nan=True, Optional[Callable[[Any], Any]] default=None, bool sort_keys=False)
"JSON" from_data(cls, Any data, Union[None, int, str] indent=2, bool highlight=True, bool skip_keys=False, bool ensure_ascii=False, bool check_circular=True, bool allow_nan=True, Optional[Callable[[Any], Any]] default=None, bool sort_keys=False)