Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 10 additions & 9 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14688,16 +14688,17 @@ def de_json(cls, json_string):
if json_string is None: return None
if isinstance(json_string, str):
# "...can be either a String for plain text..."
if (json_string.startswith('{') and json_string.endswith('}')) or (json_string.startswith('[') and json_string.endswith(']')):
try:
# Check if string is valid json. If so, assume it's json, not plain string
json.loads(json_string)
# If it's valid json, continue to json processing below
except (ValueError, TypeError, json.JSONDecodeError):
# If it's not valid json, return the string as is
return json_string
else:
# Only a JSON object is a serialized RichText. Plain text can look
# like JSON too, e.g. a "[56]" footnote mark, and stays text.
if not (json_string.startswith('{') and json_string.endswith('}')):
return json_string
try:
parsed = json.loads(json_string)
except (ValueError, TypeError):
return json_string
if not isinstance(parsed, dict):
return json_string
json_string = parsed
elif isinstance(json_string, list):
# "...an Array of :class:`RichText`..."
return [RichText.de_json(item) for item in json_string]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,17 @@ def test_json_richtextbold():
assert result.text == 'Test'


def test_json_richtext_plain_text_that_looks_like_json():
# issue #2634: a "[56]" footnote mark is plain text, not a JSON array
json_str = r'{"type": "url", "text": {"type": "superscript", "text": "[56]"}, "url": "https://example.com/#cite_note-56"}'
result = types.RichText.de_json(json_str)
assert isinstance(result, types.RichTextUrl)
assert isinstance(result.text, types.RichTextSuperscript)
assert result.text.text == '[56]'
for text in ['[56]', '[]', '{}x', '123', 'null', '["a", "b"]']:
assert types.RichText.de_json(text) == text


def test_json_richtextitalic():
json_str = r'{"type": "italic", "text": "Test"}'
result = types.RichText.de_json(json_str)
Expand Down
Loading