-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path_data.py
More file actions
21 lines (18 loc) · 577 Bytes
/
_data.py
File metadata and controls
21 lines (18 loc) · 577 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import json
import re
from io import IOBase
from typing import Any
_RE_PROBABLY_MALFORMED = re.compile(r"[\{\}\[\]]")
def load_data(data: object) -> Any:
if isinstance(data, str):
try:
return json.loads(data)
except json.JSONDecodeError:
# Overly simple way to detect a malformed JSON document vs a
# top-level string only document
if _RE_PROBABLY_MALFORMED.search(data):
raise
return data
if isinstance(data, IOBase):
return json.loads(data.read())
return data