Skip to content

Commit 15996fb

Browse files
Merge pull request #148 from RemcoMeeuwissen/fix-decimal-error-again
Fix decimal error again
2 parents c856a09 + 24b7109 commit 15996fb

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

tipg/factory.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
)
3636
from tipg.errors import MissingGeometryColumn, NoPrimaryKey, NotFound
3737
from tipg.resources.enums import MediaType
38-
from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse
38+
from tipg.resources.response import GeoJSONResponse, SchemaJSONResponse, orjsonDumps
3939
from tipg.settings import FeaturesSettings, MVTSettings, TMSSettings
4040

4141
from fastapi import APIRouter, Depends, Path, Query
@@ -781,7 +781,7 @@ async def items( # noqa: C901
781781
# NDJSON Response
782782
if output_type == MediaType.ndjson:
783783
return StreamingResponse(
784-
(orjson.dumps(row) + b"\n" for row in rows),
784+
(orjsonDumps(row) + b"\n" for row in rows),
785785
media_type=MediaType.ndjson,
786786
headers={
787787
"Content-Disposition": "attachment;filename=items.ndjson"
@@ -886,13 +886,13 @@ async def items( # noqa: C901
886886
# HTML Response
887887
if output_type == MediaType.html:
888888
return self._create_html_response(
889-
request, orjson.dumps(data).decode(), template_name="items"
889+
request, orjsonDumps(data).decode(), template_name="items"
890890
)
891891

892892
# GeoJSONSeq Response
893893
elif output_type == MediaType.geojsonseq:
894894
return StreamingResponse(
895-
(orjson.dumps(f) + b"\n" for f in data["features"]), # type: ignore
895+
(orjsonDumps(f) + b"\n" for f in data["features"]), # type: ignore
896896
media_type=MediaType.geojsonseq,
897897
headers={
898898
"Content-Disposition": "attachment;filename=items.geojson"
@@ -1016,7 +1016,7 @@ async def item(
10161016
# NDJSON Response
10171017
if output_type == MediaType.ndjson:
10181018
return StreamingResponse(
1019-
(orjson.dumps(row) + b"\n" for row in rows),
1019+
(orjsonDumps(row) + b"\n" for row in rows),
10201020
media_type=MediaType.ndjson,
10211021
headers={
10221022
"Content-Disposition": "attachment;filename=items.ndjson"
@@ -1050,7 +1050,7 @@ async def item(
10501050
if output_type == MediaType.html:
10511051
return self._create_html_response(
10521052
request,
1053-
orjson.dumps(data).decode(),
1053+
orjsonDumps(data).decode(),
10541054
template_name="item",
10551055
)
10561056

@@ -1512,7 +1512,6 @@ async def collection_get_tile(
15121512
return Response(bytes(tile), media_type=MediaType.mvt.value)
15131513

15141514
def _tilejson_routes(self):
1515-
15161515
############################################################################
15171516
# ADDITIONAL ENDPOINTS NOT IN OGC Tiles API (tilejson, style.json, viewer) #
15181517
############################################################################

tipg/resources/response.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ def default(obj):
1414
return str(obj)
1515

1616

17+
def orjsonDumps(content: Any):
18+
"""Small wrapper function to run the orjson.dumps with the additional options we want"""
19+
return orjson.dumps(
20+
content,
21+
default=default,
22+
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY,
23+
)
24+
25+
1726
class ORJSONResponse(JSONResponse):
1827
"""Custom response handler for using orjson"""
1928

2029
def render(self, content: Any) -> bytes:
2130
"""Render the content into a JSON response using orjson"""
22-
return orjson.dumps(
23-
content,
24-
default=default,
25-
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY,
26-
)
31+
return orjsonDumps(content)
2732

2833

2934
class GeoJSONResponse(ORJSONResponse):

0 commit comments

Comments
 (0)