Skip to content
Closed

AI junk #6112

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
7 changes: 6 additions & 1 deletion src/flask/json/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import dataclasses
import decimal
import enum
import json
import pathlib
import typing as t
import uuid
import weakref
Expand Down Expand Up @@ -109,9 +111,12 @@ def _default(o: t.Any) -> t.Any:
if isinstance(o, date):
return http_date(o)

if isinstance(o, (decimal.Decimal, uuid.UUID)):
if isinstance(o, (decimal.Decimal, uuid.UUID, pathlib.PurePath)):
return str(o)

if isinstance(o, enum.Enum):
return o.value

if dataclasses and dataclasses.is_dataclass(o):
return dataclasses.asdict(o) # type: ignore[arg-type]

Expand Down
13 changes: 13 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,16 @@ def __html__(self):

result = json.dumps(ObjectWithHTML())
assert result == '"<p>test</p>"'


def test_pathlib_and_enum_serialization():
import enum
import pathlib

class Status(enum.Enum):
PENDING = "pending"
ACTIVE = "active"

path = pathlib.Path("test.txt")
assert json.dumps(path) == '"test.txt"'
assert json.dumps(Status.ACTIVE) == '"active"'
Loading