Encodes a JSON object from arbitrary data. Args: data (Any): An object that may be encoded in to JSON indent (Union[None, int, str], optional): Number of characters to indent by. Defaults to 2. highlight (bool, optional): Enable highlighting. Defaults to
(
cls,
data: Any,
indent: Union[None, int, str] = 2,
highlight: bool = True,
skip_keys: bool = False,
ensure_ascii: bool = False,
check_circular: bool = True,
allow_nan: bool = True,
default: Optional[Callable[[Any], Any]] = None,
sort_keys: bool = False,
)
| 52 | |
| 53 | @classmethod |
| 54 | def from_data( |
| 55 | cls, |
| 56 | data: Any, |
| 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, |
| 65 | ) -> "JSON": |
| 66 | """Encodes a JSON object from arbitrary data. |
| 67 | |
| 68 | Args: |
| 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. |
| 80 | |
| 81 | Returns: |
| 82 | JSON: New JSON object from the given data. |
| 83 | """ |
| 84 | json_instance: "JSON" = cls.__new__(cls) |
| 85 | json = dumps( |
| 86 | data, |
| 87 | indent=indent, |
| 88 | skipkeys=skip_keys, |
| 89 | ensure_ascii=ensure_ascii, |
| 90 | check_circular=check_circular, |
| 91 | allow_nan=allow_nan, |
| 92 | default=default, |
| 93 | sort_keys=sort_keys, |
| 94 | ) |
| 95 | highlighter = JSONHighlighter() if highlight else NullHighlighter() |
| 96 | json_instance.text = highlighter(json) |
| 97 | json_instance.text.no_wrap = True |
| 98 | json_instance.text.overflow = None |
| 99 | return json_instance |
| 100 | |
| 101 | def __rich__(self) -> Text: |
| 102 | return self.text |