Skip to content

Instantly share code, notes, and snippets.

View gmassman's full-sized avatar

Garrett Massman gmassman

View GitHub Profile
@gmassman
gmassman / semityped_json_serializer.py
Last active August 11, 2022 17:18
Python function to recursively encode any object into a JSON compatible representation.
from dataclasses import is_dataclass
from datetime import datetime
from decimal import Decimal
from typing import Optional, Union, get_args, get_origin, get_type_hints
from flask import current_app, json
JSONType = None | type | dict | list
@gmassman
gmassman / safe_json_api.py
Last active December 1, 2020 21:15
This subclass of flask.Blueprint accepts and returns JSON and optionally applies a custom authorization function to every request.
from functools import wraps
from flask import Blueprint, request, redirect, url_for
from flask_jsontools import jsonapi
def ensure_json_request(f):
@wraps(f)
def enforce_json_body(*args, **kwargs):
if not request.is_json: