Return a json string representation of an object. Args: obj: The object to dump pretty: Whether to pretty print the json. If true, the json will be indented with 2 spaces (if no indent is provided as part of kwargs) **kwargs: Additional arguments to pass to j
(obj: Any, *, pretty: bool = False, **kwargs: Any)
| 14 | |
| 15 | |
| 16 | def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str: |
| 17 | """Return a json string representation of an object. |
| 18 | |
| 19 | Args: |
| 20 | obj: The object to dump |
| 21 | pretty: Whether to pretty print the json. If true, the json will be |
| 22 | indented with 2 spaces (if no indent is provided as part of kwargs) |
| 23 | **kwargs: Additional arguments to pass to json.dumps |
| 24 | |
| 25 | Returns: |
| 26 | A json string representation of the object |
| 27 | """ |
| 28 | if "default" in kwargs: |
| 29 | raise ValueError("`default` should not be passed to dumps") |
| 30 | try: |
| 31 | if pretty: |
| 32 | indent = kwargs.pop("indent", 2) |
| 33 | return json.dumps(obj, default=default, indent=indent, **kwargs) |
| 34 | else: |
| 35 | return json.dumps(obj, default=default, **kwargs) |
| 36 | except TypeError: |
| 37 | if pretty: |
| 38 | indent = kwargs.pop("indent", 2) |
| 39 | return json.dumps(to_json_not_implemented(obj), indent=indent, **kwargs) |
| 40 | else: |
| 41 | return json.dumps(to_json_not_implemented(obj), **kwargs) |
| 42 | |
| 43 | |
| 44 | def dumpd(obj: Any) -> Any: |