Skip to content

Commit

Permalink
Merge pull request #148 from RemcoMeeuwissen/fix-decimal-error-again
Browse files Browse the repository at this point in the history
Fix decimal error again
  • Loading branch information
vincentsarago authored Dec 19, 2023
2 parents c856a09 + 24b7109 commit 15996fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
13 changes: 6 additions & 7 deletions tipg/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -886,13 +886,13 @@ async def items( # noqa: C901
# HTML Response
if output_type == MediaType.html:
return self._create_html_response(
request, orjson.dumps(data).decode(), template_name="items"
request, orjsonDumps(data).decode(), template_name="items"
)

# 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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -1050,7 +1050,7 @@ async def item(
if output_type == MediaType.html:
return self._create_html_response(
request,
orjson.dumps(data).decode(),
orjsonDumps(data).decode(),
template_name="item",
)

Expand Down Expand Up @@ -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) #
############################################################################
Expand Down
15 changes: 10 additions & 5 deletions tipg/resources/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ def default(obj):
return str(obj)


def orjsonDumps(content: Any):
"""Small wrapper function to run the orjson.dumps with the additional options we want"""
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):
Expand Down

0 comments on commit 15996fb

Please sign in to comment.