Skip to content

Commit

Permalink
coerce non-json-serializable objects to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjonbrazil committed Nov 20, 2024
1 parent 7ddd2a4 commit 7887789
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion jc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,16 @@ def json_out(self) -> str:
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:
Expand Down
9 changes: 1 addition & 8 deletions jc/jc_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]

Expand Down Expand Up @@ -42,11 +43,3 @@
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

0 comments on commit 7887789

Please sign in to comment.