Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: Do you know what the death rate around here is? A: One per person.


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

SubjectAuthor
o Re: Serializing pydantic enumsLeft Right

1
Subject: Re: Serializing pydantic enums
From: Left Right
Newsgroups: comp.lang.python
Date: Tue, 28 May 2024 13:23 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: olegsivokon@gmail.com (Left Right)
Newsgroups: comp.lang.python
Subject: Re: Serializing pydantic enums
Date: Tue, 28 May 2024 15:23:49 +0200
Lines: 40
Message-ID: <mailman.51.1716921931.2909.python-list@python.org>
References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
<CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@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 pxvXQdiUhAzYtoR5of7sFAorbXA1VkXjwQJTfo96qFdA==
Cancel-Lock: sha1:WuLqeqzlHfjSk9LHhFkaPwUj8u0= sha256:gRIkUkewH9pe6v623r6AB5tFbwPLOPEZ89t93vaQQno=
Return-Path: <olegsivokon@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=YKyhvIeZ;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'argument':
0.04; 'json': 0.09; 'recurring': 0.09; 'situation,': 0.09;
'typeerror:': 0.09; 'url:mailman': 0.15; '***': 0.16; '2024':
0.16; 'passing.': 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; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'cannot': 0.25;
'url-ip:188.166/16': 0.25; 'tried': 0.26; 'object': 0.26; "isn't":
0.27; 'this?': 0.29; 'takes': 0.31; 'url-ip:188/8': 0.31;
'official': 0.32; 'objects': 0.32; 'python-list': 0.32;
'to:name:python': 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; 'method': 0.61;
'extend': 0.64; 'too.': 0.70
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1716902641; x=1717507441; darn=python.org;
h=content-transfer-encoding:to:subject:message-id:date:from
:in-reply-to:references:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=DGdVLBc3ZXX2Bxbctyy0ZDGSbI+k4qmU6F8c/bEKXqM=;
b=YKyhvIeZKXlRgKl8IYThMGR0NbtEBwRMwU3K/tmPT/Tss+lLk/5o5N87tLHna2kICg
klTtf/Wn/zMqYDJ1gzTSkZ27pzgFhmFKhV81gXcboLcCFiE4fCFwW9e6jfIZzEw+6adI
DoU7puzx2drInwMRB6bMgVuXJ07rdoD74BE8DRYmEbyoT7EDF9whlOJ+qS3dtzLLXHpo
onQOycAOhTMvRz8tv2NU6CQEMcjPvg/8MXbw4bS923lrjX5ipSB39yOCd0PVSszbgYAK
+WRGua+Lo1UAXPPGLNi/AwH8MA6AuTJqobn2/1hyz0E1SVCnklvBf9f9juEFHqQtYQIn
m/kw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1716902641; x=1717507441;
h=content-transfer-encoding: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=DGdVLBc3ZXX2Bxbctyy0ZDGSbI+k4qmU6F8c/bEKXqM=;
b=S/6zQz6jU2gpMBu4ujwWe5mF36BL4W/nCvRUA7zfyk9aK2zT4GjVKIWbQdTRqVNkM2
xDKjVaOdx4rFSsDoVsXNBluKDkSH83QDzyZMJjvZyofBc9z7IU6YJKF9B6YjLFtK5+fm
jvnDGjBGsLnD5VbCjZPtJC1TnCvfg78RJ/b9jq3WxX3nTQgMAa/Knf/SYJrXky2xPpWw
vdbGSuS9wTwTT8ghaaOFP/DcQVDNuQLlLXDaoTsqg79Id5rvTHMYIlQiWJowPqO2oV4+
6EbxkrAkcjeZRszsIDKc97US8o0/9Dhq8LE4OvikjGPgiYQ9apOyL8piSevVrqVVYCkh
b6qw==
X-Gm-Message-State: AOJu0Ywz+/2NZX/d2ybaAxeIfym+9RTFisvfx1g4hGGmaOp2UOjZnJfE
76DEKwk5wQTuVU6H46IjYhaSSJRObRJ2NbXIcGIJG460BPAIyQE5lkqFiU8n1xpsht+tLDA4z0p
b4PmKyRU5qZu24A4y1PPztLofDRZXC/YI
X-Google-Smtp-Source: AGHT+IE10+SZ5FgEdIvrNcQHYJ3eWgdF8wRaPoFRy6OkfYi7GKEMRJRIY40wo9BNhinpgqNOaEfOJXh28XCRAtqZngo=
X-Received: by 2002:a05:6122:202a:b0:4df:1d07:5ffa with SMTP id
71dfb90a1353d-4e4f0228038mr12672178e0c.3.1716902640645; Tue, 28 May 2024
06:24:00 -0700 (PDT)
In-Reply-To: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
X-Mailman-Approved-At: Tue, 28 May 2024 14:45:30 -0400
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: <CAJQBtg=f3t9-TVeyrJVd2Y27uymSuMw1mJ9OWVUXN7BK8MLcUg@mail.gmail.com>
X-Mailman-Original-References: <CACwCsY5xAJrFMvXQquLvrMrtgZk28-2UPnoQEwgH=8CtFxvRdg@mail.gmail.com>
View all headers

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.

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?
> --
> https://mail.python.org/mailman/listinfo/python-list

1

rocksolid light 0.9.8
clearnet tor