1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-05 00:49:03 +02:00

coerce non-json-serializable objects to strings

This commit is contained in:
Kelly Brazil
2024-11-20 13:49:27 -08:00
parent 7ddd2a4ce2
commit 7887789d0d
3 changed files with 9 additions and 10 deletions

View File

@ -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

View File

@ -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:

View File

@ -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