Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions fiftyone/server/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,23 @@
|
"""

from json import JSONEncoder
import traceback
import typing as t
import logging

from bson import json_util
import numpy as np
from starlette.endpoints import HTTPEndpoint
from starlette.exceptions import HTTPException
from starlette.responses import JSONResponse, Response
from starlette.requests import Request

from fiftyone.core.utils import run_sync_task


class Encoder(JSONEncoder):
"""Custom JSON encoder that handles numpy types."""

def default(self, o):
if isinstance(o, np.floating):
return float(o)

if isinstance(o, np.integer):
return int(o)

return JSONEncoder.default(self, o)
from fiftyone.server import utils


async def create_response(response: dict):
"""Creates a JSON response from the given dictionary."""
return Response(
await run_sync_task(lambda: json_util.dumps(response, cls=Encoder)),
await run_sync_task(lambda: utils.json.dumps(response)),
headers={"Content-Type": "application/json"},
)

Expand All @@ -52,7 +37,7 @@ async def wrapper(
try:
body = await request.body()
payload = body.decode("utf-8")
data = json_util.loads(payload) if payload else {}
data = utils.json.loads(payload)
response = await func(endpoint, request, data, *args)
if isinstance(response, Response):
return response
Expand Down
Loading
Loading