diff --git a/CHANGELOG b/CHANGELOG index 43fff3a4..80a19377 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ jc changelog -20241119 v1.25.4 +20241120 v1.25.4 - Add `ipconfig` command parser (`ipconfig` for Windows) - Enhance `ethtool` parser to support `link_partner_advertised_link_modes` - Enhance `ifconfig` parser to support `utun` interfaces with assigned IPv4 addresses on macOS @@ -16,6 +16,7 @@ jc changelog - Fix `uptime` parser for data that contains `user` instead of `users` - Fix `yaml` parser to support values that start with an equal sign - Enhance `jc.utils.convert_size_to_int()` to add `posix_mode` and `decimal_bias` parameters +- Enhance cli to coerce any non-JSON-serializable objects to a string 20240609 v1.25.3 - Enhance `bluetoothctl` parser with added `battery_percentage` field diff --git a/jc/cli.py b/jc/cli.py index 41c8358d..72ef7997 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -401,11 +401,16 @@ class JcCli(): self.json_indent = 2 self.json_separators = None + # Convert any non-serializable object to a string + def string_serializer(data): + return str(data) + j_string = json.dumps( self.data_out, indent=self.json_indent, separators=self.json_separators, - ensure_ascii=self.ascii_only + ensure_ascii=self.ascii_only, + default=string_serializer ) if not self.mono and PYGMENTS_INSTALLED: diff --git a/jc/jc_types.py b/jc/jc_types.py index d34af271..6377559f 100644 --- a/jc/jc_types.py +++ b/jc/jc_types.py @@ -3,6 +3,7 @@ import sys from typing import Any, Dict, List, Tuple, Iterator, Optional, Union +CustomColorType = Dict[Any, str] JSONDictType = Dict[str, Any] StreamingOutputType = Iterator[Union[JSONDictType, Tuple[BaseException, str]]] @@ -42,11 +43,3 @@ if sys.version_info >= (3, 8): else: ParserInfoType = Dict TimeStampFormatType = Dict - - -try: - from pygments.token import (Name, Number, String, Keyword) - CustomColorType = Dict[Union[Name.Tag, Number, String, Keyword], str] - -except Exception: - CustomColorType = Dict # type: ignore