Использование Pydantic сегодня стало нормой, и это правильно. Но иногда на ревью вижу, что используют его не всегда корректно.
Например, метод BaseModel.model_dump() по умолчанию не преобразует стандартные типы, такие как datetime, UUID или Decimal, в простой сериализуемый для JSON вид. Тогда пишут кастмоный сериализатор для этих типов чтобы функция json.dump() не падала с ошибкой.
import uuid
from datetime import datetime
from decimal import Decimal
from uuid import UUID
from pydantic import BaseModel
class MyModel(BaseModel):
id: UUID
date: datetime
value: Decimal
obj = MyModel(
id=uuid.uuid4(),
date=datetime.now(),
value='1.23'
)
print(obj.model_dump())
# не подходит для json.dump
# {
# 'id': UUID('4f8c1bc4-25fd-40cd-9dbe-2c73639b0dc1'),
# 'date': datetime.datetime(2025, 12, 12, 12, 12, 12, 111111),
# 'value': Decimal('1.23')
# }
# добавляем свой кастомный сериализатор
json.dumps(obj.model_dump(), cls=MySerializer)
# {
# 'id': '4f8c1bc4-25fd-40cd-9dbe-2c73639b0dc1',
# 'date': '2025-12-12T12:12:12.111111',
# 'value': '1.23'
# }
В данном случае класс MySerializer обрабатывает datetime, UUID и Decimal. Например так:
class MySerializer(json.JSONEncoder):
def default(self, o):
if isinstance(o, Decimal):
return str(o)
elif isinstance(o, datetime):
return o.isoformat()
elif isinstance(o, UUID):
return str(o)
return super().default(o)
Специально для тех, кто всё еще так делает - в этом нет необходимости!
Pydantic может это сделать сам, просто нужно добавить параметр mode="json".
json.dumps(obj.model_dump(mode="json"))
# {
# 'id': '4f8c1bc4-25fd-40cd-9dbe-2c73639b0dc1',
# 'date': '2012-12-12T12:12:12.111111',
# 'value': '1.23'
# }
#pydantic#libs
FTX/Alameda unstake 197,637 $SOL, valued at 17.07M.
https://solscan.io/account/H4yiPhdSsmSMJTznXzmZvdqWuhxDRzzkoQMEWXZ6agFZ#transfers
https://x.com/OnchainLens/status/2031919069579132969
Follow @onchainlens for more onchain updates
FTX/Alameda unstaked 196,611 $SOL, valued at $15.97M.
https://solscan.io/account/H4yiPhdSsmSMJTznXzmZvdqWuhxDRzzkoQMEWXZ6agFZ#transfers
https://x.com/OnchainLens/status/2021521751658881497
Follow @OnchainLens for more Onchain Updates
FTX/Alameda unstaked 195,669 $SOL, valued at $27.98M.
https://solscan.io/account/HoBeML5zvsmNGn5fXXKADEdSKK9G7qSBFxVFwoRQsyMx#transfers
https://x.com/OnchainLens/status/2010864084745584724
Follow @OnchainLens for more Onchain Updates
A whale deposited 51,750 $SOL worth $4.75M into #Binance after being dormant for 7 months, realizing a loss of $4.37M
The whale initially withdrew 50,000 $SOL for $9.12M and sent it for staking.
https://solscan.io/account/HnsXQEWHrHQQa8E3ok1D1EkmK9Muwmt8C14urBhc9iyq#transfers
https://x.com/OnchainLens/status/2036701947693240644
Follow @onchainlens for more onchain updates
A whale withdrew 48,744 $SOL, worth $6.15M, from #OKX and sent it for staking.
In total, the whale has withdrawn and staked 1,231,861 $SOL ($186.09M) since August 22, 2025. Currently, these $SOL are valued at $155.68M, resulting in a loss of $30.4M.
https://solscan.io/account/GcJFx1MZJ8Zn7PwtRprnzrXz4NyApdm16yAkCFQ4JvG#transfers
https://x.com/OnchainLens/status/2002256392284221914
Follow @onchainlens for more onchain updates
A whale withdrew 100,000 $SOL worth $9.93M from #Bybit and sent it for staking after 2 months.
https://solscan.io/account/9bcNtnFMBgRuiXq2jF5XG8CVGzqfBCpm1sMrBPTEuboq#transfers
https://x.com/OnchainLens/status/2018845766056550470
Follow @OnchainLens for more Onchain Updates.
In the past 24 hours, a whale has unstaked 170K $SOL ($14.85M) and deposited it into #Kraken.
The whale still has 457,937 $SOL ($38.9M) staked.
https://solscan.io/account/7VMTVroogF6GhVunnUWF9hX8JiXqPHiZoG3VKAe64Ckt#transfers
https://x.com/OnchainLens/status/2037469340887593046
Follow @onchainlens for more onchain updates
A wallet linked to the Pump.fun team or investor deposited 2.34B $PUMP worth $4M into #Bitget.
https://solscan.io/account/5v7ZZg1D1si417WhUQF9Br2dRQEnd1sTbCfesscUCVKE#transfers
https://x.com/OnchainLens/status/2041552240666915231
Follow @onchainlens for more onchain updates
A wallet linked to the Pump.fun team deposited 1.757B $PUMP worth $3.54M into #Bitget.
The wallet still holds 12.3B $PUMP ($24.77M).
https://solscan.io/account/5v7ZZg1D1si417WhUQF9Br2dRQEnd1sTbCfesscUCVKE#transfers
https://x.com/OnchainLens/status/2029719672535544181
Follow @onchainlens for more onchain updates
After 5 months of dormancy, a whale withdrew 50,000 $SOL ($4.25M) from #Binance and #Bybit, then sent it for staking.
https://solscan.io/account/8iZyywnZu1yBPPNkQLgpPZyBtuzGG9rjNLGggKaGozXD#transfers
https://x.com/OnchainLens/status/2025436744360096170
Follow @onchainlens for more onchain updates
Trump Meme Team Allocation Wallet sent 5.267M $TRUMP, worth $22.44M, into a #BitGo Custody wallet. Are we selling via OTC? https://solscan.io/account/AduisyNJFAxo49tDQmJUN7Ujc2xAmgmfjJqTaPptPUU4#transfers https://x.com/OnchainLens/status/2018495068852871321…
Trump Meme Team Allocation Wallet sent 5.267M $TRUMP, worth $22.44M, into a #BitGo Custody wallet.
Are we selling via OTC?
https://solscan.io/account/AduisyNJFAxo49tDQmJUN7Ujc2xAmgmfjJqTaPptPUU4#transfers
https://x.com/OnchainLens/status/2018495068852871321
Follow @onchainlens for more onchain updates