Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover conversion to/from builtins in usage docs #780

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,40 @@ types.
>>> msgspec.json.decode(b'[1, 2, "3"]', type=list[int], strict=False)
[1, 2, 3]

.. _builtin-conversion:

Converting to and from builtin types
------------------------------------

In some cases, ``msgspec`` will only be processing part of a message,
so full serialization to or from text or bytes isn't desirable. For
these situations, ``msgspec`` supports conversion to and from builtin
types which are otherwise handled (or created) by another library.

"Decoding" is handled by :func:`~msgspec.convert`, while "encoding" is handled by
:func:`~msgspec.to_builtins` (note that the input/output in this example is a
Python dictionary, *not* an encoded string):

.. code-block:: python

>>> import msgspec

>>> class User(msgspec.Struct):
... name: str
... groups: set[str] = set()
... email: str | None = None

>>> data = {"name": "bill", "groups": ["devops", "engineering"]}

>>> # Convert already decoded data into typed structs
... msgspec.convert(data, User)
User(name='bill', groups={'devops', 'engineering'}, email=None)

>>> user = User(name="alice", groups={"admin", "engineering"}, email=None)

>>> # Convert to builtin types for subsequent encoding
... msgspec.to_builtins(user)
{'name': 'alice', 'groups': ['engineering', 'admin']}

.. _JSON: https://json.org
.. _MessagePack: https://msgpack.org
Expand Down
9 changes: 8 additions & 1 deletion msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7798,6 +7798,10 @@ PyDoc_STRVAR(struct_asdict__doc__,
"\n"
"Convert a struct to a dict.\n"
"\n"
"This function converts all fields, regardless of the ``omit_defaults`` setting.\n"
"\n"
"Use ``to_builtins`` for recursive conversion with more encoding-like behavior.\n"
"\n"
"Parameters\n"
"----------\n"
"struct: Struct\n"
Expand Down Expand Up @@ -19995,7 +19999,10 @@ PyDoc_STRVAR(msgspec_to_builtins__doc__,
"Convert a complex object to one composed only of simpler builtin types\n"
"commonly supported by Python serialization libraries.\n"
"\n"
"This is mainly useful for adding msgspec support for other protocols.\n"
"This is useful for adding msgspec support for other protocols, as well as\n"
"to handle cases where msgspec is only processing part of a data structure.\n"
"\n"
"This function respects the ``omit_defaults`` setting on structs.\n"
"\n"
"Parameters\n"
"----------\n"
Expand Down