Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You are so boring that when I see you my feet go to sleep.


comp / comp.lang.python / Re: Serializing pydantic enums

SubjectAuthor
o Re: Serializing pydantic enumsLarry Martell

1
Subject: Re: Serializing pydantic enums
From: Larry Martell
Newsgroups: comp.lang.python
Date: Wed, 29 May 2024 19:27 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: larry.martell@gmail.com (Larry Martell)
Newsgroups: comp.lang.python
Subject: Re: Serializing pydantic enums
Date: Wed, 29 May 2024 12:27:04 -0700
Lines: 44
Message-ID: <mailman.69.1717010863.2909.python-list@python.org>
References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
<CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
<CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de RgDikQw2OJV844GSGR426Qt0lJHFJClbdc7/yp23LgvA==
Cancel-Lock: sha1:hLNvLzKa5vdkuR9d4JXRcS5e/90= sha256:q25gxvg4kp6kOaZaoBvwlTdIqHz01ZZ2ZshcLd0iq+4=
Return-Path: <larry.martell@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=WqgIWdLW;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'argument':
0.04; 'cc:addr:python-list': 0.09; 'json': 0.09; 'recurring':
0.09; 'situation,': 0.09; 'typeerror:': 0.09; '***': 0.16; '2024':
0.16; 'cc:name:python': 0.16; 'passing.': 0.16; 'recall': 0.16;
'received:mail-yb1-xb29.google.com': 0.16; 'wrote:': 0.16;
'python': 0.16; "aren't": 0.19; 'implement': 0.19; 'tue,': 0.19;
'cc:addr:python.org': 0.20; 'maybe': 0.22; 'cannot': 0.25;
'cc:2**0': 0.25; 'tried': 0.26; 'object': 0.26; "isn't": 0.27;
'this?': 0.29; 'seem': 0.31; 'takes': 0.31; 'official': 0.32;
'guess': 0.32; 'objects': 0.32; 'python-list': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'missing': 0.37; 'special': 0.37;
'class': 0.37; 'this,': 0.39; 'method': 0.61; 'extend': 0.64;
'right': 0.68; 'too.': 0.70; 'left': 0.83; 'handled': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1717010861; x=1717615661; darn=python.org;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:in-reply-to:references:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=E/JkaHB2mwtJkoqzPr9jQORkhz77HxNHajnkKxLowSU=;
b=WqgIWdLWReEn3p7E2R+rIgR6oamdBVwCcisM9IEoDr/3/zfB3nX5l3cz7mZ72JdUe0
7BpVPlX0+JmEhqxKsmZ0/9vPG+ePFcroIxgeM1bc7AvtT53q4+YKZRWugX024YPNcrNM
NSJ0Bh0iPs+EAAU+RjS8kTmP4cUQcFwlrNlDSZVkLvsdU8XT6e6pnfDgNBtDzPP6/7Zt
Q7DzmnAdIaAs+/6mFGS/snPzHmdreTEVIBkPZD1qY4DzJPqpDj3bZYzYIdXQwrfCxx/d
xuUNwYQL38EG0l+OiHNT38fL+FOuPihS4gLNR104poU9yIOvWAytFLp9ebk4Ktoc4l8h
62yg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1717010861; x=1717615661;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=E/JkaHB2mwtJkoqzPr9jQORkhz77HxNHajnkKxLowSU=;
b=Cz0N/JA1JwTV68+6y+Ssezp+6ApmSDMBTBDpKlIezIQFFXM9Vx3GO0i2LabzBRJtfu
yJCFfCIWKuR7FBUEZyN/j7iz8UpNULrO+HTvXv1w/syaIOY6DGCGZC224eRXZIQnu9UI
fXo5YMqqzvlcnrWOLh0z+B6g7NYefPrGaAz9ZQMP10qv/DtdLZKwPoXIdVjwctCxNdEv
5HM5TdyZmOM70Q7TkGd9IML7BdGwF7zZJg8uy4kGcFqYtI4oT2jhnImZLWtw23MSILPb
VNeBMBf8ZKmA4dTOX2NYtHabboaz3cx1frudQCQHQj62dzi+xf5JQdrf8pnbKA9Z7dvI
eEgg==
X-Gm-Message-State: AOJu0YyJENyAm5pWScBQoUlEyiVkTfKGG5ijJVeULTIQRT68gDMD8HF+
b7rxmvRhj6iAXLFMi1fVoOAAZML/Y6Y2dwgqXtXveMCUyn4FGloXG7KAPoaRAYtsSF8aQX49ggE
oTgg+6ntZ4U/rTLaVHQNqAaKS8g==
X-Google-Smtp-Source: AGHT+IGa/N1RU8JLuGurRgUSCyxKbmu1LnFbGPqIR4lapbahuONV5/UrZs/i1p6Z+Xt9maTi1ezFWKMJnlYWR6rQc1U=
X-Received: by 2002:a25:ea02:0:b0:df7:b7da:d213 with SMTP id
3f1490d57ef6-dfa5a5e1151mr220156276.16.1717010861091; Wed, 29 May 2024
12:27:41 -0700 (PDT)
In-Reply-To: <CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.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: <CACwCsY7Ze-2n2Gusu=6QKftCfbzQGaaSXJz5vwLErEi6q6xmAA@mail.gmail.com>
X-Mailman-Original-References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
<CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
View all headers

On Tue, May 28, 2024 at 11:46 AM 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.

Yeah, I know I can do this, but I seem to recall reading that pydantic
handled serialization. Guess not.

> On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
> <python-list@python.org> wrote:
> >
> > Just getting started with pydantic. I have this example code:
> >
> > class FinishReason(Enum):
> > stop = 'stop'
> >
> > class Choice(BaseModel):
> > finish_reason: FinishReason = Field(...)
> >
> >
> > But I cannot serialize this:
> >
> > json.dumps(Choice(finish_reason=FinishReason.stop).dict())
> > *** TypeError: Object of type FinishReason is not JSON serializable
> >
> >
> > I get the object not the value:
> >
> > (Pdb) Choice(finish_reason=FinishReason.stop)
> > Choice(finish_reason=<FinishReason.stop: 'stop'>)
> >
> >
> > Also tried it with .value, same result.
> >
> > What am I missing here?

1

rocksolid light 0.9.8
clearnet tor