File: _data.py

package info (click to toggle)
python-jsonpath 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,028 kB
  • sloc: python: 9,473; makefile: 6
file content (21 lines) | stat: -rw-r--r-- 577 bytes parent folder | download
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