summaryrefslogtreecommitdiff
path: root/sphinx/util/jsonimpl.py
blob: c5336a1953bb788964e78a6bdbe1fe72b38fd44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
    sphinx.util.jsonimpl
    ~~~~~~~~~~~~~~~~~~~~

    JSON serializer implementation wrapper.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import json
import warnings
from collections import UserString
from typing import Any, IO

from sphinx.deprecation import RemovedInSphinx40Warning


warnings.warn('sphinx.util.jsonimpl is deprecated',
              RemovedInSphinx40Warning, stacklevel=2)


class SphinxJSONEncoder(json.JSONEncoder):
    """JSONEncoder subclass that forces translation proxies."""
    def default(self, obj: Any) -> str:
        if isinstance(obj, UserString):
            return str(obj)
        return super().default(obj)


def dump(obj: Any, fp: IO, *args, **kwds) -> None:
    kwds['cls'] = SphinxJSONEncoder
    json.dump(obj, fp, *args, **kwds)


def dumps(obj: Any, *args, **kwds) -> str:
    kwds['cls'] = SphinxJSONEncoder
    return json.dumps(obj, *args, **kwds)


def load(*args, **kwds) -> Any:
    return json.load(*args, **kwds)


def loads(*args, **kwds) -> Any:
    return json.loads(*args, **kwds)