Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make json viewer more clear (reopen) #2350

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@


DISABLE_KEYLEN_ERROR = False
JSONDESERIALIZABLE_INDENT = 2
JSONDESERIALIZABLE_PARSE_OUTPUT = False
JSONDESERIALIZABLE_SKIP_NONE = True

logger = logging.getLogger('TeleBot')

Expand Down Expand Up @@ -97,12 +100,34 @@ def check_json(json_type, dict_copy = True):
else:
raise ValueError("json_type should be a json dict or string.")

def __str__(self):
d = {
x: y.__dict__ if hasattr(y, '__dict__') else y
for x, y in self.__dict__.items()
}
return str(d)
@staticmethod
def _default(obj) -> Union[dict, str]:
if not isinstance(obj, JsonDeserializable):
return repr(obj) if JSONDESERIALIZABLE_PARSE_OUTPUT else obj
else:
return {
attr: getattr(obj, attr)
for attr in filter(
lambda x: not x.startswith("_"),
obj.__dict__,
)
if getattr(obj, attr) is not None or not JSONDESERIALIZABLE_SKIP_NONE
}

def __str__(self) -> str:
if JSONDESERIALIZABLE_PARSE_OUTPUT:
return json.dumps(
self,
default=JsonDeserializable._default,
indent=JSONDESERIALIZABLE_INDENT, ensure_ascii=False)
else:
return str(
{
x: JsonDeserializable._default(y) if isinstance(y, JsonDeserializable) else y
for x, y in self.__dict__.items()
if y is not None or not JSONDESERIALIZABLE_SKIP_NONE
}
)


class Update(JsonDeserializable):
Expand Down Expand Up @@ -10726,4 +10751,4 @@ def de_json(cls, json_string):
obj = cls.check_json(json_string)
return cls(**obj)



Loading