Deutsch   English   Français   Italiano  
<mailman.72.1717023734.2909.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Mats Wichmann <mats@wichmann.us>
Newsgroups: comp.lang.python
Subject: Re: Serializing pydantic enums
Date: Wed, 29 May 2024 17:02:08 -0600
Lines: 28
Message-ID: <mailman.72.1717023734.2909.python-list@python.org>
References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
 <CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
 <CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
 <c7b051e7-09b4-4e99-a039-97c66c52ffb1@wichmann.us>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de A0va5USxgfelChJeSe7w1wfSKRus8C+DXZJ9ksGv4MqQ==
Cancel-Lock: sha1:Auv1fmhNUqAERMxbNAwscLCIXtU= sha256:uvdlzQMXMiLujInbf0afUplaZpwD954K9NcdjPYZCEY=
Return-Path: <mats@wichmann.us>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="1024-bit key; unprotected key"
 header.d=pobox.com header.i=@pobox.com header.b=pcZNQGkn;
 dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.054
X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'argument': 0.04;
 'obviously': 0.09; 'recurring': 0.09; 'situation,': 0.09; '2024':
 0.16; 'aliases': 0.16; 'elsewhere,': 0.16; 'help:': 0.16;
 'passing.': 0.16; 'recall': 0.16; 'received:64.147': 0.16;
 'url:latest': 0.16; 'wrote:': 0.16; 'python': 0.16; "aren't":
 0.19; 'implement': 0.19; 'tue,': 0.19; 'to:addr:python-list':
 0.20; 'maybe': 0.22; 'help.': 0.25; "isn't": 0.27; 'this?': 0.29;
 'header:User-Agent:1': 0.30; 'seem': 0.31; 'takes': 0.31;
 'official': 0.32; 'guess': 0.32; 'objects': 0.32; 'python-list':
 0.32; 'but': 0.32; 'particular': 0.33; 'header:In-Reply-To:1':
 0.34; 'couple': 0.37; 'special': 0.37; 'class': 0.37;
 'received:192.168': 0.37; 'this,': 0.39; 'provide': 0.60;
 'method': 0.61; "there's": 0.61; 'url-ip:104.26/16': 0.64;
 'extend': 0.64; 'received:64': 0.67; 'right': 0.68; 'too.': 0.70;
 'easy': 0.74; 'field': 0.78; 'out.': 0.80; 'left': 0.83;
 'handled': 0.84; 'noted': 0.84; 'url-ip:104.26.8/24': 0.93; 'url-
 ip:104.26.9/24': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=message-id
 :date:mime-version:subject:to:references:from:in-reply-to
 :content-type:content-transfer-encoding; s=sasl; bh=N1So2BqLS3JN
 Z30bC95zMCCwZmVzuHzK+c8EIc/Eg34=; b=pcZNQGkn+/72BjYFIrini7vsNw9S
 /kMWrZKnsZqk6w53GLvtPzvvBnuV5E/dHuLlzJtNKu6d0NtgmUdrXIZQIrNz9w6L
 chFc78jiIsnMG9GfkAtgMtwXkNPMqPfHennWGgN7A5UCXed7iBLfpJa5547SiZUD
 b9PC7smpNClGwso=
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=wichmann.us;
 h=message-id:date:mime-version:subject:to:references:from:in-reply-to:content-type:content-transfer-encoding;
 s=2018-07.pbsmtp; bh=RjNfCJeaVtM5Z0YM3n2eTZiJ4LLYHY5cYp7mXOW6n4w=;
 b=jREzK5sqthNMkgd20nsatXDpbNLRSXIaMKOwNtAnUPWNn0O2PPV+l+rusobdrNIVt1CjQrckQVELlyoSlSm25D0GFXwK+0/1uQYlJmiyM0HxcDrl3z/W2PcOLk/NyEdbrCBAVNCa4fDF5iMv0k6P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V6j9xkPkLWOaIDw7dlwEOlF1N1YtZ2wa0p1wsttdIbIx
 ffgwXmcH4zrdxUIMz3U0BqYzk5H+5cYFXECXTFVOmweS+JECYMj80PjRoKCO1eVO1N30zksB
 36NnhZWPRWIhjK3ZarIYH8JGBBgRAgAGBQI/mKzwAAoJEMAxoJBz2FTG6VEAoKDYHfDp5Q3q
 PuPvPahCE9HsXMgAAJ9INTqcLSJrOfyJ8q95nBO1T26H2Q==
In-Reply-To: <CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
X-Pobox-Relay-ID: 7AC4E68E-1E0F-11EF-862A-B84BEB2EC81B-81526775!pb-smtp1.pobox.com
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <c7b051e7-09b4-4e99-a039-97c66c52ffb1@wichmann.us>
X-Mailman-Original-References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
 <CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
 <CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
Bytes: 6901

On 5/29/24 13:27, Larry Martell via Python-list wrote:
> On Tue, May 28, 2024 at 11:46=E2=80=AFAM Left Right via Python-list
> <python-list@python.org> wrote:
>>
>> Most Python objects aren't serializable into JSON. Pydantic isn't
>> special in this sense.
>>
>> What can you do about this? -- Well, if this is a one-of situation,
>> then, maybe just do it by hand?
>>
>> If this is a recurring problem: json.dumps() takes a cls argument that
>> will be used to do the serialization. Extend json.JSONEncoder and
>> implement the encode() method for the encoder class you are passing. I
>> believe that the official docs have some information about this too.
>=20
> Yeah, I know I can do this, but I seem to recall reading that pydantic
> handled serialization.  Guess not.

Pydantic devotes some of its documentation to serialization.

https://docs.pydantic.dev/latest/concepts/serialization/

As noted elsewhere, some Python objects are easy to serialize, some you=20
need to provide some help. Consider pickling: if you write a class that=20
isn't obviously pickleable, the getstate dunder method can be defined to=20
help out.  For Pydantic, there's a couple of ways... aliases in=20
particular seem designed to help: there's a serialization_alias argument=20
to the Field function.