From 59a8350aace8f44122f7a21cf65e006cc66e0114 Mon Sep 17 00:00:00 2001 From: Remco Meeuwissen Date: Thu, 14 Dec 2023 14:03:07 +0100 Subject: [PATCH] Updated a few places I missed last time with the decimal.Decimal orjson fix --- tipg/factory.py | 9 ++++----- tipg/resources/response.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tipg/factory.py b/tipg/factory.py index ed3e713b..3a8c5edb 100644 --- a/tipg/factory.py +++ b/tipg/factory.py @@ -35,7 +35,7 @@ ) from tipg.errors import MissingGeometryColumn, NoPrimaryKey, NotFound from tipg.resources.enums import MediaType -from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse +from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse, orjsonDumps from tipg.settings import FeaturesSettings, MVTSettings, TMSSettings from fastapi import APIRouter, Depends, Path, Query @@ -781,7 +781,7 @@ async def items( # noqa: C901 # NDJSON Response if output_type == MediaType.ndjson: return StreamingResponse( - (orjson.dumps(row) + b"\n" for row in rows), + (orjsonDumps(row) + b"\n" for row in rows), media_type=MediaType.ndjson, headers={ "Content-Disposition": "attachment;filename=items.ndjson" @@ -892,7 +892,7 @@ async def items( # noqa: C901 # GeoJSONSeq Response elif output_type == MediaType.geojsonseq: return StreamingResponse( - (orjson.dumps(f) + b"\n" for f in data["features"]), # type: ignore + (orjsonDumps(f) + b"\n" for f in data["features"]), # type: ignore media_type=MediaType.geojsonseq, headers={ "Content-Disposition": "attachment;filename=items.geojson" @@ -1016,7 +1016,7 @@ async def item( # NDJSON Response if output_type == MediaType.ndjson: return StreamingResponse( - (orjson.dumps(row) + b"\n" for row in rows), + (orjsonDumps(row) + b"\n" for row in rows), media_type=MediaType.ndjson, headers={ "Content-Disposition": "attachment;filename=items.ndjson" @@ -1512,7 +1512,6 @@ async def collection_get_tile( return Response(bytes(tile), media_type=MediaType.mvt.value) def _tilejson_routes(self): - ############################################################################ # ADDITIONAL ENDPOINTS NOT IN OGC Tiles API (tilejson, style.json, viewer) # ############################################################################ diff --git a/tipg/resources/response.py b/tipg/resources/response.py index 51a7f0fd..6e89a403 100644 --- a/tipg/resources/response.py +++ b/tipg/resources/response.py @@ -14,16 +14,20 @@ def default(obj): return str(obj) +def orjsonDumps(content: Any): + return orjson.dumps( + content, + default=default, + option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY, + ) + + class ORJSONResponse(JSONResponse): """Custom response handler for using orjson""" def render(self, content: Any) -> bytes: """Render the content into a JSON response using orjson""" - return orjson.dumps( - content, - default=default, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY, - ) + return orjsonDumps(content) class GeoJSONResponse(ORJSONResponse):