2024-05-04 15:25:05 -04:00
|
|
|
import json
|
|
|
|
from datetime import datetime, timedelta
|
2024-06-04 14:20:01 -04:00
|
|
|
from typing import Any
|
2024-05-04 15:25:05 -04:00
|
|
|
|
2024-05-04 20:55:11 -04:00
|
|
|
from redbot.core.bot import Red
|
|
|
|
|
2024-05-06 21:39:43 -04:00
|
|
|
from ..models.base import AuroraBaseModel
|
2024-07-06 12:03:49 -04:00
|
|
|
from ..models.type import Type
|
2024-05-04 15:25:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
2024-06-04 14:20:01 -04:00
|
|
|
def default(self, o) -> Any:
|
|
|
|
match o:
|
|
|
|
case datetime():
|
|
|
|
return int(o.timestamp())
|
|
|
|
case timedelta():
|
2024-08-19 14:49:04 -04:00
|
|
|
from ..utilities.utils import timedelta_to_string
|
|
|
|
return timedelta_to_string(o)
|
2024-06-04 14:20:01 -04:00
|
|
|
case AuroraBaseModel():
|
|
|
|
return o.dump()
|
2024-07-06 12:03:49 -04:00
|
|
|
case Type():
|
2024-07-06 12:29:43 -04:00
|
|
|
return o.key
|
2024-06-04 14:20:01 -04:00
|
|
|
case Red():
|
|
|
|
return None
|
|
|
|
case _:
|
|
|
|
return super().default(o)
|
2024-05-04 15:25:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
# This is a wrapper around the json module's dumps function that uses our custom JSONEncoder class
|
|
|
|
def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
|
|
|
|
allow_nan=True, indent=None, separators=None,
|
|
|
|
default=None, sort_keys=False, **kw) -> str:
|
|
|
|
"""Serialize ``obj`` to a JSON formatted ``str``.
|
|
|
|
|
|
|
|
If ``skipkeys`` is true then ``dict`` keys that are not basic types
|
|
|
|
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
|
|
|
|
instead of raising a ``TypeError``.
|
|
|
|
|
|
|
|
If ``ensure_ascii`` is false, then the return value can contain non-ASCII
|
|
|
|
characters if they appear in strings contained in ``obj``. Otherwise, all
|
|
|
|
such characters are escaped in JSON strings.
|
|
|
|
|
|
|
|
If ``check_circular`` is false, then the circular reference check
|
|
|
|
for container types will be skipped and a circular reference will
|
|
|
|
result in an ``RecursionError`` (or worse).
|
|
|
|
|
|
|
|
If ``allow_nan`` is false, then it will be a ``ValueError`` to
|
|
|
|
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
|
|
|
|
strict compliance of the JSON specification, instead of using the
|
|
|
|
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
|
|
|
|
|
|
|
|
If ``indent`` is a non-negative integer, then JSON array elements and
|
|
|
|
object members will be pretty-printed with that indent level. An indent
|
|
|
|
level of 0 will only insert newlines. ``None`` is the most compact
|
|
|
|
representation.
|
|
|
|
|
|
|
|
If specified, ``separators`` should be an ``(item_separator, key_separator)``
|
|
|
|
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
|
|
|
|
``(',', ': ')`` otherwise. To get the most compact JSON representation,
|
|
|
|
you should specify ``(',', ':')`` to eliminate whitespace.
|
|
|
|
|
|
|
|
``default(obj)`` is a function that should return a serializable version
|
|
|
|
of obj or raise TypeError. The default simply raises TypeError.
|
|
|
|
|
|
|
|
If *sort_keys* is true (default: ``False``), then the output of
|
|
|
|
dictionaries will be sorted by key.
|
|
|
|
"""
|
|
|
|
return json.dumps(
|
|
|
|
obj,
|
|
|
|
cls=JSONEncoder,
|
2024-05-04 15:29:01 -04:00
|
|
|
skipkeys=skipkeys,
|
2024-05-04 15:25:05 -04:00
|
|
|
ensure_ascii=ensure_ascii,
|
|
|
|
check_circular=check_circular,
|
|
|
|
allow_nan=allow_nan,
|
|
|
|
indent=indent,
|
|
|
|
separators=separators,
|
|
|
|
default=default,
|
|
|
|
sort_keys=sort_keys,
|
|
|
|
**kw
|
2024-06-03 00:09:39 -04:00
|
|
|
)
|
2024-05-04 15:25:05 -04:00
|
|
|
|
|
|
|
# This is a wrapper around the json module's dump function that uses our custom JSONEncoder class
|
|
|
|
def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
|
|
|
|
allow_nan=True, indent=None, separators=None,
|
|
|
|
default=None, sort_keys=False, **kw) -> str:
|
|
|
|
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
|
|
|
|
``.write()``-supporting file-like object).
|
|
|
|
|
|
|
|
If ``skipkeys`` is true then ``dict`` keys that are not basic types
|
|
|
|
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
|
|
|
|
instead of raising a ``TypeError``.
|
|
|
|
|
|
|
|
If ``ensure_ascii`` is false, then the strings written to ``fp`` can
|
|
|
|
contain non-ASCII characters if they appear in strings contained in
|
|
|
|
``obj``. Otherwise, all such characters are escaped in JSON strings.
|
|
|
|
|
|
|
|
If ``check_circular`` is false, then the circular reference check
|
|
|
|
for container types will be skipped and a circular reference will
|
|
|
|
result in an ``RecursionError`` (or worse).
|
|
|
|
|
|
|
|
If ``allow_nan`` is false, then it will be a ``ValueError`` to
|
|
|
|
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
|
|
|
|
in strict compliance of the JSON specification, instead of using the
|
|
|
|
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
|
|
|
|
|
|
|
|
If ``indent`` is a non-negative integer, then JSON array elements and
|
|
|
|
object members will be pretty-printed with that indent level. An indent
|
|
|
|
level of 0 will only insert newlines. ``None`` is the most compact
|
|
|
|
representation.
|
|
|
|
|
|
|
|
If specified, ``separators`` should be an ``(item_separator, key_separator)``
|
|
|
|
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
|
|
|
|
``(',', ': ')`` otherwise. To get the most compact JSON representation,
|
|
|
|
you should specify ``(',', ':')`` to eliminate whitespace.
|
|
|
|
|
|
|
|
``default(obj)`` is a function that should return a serializable version
|
|
|
|
of obj or raise TypeError. The default simply raises TypeError.
|
|
|
|
|
|
|
|
If *sort_keys* is true (default: ``False``), then the output of
|
|
|
|
dictionaries will be sorted by key.
|
|
|
|
"""
|
|
|
|
return json.dump(
|
|
|
|
obj,
|
|
|
|
fp,
|
|
|
|
cls=JSONEncoder,
|
|
|
|
skipkeys=skipkeys,
|
|
|
|
ensure_ascii=ensure_ascii,
|
|
|
|
check_circular=check_circular,
|
|
|
|
allow_nan=allow_nan,
|
|
|
|
indent=indent,
|
|
|
|
separators=separators,
|
|
|
|
default=default,
|
|
|
|
sort_keys=sort_keys,
|
|
|
|
**kw
|
|
|
|
)
|