File: serializer_hooks.rst

package info (click to toggle)
dataclass-wizard 0.35.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: python: 15,276; makefile: 111; javascript: 23
file content (72 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Serializer Hooks
================

.. note::
    To customize the load or dump process for annotated types
    instead of individual fields, please see the `Type
    Hooks <type_hooks.html>`__ section.

You can optionally add hooks that are run before a JSON string or a
Python ``dict`` object is loaded to a dataclass instance, or before the
dataclass instance is converted back to a Python ``dict`` object.

To customize the load process:

* To pre-process data before ``from_dict`` is called, simply
  implement a ``_pre_from_dict`` method which will be called
  whenever you invoke the ``from_dict`` or ``from_json`` methods.
  Please note that this will pass in the original ``dict`` object,
  so updating any values will affect data in the underlying ``dict``
  (**this might change in a future revision**).
* To post-process data, *after* a dataclass instance is de-serialized,
  simply implement the ``__post_init__`` method which will be run
  by the ``dataclass`` decorator.

To customize the dump process, simply implement
a ``_pre_dict`` method which will be called
whenever you invoke the ``to_dict`` or ``to_json``
methods. Please note that this will pass in the
original dataclass instance, so updating any values
will affect the fields of the underlying dataclass
(**this might change in a future revision**).

A simple example to illustrate both approaches is shown below:

.. code:: python3

    from dataclasses import dataclass
    from dataclass_wizard import JSONWizard
    from dataclass_wizard.type_def import JSONObject


    @dataclass
    class MyClass(JSONWizard):
        my_str: str
        my_int: int
        my_bool: bool = False

        def __post_init__(self):
            self.my_str = self.my_str.title()
            self.my_int *= 2

        @classmethod
        def _pre_from_dict(cls, o: JSONObject) -> JSONObject:
            # o = o.copy()  # Copying the `dict` object is optional
            o['my_bool'] = True  # Adds a new key/value pair
            return o

        def _pre_dict(self):
            self.my_str = self.my_str.swapcase()


    data = {"my_str": "my string", "myInt": "10"}

    c = MyClass.from_dict(data)
    print(repr(c))
    # prints:
    #   MyClass(my_str='My String', my_int=20, my_bool=True)

    string = c.to_json()
    print(string)
    # prints:
    #   {"myStr": "mY sTRING", "myInt": 20, "myBool": true}