You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
948 B
Python
32 lines
948 B
Python
import datetime as dt
|
|
from os import path
|
|
from decimal import Decimal
|
|
from typing import Any
|
|
from molten import JSONRenderer, is_schema, dump_schema
|
|
|
|
BASE_PATH = path.normpath(path.join(path.abspath(path.dirname(__file__)), "."))
|
|
|
|
|
|
def path_to(*xs):
|
|
"""
|
|
Construct a path from the root project directory
|
|
"""
|
|
return path.join(BASE_PATH, *xs)
|
|
|
|
|
|
class ExtJSONRenderer(JSONRenderer):
|
|
"""JSON Render with support for ISO 8601 datetime format strings and Decimal"""
|
|
|
|
def default(self, ob: Any) -> Any:
|
|
"""You may override this when subclassing the JSON renderer in
|
|
order to encode non-standard object types.
|
|
"""
|
|
if is_schema(type(ob)):
|
|
return dump_schema(ob)
|
|
if isinstance(ob, dt.datetime):
|
|
return ob.isoformat()
|
|
if isinstance(ob, Decimal):
|
|
return float(ob)
|
|
|
|
raise TypeError(f"cannot encode values of type {type(ob)}") # pragma: no cover
|