Код: Выделить всё
import json
from annotated_types import Len
from typing_extensions import Annotated
from pydantic import BaseModel, Field, ConfigDict
TwoDim = Annotated[
Sequence[float],
Len(min_length=2, max_length=2),
]
class Tester(BaseModel):
test_val: Sequence[TwoDim] = Field()
Код: Выделить всё
# in the model definition
model_config = ConfigDict(
json_encoders={
TwoDim: NoIndentEncoder().encode
}
)
class NoIndentEncoder(json.JSONEncoder):
def encode(self, obj):
if isinstance(obj, list):
return f”[{‘, ‘.join([json.dumps(elem) for elem in obj])}]”
return super().encode(obj)
Код: Выделить всё
{
“test_val”: [
“[0.0, 1.2]”,
“[3.0, 1.4]”,
…
]
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... -json-dump