File: README.md

package info (click to toggle)
python-mashumaro 3.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,408 kB
  • sloc: python: 19,981; sh: 16; makefile: 5
file content (3542 lines) | stat: -rw-r--r-- 116,942 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
<div align="center">

<img alt="logo" width="175" src="https://raw.githubusercontent.com/Fatal1ty/mashumaro/ac2f924591d488dbd9a776a6b1ae7dede2d8c73e/img/logo.svg">

###### Fast and well tested serialization library

[![Build Status](https://github.com/Fatal1ty/mashumaro/workflows/tests/badge.svg)](https://github.com/Fatal1ty/mashumaro/actions)
[![Coverage Status](https://coveralls.io/repos/github/Fatal1ty/mashumaro/badge.svg?branch=master)](https://coveralls.io/github/Fatal1ty/mashumaro?branch=master)
[![Latest Version](https://img.shields.io/pypi/v/mashumaro.svg)](https://pypi.python.org/pypi/mashumaro)
[![Python Version](https://img.shields.io/pypi/pyversions/mashumaro.svg)](https://pypi.python.org/pypi/mashumaro)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
</div>

In Python, you often need to dump and load objects based on the schema you
have. It can be a dataclass model, a list of third-party generic classes or
whatever. Mashumaro not only lets you save and load things in different ways,
but it also does it _super quick_.

**Key features**
* 🚀 One of the fastest libraries
* ☝️ Mature and time-tested
* 👶 Easy to use out of the box
* ⚙️ Highly customizable
* 🎉 Built-in support for JSON, YAML, TOML, MessagePack
* 📦 Built-in support for almost all Python types including typing-extensions
* 📝 JSON Schema generation

Table of contents
-------------------------------------------------------------------------------
* [Table of contents](#table-of-contents)
* [Introduction](#introduction)
* [Installation](#installation)
* [Changelog](#changelog)
* [Supported data types](#supported-data-types)
* [Usage example](#usage-example)
* [How does it work?](#how-does-it-work)
* [Benchmark](#benchmark)
* [Supported serialization formats](#supported-serialization-formats)
    * [Basic form](#basic-form)
    * [JSON](#json)
        * [json library](#json-library)
        * [orjson library](#orjson-library)
    * [YAML](#yaml)
    * [TOML](#toml)
    * [MessagePack](#messagepack)
* [Customization](#customization)
    * [SerializableType interface](#serializabletype-interface)
        * [User-defined types](#user-defined-types)
        * [User-defined generic types](#user-defined-generic-types)
    * [SerializationStrategy](#serializationstrategy)
        * [Third-party types](#third-party-types)
        * [Third-party generic types](#third-party-generic-types)
    * [Field options](#field-options)
        * [`serialize` option](#serialize-option)
        * [`deserialize` option](#deserialize-option)
        * [`serialization_strategy` option](#serialization_strategy-option)
        * [`alias` option](#alias-option)
    * [Config options](#config-options)
        * [`debug` config option](#debug-config-option)
        * [`code_generation_options` config option](#code_generation_options-config-option)
        * [`serialization_strategy` config option](#serialization_strategy-config-option)
        * [`aliases` config option](#aliases-config-option)
        * [`serialize_by_alias` config option](#serialize_by_alias-config-option)
        * [`allow_deserialization_not_by_alias` config option](#allow_deserialization_not_by_alias-config-option)
        * [`omit_none` config option](#omit_none-config-option)
        * [`omit_default` config option](#omit_default-config-option)
        * [`namedtuple_as_dict` config option](#namedtuple_as_dict-config-option)
        * [`allow_postponed_evaluation` config option](#allow_postponed_evaluation-config-option)
        * [`dialect` config option](#dialect-config-option)
        * [`orjson_options` config option](#orjson_options-config-option)
        * [`discriminator` config option](#discriminator-config-option)
        * [`lazy_compilation` config option](#lazy_compilation-config-option)
        * [`sort_keys` config option](#sort_keys-config-option)
        * [`forbid_extra_keys` config option](#forbid_extra_keys-config-option)
    * [Passing field values as is](#passing-field-values-as-is)
    * [Extending existing types](#extending-existing-types)
    * [Field aliases](#field-aliases)
    * [Dialects](#dialects)
        * [`serialization_strategy` dialect option](#serialization_strategy-dialect-option)
        * [`serialize_by_alias` dialect option](#serialize_by_alias-dialect-option)
        * [`omit_none` dialect option](#omit_none-dialect-option)
        * [`omit_default` dialect option](#omit_default-dialect-option)
        * [`namedtuple_as_dict` dialect option](#namedtuple_as_dict-dialect-option)
        * [`no_copy_collections` dialect option](#no_copy_collections-dialect-option)
        * [Changing the default dialect](#changing-the-default-dialect)
    * [Discriminator](#discriminator)
        * [Subclasses distinguishable by a field](#subclasses-distinguishable-by-a-field)
        * [Subclasses without a common field](#subclasses-without-a-common-field)
        * [Class level discriminator](#class-level-discriminator)
        * [Working with union of classes](#working-with-union-of-classes)
        * [Using a custom variant tagger function](#using-a-custom-variant-tagger-function)
    * [Code generation options](#code-generation-options)
        * [Add `omit_none` keyword argument](#add-omit_none-keyword-argument)
        * [Add `by_alias` keyword argument](#add-by_alias-keyword-argument)
        * [Add `dialect` keyword argument](#add-dialect-keyword-argument)
        * [Add `context` keyword argument](#add-context-keyword-argument)
    * [Generic dataclasses](#generic-dataclasses)
        * [Generic dataclass inheritance](#generic-dataclass-inheritance)
        * [Generic dataclass in a field type](#generic-dataclass-in-a-field-type)
    * [GenericSerializableType interface](#genericserializabletype-interface)
    * [Serialization hooks](#serialization-hooks)
        * [Before deserialization](#before-deserialization)
        * [After deserialization](#after-deserialization)
        * [Before serialization](#before-serialization)
        * [After serialization](#after-serialization)
* [JSON Schema](#json-schema)
    * [Building JSON Schema](#building-json-schema)
    * [JSON Schema constraints](#json-schema-constraints)
    * [JSON Schema plugins](#json-schema-plugins)
    * [Extending JSON Schema](#extending-json-schema)
    * [JSON Schema and custom serialization methods](#json-schema-and-custom-serialization-methods)

Introduction
-------------------------------------------------------------------------------

This library provides two fundamentally different approaches to converting
your data to and from various formats. Each of them is useful in different
situations:

* Codecs
* Mixins

Codecs are represented by a set of decoder / encoder classes and
decode / encode functions for each supported format. You can use them
to convert data of any python built-in and third-party type to JSON, YAML,
TOML, MessagePack or a basic form accepted by other serialization formats.
For example, you can convert a list of datetime objects to JSON array
containing string-represented datetimes and vice versa.

Mixins are primarily for dataclass models. They are represented by mixin
classes that add methods for converting to and from JSON, YAML, TOML,
MessagePack or a basic form accepted by other serialization formats.
If you have a root dataclass model, then it will be the easiest way to make it
serializable. All you have to do is inherit a particular mixin class.

In addition to serialization functionality, this library also provides JSON
Schema builder that can be used in places where interoperability matters.

Installation
-------------------------------------------------------------------------------

Use pip to install:
```shell
$ pip install mashumaro
```

The current version of `mashumaro` supports Python versions 3.9 — 3.13.


It's not recommended to use any version of Python that has reached its
[end of life](https://devguide.python.org/versions/) and is no longer receiving
security updates or bug fixes from the Python development team.
For convenience, there is a table below that outlines the
last version of `mashumaro` that can be installed on unmaintained versions
of Python.

| Python Version | Last Version of mashumaro                                          | Python EOL |
|----------------|--------------------------------------------------------------------|------------|
| 3.8            | [3.14](https://github.com/Fatal1ty/mashumaro/releases/tag/v3.14)   | 2024-10-07 |
| 3.7            | [3.9.1](https://github.com/Fatal1ty/mashumaro/releases/tag/v3.9.1) | 2023-06-27 |
| 3.6            | [3.1.1](https://github.com/Fatal1ty/mashumaro/releases/tag/v3.1.1) | 2021-12-23 |


Changelog
-------------------------------------------------------------------------------

This project follows the principles of [Semantic Versioning](https://semver.org).
Changelog is available on [GitHub Releases page](https://github.com/Fatal1ty/mashumaro/releases).

Supported data types
-------------------------------------------------------------------------------

There is support for generic types from the standard [`typing`](https://docs.python.org/3/library/typing.html) module:
* [`List`](https://docs.python.org/3/library/typing.html#typing.List)
* [`Tuple`](https://docs.python.org/3/library/typing.html#typing.Tuple)
* [`NamedTuple`](https://docs.python.org/3/library/typing.html#typing.NamedTuple)
* [`Set`](https://docs.python.org/3/library/typing.html#typing.Set)
* [`FrozenSet`](https://docs.python.org/3/library/typing.html#typing.FrozenSet)
* [`Deque`](https://docs.python.org/3/library/typing.html#typing.Deque)
* [`Dict`](https://docs.python.org/3/library/typing.html#typing.Dict)
* [`OrderedDict`](https://docs.python.org/3/library/typing.html#typing.OrderedDict)
* [`DefaultDict`](https://docs.python.org/3/library/typing.html#typing.DefaultDict)
* [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict)
* [`Mapping`](https://docs.python.org/3/library/typing.html#typing.Mapping)
* [`MutableMapping`](https://docs.python.org/3/library/typing.html#typing.MutableMapping)
* [`Counter`](https://docs.python.org/3/library/typing.html#typing.Counter)
* [`ChainMap`](https://docs.python.org/3/library/typing.html#typing.ChainMap)
* [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)

for standard generic types on [PEP 585](https://www.python.org/dev/peps/pep-0585/) compatible Python (3.9+):
* [`list`](https://docs.python.org/3/library/stdtypes.html#list)
* [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)
* [`namedtuple`](https://docs.python.org/3/library/collections.html#collections.namedtuple)
* [`set`](https://docs.python.org/3/library/stdtypes.html#set)
* [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)
* [`collections.abc.Set`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Set)
* [`collections.abc.MutableSet`](https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableSet)
* [`collections.deque`](https://docs.python.org/3/library/collections.html#collections.deque)
* [`dict`](https://docs.python.org/3/library/stdtypes.html#dict)
* [`collections.OrderedDict`](https://docs.python.org/3/library/collections.html#collections.OrderedDict)
* [`collections.defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict)
* [`collections.abc.Mapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)
* [`collections.abc.MutableMapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableMapping)
* [`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter)
* [`collections.ChainMap`](https://docs.python.org/3/library/collections.html#collections.ChainMap)
* [`collections.abc.Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)
* [`collections.abc.MutableSequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableSequence)

for special primitives from the [`typing`](https://docs.python.org/3/library/typing.html) module:
* [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)
* [`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)
* [`Union`](https://docs.python.org/3/library/typing.html#typing.Union)
* [`TypeVar`](https://docs.python.org/3/library/typing.html#typing.TypeVar)
* [`TypeVarTuple`](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple)
* [`NewType`](https://docs.python.org/3/library/typing.html#newtype)
* [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated)
* [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)
* [`LiteralString`](https://docs.python.org/3/library/typing.html#typing.LiteralString)
* [`Final`](https://docs.python.org/3/library/typing.html#typing.Final)
* [`Self`](https://docs.python.org/3/library/typing.html#typing.Self)
* [`Unpack`](https://docs.python.org/3/library/typing.html#typing.Unpack)
* [`ReadOnly`](https://docs.python.org/3/library/typing.html#typing.ReadOnly)

for standard interpreter types from [`types`](https://docs.python.org/3/library/types.html#standard-interpreter-types) module:
* [`NoneType`](https://docs.python.org/3/library/types.html#types.NoneType)
* [`UnionType`](https://docs.python.org/3/library/types.html#types.UnionType)
* [`MappingProxyType`](https://docs.python.org/3/library/types.html#types.MappingProxyType)

for enumerations based on classes from the standard [`enum`](https://docs.python.org/3/library/enum.html) module:
* [`Enum`](https://docs.python.org/3/library/enum.html#enum.Enum)
* [`IntEnum`](https://docs.python.org/3/library/enum.html#enum.IntEnum)
* [`StrEnum`](https://docs.python.org/3/library/enum.html#enum.StrEnum)
* [`Flag`](https://docs.python.org/3/library/enum.html#enum.Flag)
* [`IntFlag`](https://docs.python.org/3/library/enum.html#enum.IntFlag)

for common built-in types:
* [`int`](https://docs.python.org/3/library/functions.html#int)
* [`float`](https://docs.python.org/3/library/functions.html#float)
* [`bool`](https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values)
* [`str`](https://docs.python.org/3/library/stdtypes.html#str)
* [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes)
* [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray)

for built-in datetime oriented types (see [more](#deserialize-option) details):
* [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime)
* [`date`](https://docs.python.org/3/library/datetime.html#datetime.date)
* [`time`](https://docs.python.org/3/library/datetime.html#datetime.time)
* [`timedelta`](https://docs.python.org/3/library/datetime.html#datetime.timedelta)
* [`timezone`](https://docs.python.org/3/library/datetime.html#datetime.timezone)
* [`ZoneInfo`](https://docs.python.org/3/library/zoneinfo.html#zoneinfo.ZoneInfo)

for pathlike types:
* [`PurePath`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath)
* [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)
* [`PurePosixPath`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePosixPath)
* [`PosixPath`](https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath)
* [`PureWindowsPath`](https://docs.python.org/3/library/pathlib.html#pathlib.PureWindowsPath)
* [`WindowsPath`](https://docs.python.org/3/library/pathlib.html#pathlib.WindowsPath)
* [`os.PathLike`](https://docs.python.org/3/library/os.html#os.PathLike)

for other less popular built-in types:
* [`uuid.UUID`](https://docs.python.org/3/library/uuid.html#uuid.UUID)
* [`decimal.Decimal`](https://docs.python.org/3/library/decimal.html#decimal.Decimal)
* [`fractions.Fraction`](https://docs.python.org/3/library/fractions.html#fractions.Fraction)
* [`ipaddress.IPv4Address`](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address)
* [`ipaddress.IPv6Address`](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv6Address)
* [`ipaddress.IPv4Network`](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Network)
* [`ipaddress.IPv6Network`](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv6Network)
* [`ipaddress.IPv4Interface`](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Interface)
* [`ipaddress.IPv6Interface`](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv6Interface)
* [`typing.Pattern`](https://docs.python.org/3/library/typing.html#typing.Pattern)
* [`re.Pattern`](https://docs.python.org/3/library/re.html#re.Pattern)

for backported types from [`typing-extensions`](https://github.com/python/typing_extensions):
* [`OrderedDict`](https://docs.python.org/3/library/typing.html#typing.OrderedDict)
* [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict)
* [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated)
* [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)
* [`LiteralString`](https://docs.python.org/3/library/typing.html#typing.LiteralString)
* [`Self`](https://docs.python.org/3/library/typing.html#typing.Self)
* [`TypeVarTuple`](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple)
* [`Unpack`](https://docs.python.org/3/library/typing.html#typing.Unpack)
* [`ReadOnly`](https://docs.python.org/3/library/typing.html#typing.ReadOnly)

for arbitrary types:
* [user-defined types](#user-defined-types)
* [third-party types](#third-party-types)
* [user-defined generic types](#user-defined-generic-types)
* [third-party generic types](#third-party-generic-types)

Usage example
-------------------------------------------------------------------------------

Suppose we're developing a financial application and we operate with currencies
and stocks:

```python
from dataclasses import dataclass
from enum import Enum

class Currency(Enum):
    USD = "USD"
    EUR = "EUR"

@dataclass
class CurrencyPosition:
    currency: Currency
    balance: float

@dataclass
class StockPosition:
    ticker: str
    name: str
    balance: int
```

Now we want a dataclass for portfolio that will be serialized to and from JSON.
We inherit `DataClassJSONMixin` that adds this functionality:

```python
from mashumaro.mixins.json import DataClassJSONMixin

...

@dataclass
class Portfolio(DataClassJSONMixin):
    currencies: list[CurrencyPosition]
    stocks: list[StockPosition]
```

Let's create a portfolio instance and check methods `from_json` and `to_json`:

```python
portfolio = Portfolio(
    currencies=[
        CurrencyPosition(Currency.USD, 238.67),
        CurrencyPosition(Currency.EUR, 361.84),
    ],
    stocks=[
        StockPosition("AAPL", "Apple", 10),
        StockPosition("AMZN", "Amazon", 10),
    ]
)

portfolio_json = portfolio.to_json()
assert Portfolio.from_json(portfolio_json) == portfolio
```

If we need to serialize something different from a root dataclass,
we can use codecs. In the following example we create a JSON decoder and encoder
for a list of currencies:

```python
from mashumaro.codecs.json import JSONDecoder, JSONEncoder

...

decoder = JSONDecoder(list[CurrencyPosition])
encoder = JSONEncoder(list[CurrencyPosition])

currencies = [
    CurrencyPosition(Currency.USD, 238.67),
    CurrencyPosition(Currency.EUR, 361.84),
]
currencies_json = encoder.encode(currencies)
assert decoder.decode(currencies_json) == currencies

```

How does it work?
-------------------------------------------------------------------------------

This library works by taking the schema of the data and generating a
specific decoder and encoder for exactly that schema, taking into account the
specifics of serialization format. This is much faster than inspection of
data types on every call of decoding or encoding at runtime.

These specific decoders and encoders are generated by
[codecs and mixins](#supported-serialization-formats):
* When using codecs, these methods are compiled during the creation of the
  decoder or encoder.
* When using serialization
mixins, these methods are compiled during import time (or at runtime in some
cases) and are set as attributes to your dataclasses. To minimize the import
time, you can explicitly enable
[lazy compilation](#lazy_compilation-config-option).

Benchmark
-------------------------------------------------------------------------------

* macOS 15.1 Sequoia
* Apple M3 Max
* 36GB RAM
* Python 3.13.0

Benchmark using [pyperf](https://github.com/psf/pyperf) with GitHub Issue model. Please note that the
following charts use logarithmic scale, as it is convenient for displaying
very large ranges of values.

<picture>
  <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/Fatal1ty/mashumaro/refs/heads/master/benchmark/charts/load_light.svg">
  <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Fatal1ty/mashumaro/refs/heads/master/benchmark/charts/load_dark.svg">
  <img src="https://raw.githubusercontent.com/Fatal1ty/mashumaro/refs/heads/master/benchmark/charts/load_light.svg" width="604">
</picture>
<picture>
  <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/Fatal1ty/mashumaro/refs/heads/master/benchmark/charts/dump_light.svg">
  <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Fatal1ty/mashumaro/refs/heads/master/benchmark/charts/dump_dark.svg">
  <img src="https://raw.githubusercontent.com/Fatal1ty/mashumaro/refs/heads/master/benchmark/charts/dump_light.svg" width="604">
</picture>

> [!NOTE]\
> Benchmark results may vary depending on the specific configuration and
> parameters used for serialization and deserialization. However, we have made
> an attempt to use the available options that can speed up and smooth out the
> differences in how libraries work.

To run benchmark in your environment:
```bash
git clone git@github.com:Fatal1ty/mashumaro.git
cd mashumaro
python3 -m venv env && source env/bin/activate
pip install -e .
pip install -r requirements-dev.txt
./benchmark/run.sh
```

Supported serialization formats
-------------------------------------------------------------------------------

This library has built-in support for multiple popular formats:

* [JSON](https://www.json.org)
* [YAML](https://yaml.org)
* [TOML](https://toml.io)
* [MessagePack](https://msgpack.org)

There are preconfigured codecs and mixin classes. However, you're free
to override some settings if necessary.

> [!IMPORTANT]\
> As for codecs, you are
> offered to choose between convenience and efficiency. When you need to decode
> or encode typed data more than once, it's highly recommended to create
> and reuse a decoder or encoder specifically for that data type. For one-time
> use with default settings it may be convenient to use global functions that
> create a disposable decoder or encoder under the hood. Remember that you
> should not use these convenient global functions more that once for the same
> data type if performance is important to you.

### Basic form

Basic form denotes a python object consisting only of basic data types
supported by most serialization formats. These types are:
[`str`](https://docs.python.org/3/library/stdtypes.html#str),
[`int`](https://docs.python.org/3/library/functions.html#int),
[`float`](https://docs.python.org/3/library/functions.html#float),
[`bool`](https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values),
[`list`](https://docs.python.org/3/library/stdtypes.html#list),
[`dict`](https://docs.python.org/3/library/stdtypes.html#dict).

This is also a starting point you can play with for a comprehensive
transformation of your data.

Efficient decoder and encoder can be used as follows:

```python
from mashumaro.codecs import BasicDecoder, BasicEncoder
# or from mashumaro.codecs.basic import BasicDecoder, BasicEncoder

decoder = BasicDecoder(<shape_type>, ...)
decoder.decode(...)

encoder = BasicEncoder(<shape_type>, ...)
encoder.encode(...)
```

Convenient functions are recommended to be used as follows:
```python
import mashumaro.codecs.basic as basic_codec

basic_codec.decode(..., <shape_type>)
basic_codec.encode(..., <shape_type>)
```

Mixin can be used as follows:
```python
from mashumaro import DataClassDictMixin
# or from mashumaro.mixins.dict import DataClassDictMixin

@dataclass
class MyModel(DataClassDictMixin):
    ...

MyModel.from_dict(...)
MyModel(...).to_dict()
```

> [!TIP]\
> You don't need to inherit `DataClassDictMixin` along with other serialization
> mixins because it's a base class for them.

### JSON

[JSON](https://www.json.org) is a lightweight data-interchange format. You can
choose between standard library
[json](https://docs.python.org/3/library/json.html) for compatibility and
third-party dependency [orjson](https://pypi.org/project/orjson/) for better
performance.

#### json library

Efficient decoder and encoder can be used as follows:
```python
from mashumaro.codecs.json import JSONDecoder, JSONEncoder

decoder = JSONDecoder(<shape_type>, ...)
decoder.decode(...)

encoder = JSONEncoder(<shape_type>, ...)
encoder.encode(...)
```

Convenient functions can be used as follows:
```python
from mashumaro.codecs.json import json_decode, json_encode

json_decode(..., <shape_type>)
json_encode(..., <shape_type>)
```

Convenient function aliases are recommended to be used as follows:
```python
import mashumaro.codecs.json as json_codec

json_codec.decode(...<shape_type>)
json_codec.encode(..., <shape_type>)
```

Mixin can be used as follows:
```python
from mashumaro.mixins.json import DataClassJSONMixin

@dataclass
class MyModel(DataClassJSONMixin):
    ...

MyModel.from_json(...)
MyModel(...).to_json()
```

#### orjson library

In order to use [`orjson`](https://pypi.org/project/orjson/) library, it must
be installed manually or using an extra option for `mashumaro`:

```shell
pip install mashumaro[orjson]
```

The following data types will be handled by
[`orjson`](https://pypi.org/project/orjson/) library by default:
* [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime)
* [`date`](https://docs.python.org/3/library/datetime.html#datetime.date)
* [`time`](https://docs.python.org/3/library/datetime.html#datetime.time)
* [`uuid.UUID`](https://docs.python.org/3/library/uuid.html#uuid.UUID)

Efficient decoder and encoder can be used as follows:
```python
from mashumaro.codecs.orjson import ORJSONDecoder, ORJSONEncoder

decoder = ORJSONDecoder(<shape_type>, ...)
decoder.decode(...)

encoder = ORJSONEncoder(<shape_type>, ...)
encoder.encode(...)
```

Convenient functions can be used as follows:
```python
from mashumaro.codecs.orjson import json_decode, json_encode

json_decode(..., <shape_type>)
json_encode(..., <shape_type>)
```

Convenient function aliases are recommended to be used as follows:
```python
import mashumaro.codecs.orjson as json_codec

json_codec.decode(...<shape_type>)
json_codec.encode(..., <shape_type>)
```

Mixin can be used as follows:
```python
from mashumaro.mixins.orjson import DataClassORJSONMixin

@dataclass
class MyModel(DataClassORJSONMixin):
    ...

MyModel.from_json(...)
MyModel(...).to_json()
MyModel(...).to_jsonb()
```

### YAML

[YAML](https://yaml.org) is a human-friendly data serialization language for
all programming languages. In order to use this format, the
[`pyyaml`](https://pypi.org/project/PyYAML/) package must be installed.
You can install it manually or using an extra option for `mashumaro`:

```shell
pip install mashumaro[yaml]
```

Efficient decoder and encoder can be used as follows:
```python
from mashumaro.codecs.yaml import YAMLDecoder, YAMLEncoder

decoder = YAMLDecoder(<shape_type>, ...)
decoder.decode(...)

encoder = YAMLEncoder(<shape_type>, ...)
encoder.encode(...)
```

Convenient functions can be used as follows:
```python
from mashumaro.codecs.yaml import yaml_decode, yaml_encode

yaml_decode(..., <shape_type>)
yaml_encode(..., <shape_type>)
```

Convenient function aliases are recommended to be used as follows:
```python
import mashumaro.codecs.yaml as yaml_codec

yaml_codec.decode(...<shape_type>)
yaml_codec.encode(..., <shape_type>)
```

Mixin can be used as follows:
```python
from mashumaro.mixins.yaml import DataClassYAMLMixin

@dataclass
class MyModel(DataClassYAMLMixin):
    ...

MyModel.from_yaml(...)
MyModel(...).to_yaml()
```

### TOML

[TOML](https://toml.io) is config file format for humans.
In order to use this format, the [`tomli`](https://pypi.org/project/tomli/) and
[`tomli-w`](https://pypi.org/project/tomli-w/) packages must be installed.
In Python 3.11+, `tomli` is included as
[`tomlib`](https://docs.python.org/3/library/tomllib.html) standard library
module and is used for this format. You can install the missing packages
manually or using an extra option
for `mashumaro`:

```shell
pip install mashumaro[toml]
```

The following data types will be handled by
[`tomli`](https://pypi.org/project/tomli/)/
[`tomli-w`](https://pypi.org/project/tomli-w/) library by default:
* [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime)
* [`date`](https://docs.python.org/3/library/datetime.html#datetime.date)
* [`time`](https://docs.python.org/3/library/datetime.html#datetime.time)

Fields with value `None` will be omitted on serialization because TOML
doesn't support null values.

Efficient decoder and encoder can be used as follows:
```python
from mashumaro.codecs.toml import TOMLDecoder, TOMLEncoder

decoder = TOMLDecoder(<shape_type>, ...)
decoder.decode(...)

encoder = TOMLEncoder(<shape_type>, ...)
encoder.encode(...)
```

Convenient functions can be used as follows:
```python
from mashumaro.codecs.toml import toml_decode, toml_encode

toml_decode(..., <shape_type>)
toml_encode(..., <shape_type>)
```

Convenient function aliases are recommended to be used as follows:
```python
import mashumaro.codecs.toml as toml_codec

toml_codec.decode(...<shape_type>)
toml_codec.encode(..., <shape_type>)
```

Mixin can be used as follows:
```python
from mashumaro.mixins.toml import DataClassTOMLMixin

@dataclass
class MyModel(DataClassTOMLMixin):
    ...

MyModel.from_toml(...)
MyModel(...).to_toml()
```

### MessagePack

[MessagePack](https://msgpack.org) is an efficient binary serialization format.
In order to use this mixin, the [`msgpack`](https://pypi.org/project/msgpack/)
package must be installed. You can install it manually or using an extra
option for `mashumaro`:

```shell
pip install mashumaro[msgpack]
```

The following data types will be handled by
[`msgpack`](https://pypi.org/project/msgpack/) library by default:
* [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes)
* [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray)

Efficient decoder and encoder can be used as follows:
```python
from mashumaro.codecs.msgpack import MessagePackDecoder, MessagePackEncoder

decoder = MessagePackDecoder(<shape_type>, ...)
decoder.decode(...)

encoder = MessagePackEncoder(<shape_type>, ...)
encoder.encode(...)
```

Convenient functions can be used as follows:
```python
from mashumaro.codecs.msgpack import msgpack_decode, msgpack_encode

msgpack_decode(..., <shape_type>)
msgpack_encode(..., <shape_type>)
```

Convenient function aliases are recommended to be used as follows:
```python
import mashumaro.codecs.msgpack as msgpack_codec

msgpack_codec.decode(...<shape_type>)
msgpack_codec.encode(..., <shape_type>)
```

Mixin can be used as follows:
```python
from mashumaro.mixins.msgpack import DataClassMessagePackMixin

@dataclass
class MyModel(DataClassMessagePackMixin):
    ...

MyModel.from_msgpack(...)
MyModel(...).to_msgpack()
```

Customization
-------------------------------------------------------------------------------

Customization options of `mashumaro` are extensive and will most likely cover your needs.
When it comes to non-standard data types and non-standard serialization support, you can do the following:
* Turn an existing regular or generic class into a serializable one
by inheriting the [`SerializableType`](#serializabletype-interface) class
* Write different serialization strategies for an existing regular or generic type that is not under your control
using [`SerializationStrategy`](#serializationstrategy) class
* Define serialization / deserialization methods:
  * for a specific dataclass field by using [field options](#field-options)
  * for a specific data type used in the dataclass by using [`Config`](#config-options) class
* Alter input and output data with serialization / deserialization [hooks](#serialization-hooks)
* Separate serialization scheme from a dataclass in a reusable manner using [dialects](#dialects)
* Choose from predefined serialization engines for the specific data types, e.g. `datetime` and `NamedTuple`

### SerializableType interface

If you have a custom class or hierarchy of classes whose instances you want
to serialize with `mashumaro`, the first option is to implement
`SerializableType` interface.

#### User-defined types

Let's look at this not very practicable example:

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.types import SerializableType

class Airport(SerializableType):
    def __init__(self, code, city):
        self.code, self.city = code, city

    def _serialize(self):
        return [self.code, self.city]

    @classmethod
    def _deserialize(cls, value):
        return cls(*value)

    def __eq__(self, other):
        return self.code, self.city == other.code, other.city

@dataclass
class Flight(DataClassDictMixin):
    origin: Airport
    destination: Airport

JFK = Airport("JFK", "New York City")
LAX = Airport("LAX", "Los Angeles")

input_data = {
    "origin": ["JFK", "New York City"],
    "destination": ["LAX", "Los Angeles"]
}
my_flight = Flight.from_dict(input_data)
assert my_flight == Flight(JFK, LAX)
assert my_flight.to_dict() == input_data
```

You can see how `Airport` instances are seamlessly created from lists of two
strings and serialized into them.

By default `_deserialize` method will get raw input data without any
transformations before. This should be enough in many cases, especially when
you need to perform non-standard transformations yourself, but let's extend
our example:

```python
class Itinerary(SerializableType):
    def __init__(self, flights):
        self.flights = flights

    def _serialize(self):
        return self.flights

    @classmethod
    def _deserialize(cls, flights):
        return cls(flights)

@dataclass
class TravelPlan(DataClassDictMixin):
    budget: float
    itinerary: Itinerary

input_data = {
    "budget": 10_000,
    "itinerary": [
        {
            "origin": ["JFK", "New York City"],
            "destination": ["LAX", "Los Angeles"]
        },
        {
            "origin": ["LAX", "Los Angeles"],
            "destination": ["SFO", "San Fransisco"]
        }
    ]
}
```

If we pass the flight list as is into `Itinerary._deserialize`, our itinerary
will have something that we may not expect — `list[dict]` instead of
`list[Flight]`. The solution is quite simple. Instead of calling
`Flight._deserialize` yourself, just use annotations:

```python
class Itinerary(SerializableType, use_annotations=True):
    def __init__(self, flights):
        self.flights = flights

    def _serialize(self) -> list[Flight]:
        return self.flights

    @classmethod
    def _deserialize(cls, flights: list[Flight]):
        return cls(flights)

my_plan = TravelPlan.from_dict(input_data)
assert isinstance(my_plan.itinerary.flights[0], Flight)
assert isinstance(my_plan.itinerary.flights[1], Flight)
assert my_plan.to_dict() == input_data
```

Here we add annotations to the only argument of `_deserialize` method and
to the return value of `_serialize` method as well. The latter is needed for
correct serialization.

> [!IMPORTANT]\
> The importance of explicit passing `use_annotations=True` when defining a
> class is that otherwise implicit using annotations might break compatibility
> with old code that wasn't aware of this feature. It will be enabled by
> default in the future major release.

#### User-defined generic types

The great thing to note about using annotations in `SerializableType` is that
they work seamlessly with [generic](https://docs.python.org/3/library/typing.html#user-defined-generic-types)
and [variadic generic](https://peps.python.org/pep-0646/) types.
Let's see how this can be useful:

```python
from datetime import date
from typing import TypeVar
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.types import SerializableType

KT = TypeVar("KT")
VT = TypeVar("VT")

class DictWrapper(dict[KT, VT], SerializableType, use_annotations=True):
    def _serialize(self) -> dict[KT, VT]:
        return dict(self)

    @classmethod
    def _deserialize(cls, value: dict[KT, VT]) -> 'DictWrapper[KT, VT]':
        return cls(value)

@dataclass
class DataClass(DataClassDictMixin):
    x: DictWrapper[date, str]
    y: DictWrapper[str, date]

input_data = {
    "x": {"2022-12-07": "2022-12-07"},
    "y": {"2022-12-07": "2022-12-07"}
}
obj = DataClass.from_dict(input_data)
assert obj == DataClass(
    x=DictWrapper({date(2022, 12, 7): "2022-12-07"}),
    y=DictWrapper({"2022-12-07": date(2022, 12, 7)})
)
assert obj.to_dict() == input_data
```

You can see that formatted date is deserialized to `date` object before passing
to `DictWrapper._deserialize` in a key or value according to the generic
parameters.

If you have generic dataclass types, you can use `SerializableType` for them as well, but it's not necessary since
they're [supported](#generic-dataclasses) out of the box.

### SerializationStrategy

If you want to add support for a custom third-party type that is not under your control,
you can write serialization and deserialization logic inside `SerializationStrategy` class,
which will be reusable and so well suited in case that third-party type is widely used.
`SerializationStrategy` is also good if you want to create strategies that are slightly different from each other,
because you can add the strategy differentiator in the `__init__` method.

#### Third-party types

To demonstrate how `SerializationStrategy` works let's write a simple strategy for datetime serialization
in different formats. In this example we will use the same strategy class for two dataclass fields,
but a string representing the date and time will be different.

```python
from dataclasses import dataclass, field
from datetime import datetime
from mashumaro import DataClassDictMixin, field_options
from mashumaro.types import SerializationStrategy

class FormattedDateTime(SerializationStrategy):
    def __init__(self, fmt):
        self.fmt = fmt

    def serialize(self, value: datetime) -> str:
        return value.strftime(self.fmt)

    def deserialize(self, value: str) -> datetime:
        return datetime.strptime(value, self.fmt)

@dataclass
class DateTimeFormats(DataClassDictMixin):
    short: datetime = field(
        metadata=field_options(
            serialization_strategy=FormattedDateTime("%d%m%Y%H%M%S")
        )
    )
    verbose: datetime = field(
        metadata=field_options(
            serialization_strategy=FormattedDateTime("%A %B %d, %Y, %H:%M:%S")
        )
    )

formats = DateTimeFormats(
    short=datetime(2019, 1, 1, 12),
    verbose=datetime(2019, 1, 1, 12),
)
dictionary = formats.to_dict()
# {'short': '01012019120000', 'verbose': 'Tuesday January 01, 2019, 12:00:00'}
assert DateTimeFormats.from_dict(dictionary) == formats
```

Similarly to `SerializableType`, `SerializationStrategy` could also take advantage of annotations:

```python
from dataclasses import dataclass
from datetime import datetime
from mashumaro import DataClassDictMixin
from mashumaro.types import SerializationStrategy

class TsSerializationStrategy(SerializationStrategy, use_annotations=True):
    def serialize(self, value: datetime) -> float:
        return value.timestamp()

    def deserialize(self, value: float) -> datetime:
        # value will be converted to float before being passed to this method
        return datetime.fromtimestamp(value)

@dataclass
class Example(DataClassDictMixin):
    dt: datetime

    class Config:
        serialization_strategy = {
            datetime: TsSerializationStrategy(),
        }

example = Example.from_dict({"dt": "1672531200"})
print(example)
# Example(dt=datetime.datetime(2023, 1, 1, 3, 0))
print(example.to_dict())
# {'dt': 1672531200.0}
```

Here the passed string value `"1672531200"` will be converted to `float` before being passed to `deserialize` method
thanks to the `float` annotation.

> [!IMPORTANT]\
> As well as for `SerializableType`, the value of `use_annotatons` will be
> `True` by default in the future major release.

#### Third-party generic types

To create a generic version of a serialization strategy you need to follow these steps:
* inherit [`Generic[...]`](https://docs.python.org/3/library/typing.html#typing.Generic) type
with the number of parameters matching the number of parameters
of the target generic type
* Write generic annotations for `serialize` method's return type and for `deserialize` method's argument type
* Use the origin type of the target generic type in the [`serialization_strategy`](#serialization_strategy-config-option) config section
([`typing.get_origin`](https://docs.python.org/3/library/typing.html#typing.get_origin) might be helpful)

There is no need to add `use_annotations=True` here because it's enabled implicitly
for generic serialization strategies.

For example, there is a third-party [multidict](https://pypi.org/project/multidict/) package that has a generic `MultiDict` type.
A generic serialization strategy for it might look like this:

```python
from dataclasses import dataclass
from datetime import date
from pprint import pprint
from typing import Generic, List, Tuple, TypeVar
from mashumaro import DataClassDictMixin
from mashumaro.types import SerializationStrategy

from multidict import MultiDict

T = TypeVar("T")

class MultiDictSerializationStrategy(SerializationStrategy, Generic[T]):
    def serialize(self, value: MultiDict[T]) -> List[Tuple[str, T]]:
        return [(k, v) for k, v in value.items()]

    def deserialize(self, value: List[Tuple[str, T]]) -> MultiDict[T]:
        return MultiDict(value)


@dataclass
class Example(DataClassDictMixin):
    floats: MultiDict[float]
    date_lists: MultiDict[List[date]]

    class Config:
        serialization_strategy = {
            MultiDict: MultiDictSerializationStrategy()
        }

example = Example(
    floats=MultiDict([("x", 1.1), ("x", 2.2)]),
    date_lists=MultiDict(
        [("x", [date(2023, 1, 1), date(2023, 1, 2)]),
         ("x", [date(2023, 2, 1), date(2023, 2, 2)])]
    ),
)
pprint(example.to_dict())
# {'date_lists': [['x', ['2023-01-01', '2023-01-02']],
#                 ['x', ['2023-02-01', '2023-02-02']]],
#  'floats': [['x', 1.1], ['x', 2.2]]}
assert Example.from_dict(example.to_dict()) == example
```

### Field options

In some cases creating a new class just for one little thing could be
excessive. Moreover, you may need to deal with third party classes that you are
not allowed to change. You can use [`dataclasses.field`](https://docs.python.org/3/library/dataclasses.html#dataclasses.field) function to
configure some serialization aspects through its `metadata` parameter. Next
section describes all supported options to use in `metadata` mapping.

If you don't want to remember the names of the options you can use
`field_options` helper function:

```python
from dataclasses import dataclass, field
from mashumaro import field_options

@dataclass
class A:
    x: int = field(metadata=field_options(...))
```

#### `serialize` option

This option allows you to change the serialization method. When using
this option, the serialization behaviour depends on what type of value the
option has. It could be either `Callable[[Any], Any]` or `str`.

A value of type `Callable[[Any], Any]` is a generic way to specify any callable
object like a function, a class method, a class instance method, an instance
of a callable class or even a lambda function to be called for serialization.

A value of type `str` sets a specific engine for serialization. Keep in mind
that all possible engines depend on the data type that this option is used
with. At this moment there are next serialization engines to choose from:

| Applicable data types      | Supported engines    | Description                                                                                                                                                                                                  |
|:---------------------------|:---------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `NamedTuple`, `namedtuple` | `as_list`, `as_dict` | How to pack named tuples. By default `as_list` engine is used that means your named tuple class instance will be packed into a list of its values. You can pack it into a dictionary using `as_dict` engine. |
| `Any`                      | `omit`               | Skip the field during serialization                                                                                                                                                                          |

> [!TIP]\
> You can pass a field value as is without changes on serialization using
[`pass_through`](#passing-field-values-as-is).

Example:

```python
from datetime import datetime
from dataclasses import dataclass, field
from typing import NamedTuple
from mashumaro import DataClassDictMixin

class MyNamedTuple(NamedTuple):
    x: int
    y: float

@dataclass
class A(DataClassDictMixin):
    dt: datetime = field(
        metadata={
            "serialize": lambda v: v.strftime('%Y-%m-%d %H:%M:%S')
        }
    )
    t: MyNamedTuple = field(metadata={"serialize": "as_dict"})
```

#### `deserialize` option

This option allows you to change the deserialization method. When using
this option, the deserialization behaviour depends on what type of value the
option has. It could be either `Callable[[Any], Any]` or `str`.

A value of type `Callable[[Any], Any]` is a generic way to specify any callable
object like a function, a class method, a class instance method, an instance
of a callable class or even a lambda function to be called for deserialization.

A value of type `str` sets a specific engine for deserialization. Keep in mind
that all possible engines depend on the data type that this option is used
with. At this moment there are next deserialization engines to choose from:

| Applicable data types      | Supported engines                                                                                                                   | Description                                                                                                                                                                                                                                                                                             |
|:---------------------------|:------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `datetime`, `date`, `time` | [`ciso8601`](https://github.com/closeio/ciso8601#supported-subset-of-iso-8601), [`pendulum`](https://github.com/sdispater/pendulum) | How to parse datetime string. By default native [`fromisoformat`](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat) of corresponding class will be used for `datetime`, `date` and `time` fields. It's the fastest way in most cases, but you can choose an alternative. |
| `NamedTuple`, `namedtuple` | `as_list`, `as_dict`                                                                                                                | How to unpack named tuples. By default `as_list` engine is used that means your named tuple class instance will be created from a list of its values. You can unpack it from a dictionary using `as_dict` engine.                                                                                       |

> [!TIP]\
> You can pass a field value as is without changes on deserialization using
[`pass_through`](#passing-field-values-as-is).

Example:

```python
from datetime import datetime
from dataclasses import dataclass, field
from typing import List, NamedTuple
from mashumaro import DataClassDictMixin
import ciso8601
import dateutil

class MyNamedTuple(NamedTuple):
    x: int
    y: float

@dataclass
class A(DataClassDictMixin):
    x: datetime = field(
        metadata={"deserialize": "pendulum"}
    )

class B(DataClassDictMixin):
    x: datetime = field(
        metadata={"deserialize": ciso8601.parse_datetime_as_naive}
    )

@dataclass
class C(DataClassDictMixin):
    dt: List[datetime] = field(
        metadata={
            "deserialize": lambda l: list(map(dateutil.parser.isoparse, l))
        }
    )

@dataclass
class D(DataClassDictMixin):
    x: MyNamedTuple = field(metadata={"deserialize": "as_dict"})
```

#### `serialization_strategy` option

This option is useful when you want to change the serialization logic
for a dataclass field depending on some defined parameters using a reusable
serialization scheme. You can find an example in the
[`SerializationStrategy`](#serializationstrategy) chapter.

> [!TIP]\
> You can pass a field value as is without changes on
> serialization / deserialization using
[`pass_through`](#passing-field-values-as-is).

#### `alias` option

This option can be used to assign [field aliases](#field-aliases):


```python
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin, field_options

@dataclass
class DataClass(DataClassDictMixin):
    a: int = field(metadata=field_options(alias="FieldA"))
    b: int = field(metadata=field_options(alias="#invalid"))

x = DataClass.from_dict({"FieldA": 1, "#invalid": 2})  # DataClass(a=1, b=2)
```

### Config options

If inheritance is not an empty word for you, you'll fall in love with the
`Config` class. You can register `serialize` and `deserialize` methods, define
code generation options and other things just in one place. Or in some
classes in different ways if you need flexibility. Inheritance is always on the
first place.

There is a base class `BaseConfig` that you can inherit for the sake of
convenience, but it's not mandatory.

In the following example you can see how
the `debug` flag is changed from class to class: `ModelA` will have debug mode enabled but
`ModelB` will not.

```python
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig

class BaseModel(DataClassDictMixin):
    class Config(BaseConfig):
        debug = True

class ModelA(BaseModel):
    a: int

class ModelB(BaseModel):
    b: int

    class Config(BaseConfig):
        debug = False
```

Next section describes all supported options to use in the config.

#### `debug` config option

If you enable the `debug` option the generated code for your data class
will be printed.

#### `code_generation_options` config option

Some users may need functionality that wouldn't exist without extra cost such
as valuable cpu time to execute additional instructions. Since not everyone
needs such instructions, they can be enabled by a constant in the list,
so the fastest basic behavior of the library will always remain by default.
The following table provides a brief overview of all the available constants
described below.

| Constant                                                        | Description                                                          |
|:----------------------------------------------------------------|:---------------------------------------------------------------------|
| [`TO_DICT_ADD_OMIT_NONE_FLAG`](#add-omit_none-keyword-argument) | Adds `omit_none` keyword-only argument to `to_*` methods.            |
| [`TO_DICT_ADD_BY_ALIAS_FLAG`](#add-by_alias-keyword-argument)   | Adds `by_alias` keyword-only argument to `to_*` methods.             |
| [`ADD_DIALECT_SUPPORT`](#add-dialect-keyword-argument)          | Adds `dialect` keyword-only argument to `from_*` and `to_*` methods. |
| [`ADD_SERIALIZATION_CONTEXT`](#add-context-keyword-argument)    | Adds `context` keyword-only argument to `to_*` methods.              |

#### `serialization_strategy` config option

You can register custom [`SerializationStrategy`](#serializationstrategy), `serialize` and `deserialize`
methods for specific types just in one place. It could be configured using
a dictionary with types as keys. The value could be either a
[`SerializationStrategy`](#serializationstrategy) instance or a dictionary with `serialize` and
`deserialize` values with the same meaning as in the
[field options](#field-options).

```python
from dataclasses import dataclass
from datetime import datetime, date
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
from mashumaro.types import SerializationStrategy

class FormattedDateTime(SerializationStrategy):
    def __init__(self, fmt):
        self.fmt = fmt

    def serialize(self, value: datetime) -> str:
        return value.strftime(self.fmt)

    def deserialize(self, value: str) -> datetime:
        return datetime.strptime(value, self.fmt)

@dataclass
class DataClass(DataClassDictMixin):

    x: datetime
    y: date

    class Config(BaseConfig):
        serialization_strategy = {
            datetime: FormattedDateTime("%Y"),
            date: {
                # you can use specific str values for datetime here as well
                "deserialize": "pendulum",
                "serialize": date.isoformat,
            },
        }

instance = DataClass.from_dict({"x": "2021", "y": "2021"})
# DataClass(x=datetime.datetime(2021, 1, 1, 0, 0), y=Date(2021, 1, 1))
dictionary = instance.to_dict()
# {'x': '2021', 'y': '2021-01-01'}
```

Note that you can register different methods for multiple logical types which
are based on the same type using `NewType` and `Annotated`,
see [Extending existing types](#extending-existing-types) for details.

It's also possible to define a generic (de)serialization method for a generic
type by registering a method for its
[origin](https://docs.python.org/3/library/typing.html#typing.get_origin) type.
Although this technique is widely used when working with [third-party generic
types](#third-party-generic-types) using generic strategies, it can also be
applied in simple scenarios:

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin

@dataclass
class C(DataClassDictMixin):
    ints: list[int]
    floats: list[float]

    class Config:
        serialization_strategy = {
            list: {  # origin type for list[int] and list[float] is list
                "serialize": lambda x: list(map(str, x)),
            }
        }

assert C([1], [2.2]).to_dict() == {'ints': ['1'], 'floats': ['2.2']}
```

#### `aliases` config option

Sometimes it's better to write the [field aliases](#field-aliases) in one place. You can mix
aliases here with [aliases in the field options](#alias-option), but the last ones will always
take precedence.

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig

@dataclass
class DataClass(DataClassDictMixin):
    a: int
    b: int

    class Config(BaseConfig):
        aliases = {
            "a": "FieldA",
            "b": "FieldB",
        }

DataClass.from_dict({"FieldA": 1, "FieldB": 2})  # DataClass(a=1, b=2)
```

#### `serialize_by_alias` config option

All the fields with [aliases](#field-aliases) will be serialized by them by
default when this option is enabled. You can mix this config option with
[`by_alias`](#add-by_alias-keyword-argument) keyword argument.

```python
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin, field_options
from mashumaro.config import BaseConfig

@dataclass
class DataClass(DataClassDictMixin):
    field_a: int = field(metadata=field_options(alias="FieldA"))

    class Config(BaseConfig):
        serialize_by_alias = True

DataClass(field_a=1).to_dict()  # {'FieldA': 1}
```

#### `allow_deserialization_not_by_alias` config option

When using aliases, the deserializer defaults to requiring the keys to match
what is defined as the alias.
If the flexibility to deserialize aliased and unaliased keys is required then
the config option `allow_deserialization_not_by_alias ` can be set to
enable the feature.

```python
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig


@dataclass
class AliasedDataClass(DataClassDictMixin):
    foo: int = field(metadata={"alias": "alias_foo"})
    bar: int = field(metadata={"alias": "alias_bar"})

    class Config(BaseConfig):
        allow_deserialization_not_by_alias = True


alias_dict = {"alias_foo": 1, "alias_bar": 2}
t1 = AliasedDataClass.from_dict(alias_dict)

no_alias_dict = {"foo": 1, "bar": 2}
# This would raise `mashumaro.exceptions.MissingField`
# if allow_deserialization_not_by_alias was False
t2 = AliasedDataClass.from_dict(no_alias_dict)
assert t1 == t2
```

#### `omit_none` config option

All the fields with `None` values will be skipped during serialization by
default when this option is enabled. You can mix this config option with
[`omit_none`](#add-omit_none-keyword-argument) keyword argument.

```python
from dataclasses import dataclass
from typing import Optional
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig

@dataclass
class DataClass(DataClassDictMixin):
    x: Optional[int] = 42

    class Config(BaseConfig):
        omit_none = True

DataClass(x=None).to_dict()  # {}
```

#### `omit_default` config option

When this option enabled, all the fields that have values equal to the defaults
or the default_factory results will be skipped during serialization.

```python
from dataclasses import dataclass, field
from typing import List, Optional, Tuple
from mashumaro import DataClassDictMixin, field_options
from mashumaro.config import BaseConfig

@dataclass
class Foo:
    foo: str

@dataclass
class DataClass(DataClassDictMixin):
    a: int = 42
    b: Tuple[int, ...] = field(default=(1, 2, 3))
    c: List[Foo] = field(default_factory=lambda: [Foo("foo")])
    d: Optional[str] = None

    class Config(BaseConfig):
        omit_default = True

DataClass(a=42, b=(1, 2, 3), c=[Foo("foo")]).to_dict()  # {}
```

#### `namedtuple_as_dict` config option

Dataclasses are a great way to declare and use data models. But it's not the only way.
Python has a typed version of [namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple)
called [NamedTuple](https://docs.python.org/3/library/typing.html#typing.NamedTuple)
which looks similar to dataclasses:

```python
from typing import NamedTuple

class Point(NamedTuple):
    x: int
    y: int
```

the same with a dataclass will look like this:

```python
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int
```

At first glance, you can use both options. But imagine that you need to create
a bunch of instances of the `Point` class. Due to how dataclasses work you will
have more memory consumption compared to named tuples. In such a case it could
be more appropriate to use named tuples.

By default, all named tuples are packed into lists. But with `namedtuple_as_dict`
option you have a drop-in replacement for dataclasses:

```python
from dataclasses import dataclass
from typing import List, NamedTuple
from mashumaro import DataClassDictMixin

class Point(NamedTuple):
    x: int
    y: int

@dataclass
class DataClass(DataClassDictMixin):
    points: List[Point]

    class Config:
        namedtuple_as_dict = True

obj = DataClass.from_dict({"points": [{"x": 0, "y": 0}, {"x": 1, "y": 1}]})
print(obj.to_dict())  # {"points": [{"x": 0, "y": 0}, {"x": 1, "y": 1}]}
```

If you want to serialize only certain named tuple fields as dictionaries, you
can use the corresponding [serialization](#serialize-option) and
[deserialization](#deserialize-option) engines.

#### `allow_postponed_evaluation` config option

[PEP 563](https://www.python.org/dev/peps/pep-0563/) solved the problem of forward references by postponing the evaluation
of annotations, so you can write the following code:

```python
from __future__ import annotations
from dataclasses import dataclass
from mashumaro import DataClassDictMixin

@dataclass
class A(DataClassDictMixin):
    x: B

@dataclass
class B(DataClassDictMixin):
    y: int

obj = A.from_dict({'x': {'y': 1}})
```

You don't need to write anything special here, forward references work out of
the box. If a field of a dataclass has a forward reference in the type
annotations, building of `from_*` and `to_*` methods of this dataclass
will be postponed until they are called once. However, if for some reason you
don't want the evaluation to be possibly postponed, you can disable it using
`allow_postponed_evaluation` option:

```python
from __future__ import annotations
from dataclasses import dataclass
from mashumaro import DataClassDictMixin

@dataclass
class A(DataClassDictMixin):
    x: B

    class Config:
        allow_postponed_evaluation = False

# UnresolvedTypeReferenceError: Class A has unresolved type reference B
# in some of its fields

@dataclass
class B(DataClassDictMixin):
    y: int
```

In this case you will get `UnresolvedTypeReferenceError` regardless of whether
class B is declared below or not.

#### `dialect` config option

This option is described [below](#changing-the-default-dialect) in the
Dialects section.

#### `orjson_options` config option

This option changes default options for `orjson.dumps` encoder which is
used in [`DataClassORJSONMixin`](#dataclassorjsonmixin). For example, you can
tell orjson to handle non-`str` `dict` keys as the built-in `json.dumps`
encoder does. See [orjson documentation](https://github.com/ijl/orjson#option)
to read more about these options.

```python
import orjson
from dataclasses import dataclass
from typing import Dict
from mashumaro.config import BaseConfig
from mashumaro.mixins.orjson import DataClassORJSONMixin

@dataclass
class MyClass(DataClassORJSONMixin):
    x: Dict[int, int]

    class Config(BaseConfig):
        orjson_options = orjson.OPT_NON_STR_KEYS

assert MyClass({1: 2}).to_json() == {"1": 2}
```

#### `discriminator` config option

This option is described in the
[Class level discriminator](#class-level-discriminator) section.

#### `lazy_compilation` config option

By using this option, the compilation of the `from_*` and `to_*` methods will
be deferred until they are called first time. This will reduce the import time
and, in certain instances, may enhance the speed of deserialization
by leveraging the data that is accessible after the class has been created.

> [!CAUTION]\
> If you need to save a reference to `from_*` or `to_*` method, you should
> do it after the method is compiled. To be safe, you can always use lambda
> function:
> ```python
> from_dict = lambda x: MyModel.from_dict(x)
> to_dict = lambda x: x.to_dict()
> ```

#### `sort_keys` config option

When set, the keys on serialized dataclasses will be sorted in alphabetical order.

Unlike the `sort_keys` option in the standard library's `json.dumps` function, this option acts at class creation time and has no effect on the performance of serialization.

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig

@dataclass
class SortedDataClass(DataClassDictMixin):
    foo: int
    bar: int

    class Config(BaseConfig):
        sort_keys = True

t = SortedDataClass(1, 2)
assert t.to_dict() == {"bar": 2, "foo": 1}
```

#### `forbid_extra_keys` config option

When set, the deserialization of dataclasses will fail if the input dictionary contains keys that are not present in the dataclass.

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig

@dataclass
class DataClass(DataClassDictMixin):
    a: int

    class Config(BaseConfig):
        forbid_extra_keys = True

DataClass.from_dict({"a": 1, "b": 2})  # ExtraKeysError: Extra keys: {'b'}
```

It plays well with `aliases` and `allow_deserialization_not_by_alias` options.

### Passing field values as is

In some cases it's needed to pass a field value as is without any changes
during serialization / deserialization. There is a predefined
[`pass_through`](https://github.com/Fatal1ty/mashumaro/blob/master/mashumaro/helper.py#L58)
object that can be used as `serialization_strategy` or
`serialize` / `deserialize` options:

```python
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin, pass_through

class MyClass:
    def __init__(self, some_value):
        self.some_value = some_value

@dataclass
class A1(DataClassDictMixin):
    x: MyClass = field(
        metadata={
            "serialize": pass_through,
            "deserialize": pass_through,
        }
    )

@dataclass
class A2(DataClassDictMixin):
    x: MyClass = field(
        metadata={
            "serialization_strategy": pass_through,
        }
    )

@dataclass
class A3(DataClassDictMixin):
    x: MyClass

    class Config:
        serialization_strategy = {
            MyClass: pass_through,
        }

@dataclass
class A4(DataClassDictMixin):
    x: MyClass

    class Config:
        serialization_strategy = {
            MyClass: {
                "serialize": pass_through,
                "deserialize": pass_through,
            }
        }

my_class_instance = MyClass(42)

assert A1.from_dict({'x': my_class_instance}).x == my_class_instance
assert A2.from_dict({'x': my_class_instance}).x == my_class_instance
assert A3.from_dict({'x': my_class_instance}).x == my_class_instance
assert A4.from_dict({'x': my_class_instance}).x == my_class_instance

a1_dict = A1(my_class_instance).to_dict()
a2_dict = A2(my_class_instance).to_dict()
a3_dict = A3(my_class_instance).to_dict()
a4_dict = A4(my_class_instance).to_dict()

assert a1_dict == a2_dict == a3_dict == a4_dict == {"x": my_class_instance}
```

### Extending existing types

There are situations where you might want some values of the same type to be
treated as their own type. You can create new logical types with
[`NewType`](https://docs.python.org/3/library/typing.html#newtype),
[`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated)
or [`TypeAliasType`](https://docs.python.org/3/library/typing.html#typing.TypeAliasType)
and register serialization strategies for them:

```python
from typing import Mapping, NewType, Annotated
from dataclasses import dataclass
from mashumaro import DataClassDictMixin

SessionID = NewType("SessionID", str)
AccountID = Annotated[str, "AccountID"]

type DeviceID = str

@dataclass
class Context(DataClassDictMixin):
    account_sessions: Mapping[AccountID, SessionID]
    account_devices: list[DeviceID]

    class Config:
        serialization_strategy = {
            AccountID: {
                "deserialize": lambda x: ...,
                "serialize": lambda x: ...,
            },
            SessionID: {
                "deserialize": lambda x: ...,
                "serialize": lambda x: ...,
            },
            DeviceID: {
                "deserialize": lambda x: ...,
                "serialize": lambda x: ...,
            }
        }
```

Although using `NewType` is usually the most reliable way to avoid logical
errors, you have to pay for it with notable overhead. If you are creating
dataclass instances manually, then you know that type checkers will
enforce you to enclose a value in your `"NewType"` callable, which leads
to performance degradation:

```python
python -m timeit -s "from typing import NewType; MyInt = NewType('MyInt', int)" "MyInt(42)"
10000000 loops, best of 5: 31.1 nsec per loop

python -m timeit -s "from typing import NewType; MyInt = NewType('MyInt', int)" "42"
50000000 loops, best of 5: 4.35 nsec per loop
```

However, when you create dataclass instances using the `from_*` method provided
by one of the mixins or using one of the decoders, there will be no performance
degradation, because the value won't be enclosed in the callable in the
generated code. Therefore, if performance is more important to you than
catching logical errors by type checkers, and you are actively creating or
changing dataclasses manually, then you should take a closer look at using
`Annotated`.

### Field aliases

In some cases it's better to have different names for a field in your dataclass
and in its serialized view. For example, a third-party legacy API you are
working with might operate with camel case style, but you stick to snake case
style in your code base. Or you want to load data with keys that are
invalid identifiers in Python. Aliases can solve this problem.

There are multiple ways to assign an alias:
* Using `Alias(...)` annotation in a field type
* Using `alias` parameter in field metadata
* Using `aliases` parameter in a dataclass config

By default, aliases only affect deserialization, but it can be extended to
serialization as well. If you want to serialize all the fields by aliases you
have two options to do so:
* [`serialize_by_alias` config option](#serialize_by_alias-config-option)
* [`serialize_by_alias` dialect option](#serialize_by_alias-dialect-option)
* [`by_alias` keyword argument in `to_*` methods](#add-by_alias-keyword-argument)

Here is an example with `Alias` annotation in a field type:

```python
from dataclasses import dataclass
from typing import Annotated
from mashumaro import DataClassDictMixin
from mashumaro.types import Alias

@dataclass
class DataClass(DataClassDictMixin):
    foo_bar: Annotated[int, Alias("fooBar")]

obj = DataClass.from_dict({"fooBar": 42})  # DataClass(foo_bar=42)
obj.to_dict()  # {"foo_bar": 42}  # no aliases on serialization by default
```

The same with field metadata:

```python
from dataclasses import dataclass, field
from mashumaro import field_options

@dataclass
class DataClass:
    foo_bar: str = field(metadata=field_options(alias="fooBar"))
```

And with a dataclass config:

```python
from dataclasses import dataclass
from mashumaro.config import BaseConfig

@dataclass
class DataClass:
    foo_bar: str

    class Config(BaseConfig):
        aliases = {"foo_bar": "fooBar"}
```

> [!TIP]\
> If you want to deserialize all the fields by its names along with aliases,
> there is [a config option](#allow_deserialization_not_by_alias-config-option)
> for that.

### Dialects

Sometimes it's needed to have different serialization and deserialization
methods depending on the data source where entities of the dataclass are
stored or on the API to which the entities are being sent or received from.
There is a special `Dialect` type that may contain all the differences from the
default serialization and deserialization methods. You can create different
dialects and use each of them for the same dataclass depending on
the situation.

Suppose we have the following dataclass with a field of type `date`:
```python
@dataclass
class Entity(DataClassDictMixin):
    dt: date
```

By default, a field of `date` type serializes to a string in ISO 8601 format,
so the serialized entity will look like `{'dt': '2021-12-31'}`. But what if we
have, for example, two sensitive legacy Ethiopian and Japanese APIs that use
two different formats for dates — `dd/mm/yyyy` and `yyyy年mm月dd日`? Instead of
creating two similar dataclasses we can have one dataclass and two dialects:
```python
from dataclasses import dataclass
from datetime import date, datetime
from mashumaro import DataClassDictMixin
from mashumaro.config import ADD_DIALECT_SUPPORT
from mashumaro.dialect import Dialect
from mashumaro.types import SerializationStrategy

class DateTimeSerializationStrategy(SerializationStrategy):
    def __init__(self, fmt: str):
        self.fmt = fmt

    def serialize(self, value: date) -> str:
        return value.strftime(self.fmt)

    def deserialize(self, value: str) -> date:
        return datetime.strptime(value, self.fmt).date()

class EthiopianDialect(Dialect):
    serialization_strategy = {
        date: DateTimeSerializationStrategy("%d/%m/%Y")
    }

class JapaneseDialect(Dialect):
    serialization_strategy = {
        date: DateTimeSerializationStrategy("%Y年%m月%d日")
    }

@dataclass
class Entity(DataClassDictMixin):
    dt: date

    class Config:
        code_generation_options = [ADD_DIALECT_SUPPORT]

entity = Entity(date(2021, 12, 31))
entity.to_dict(dialect=EthiopianDialect)  # {'dt': '31/12/2021'}
entity.to_dict(dialect=JapaneseDialect)   # {'dt': '2021年12月31日'}
Entity.from_dict({'dt': '2021年12月31日'}, dialect=JapaneseDialect)
```

#### `serialization_strategy` dialect option

This dialect option has the same meaning as the
[similar config option](#serialization_strategy-config-option)
but for the dialect scope. You can register custom [`SerializationStrategy`](#serializationstrategy),
`serialize` and `deserialize` methods for the specific types.

#### `serialize_by_alias` dialect option

This dialect option has the same meaning as the
[similar config option](#serialize_by_alias-config-option)
but for the dialect scope.

#### `omit_none` dialect option

This dialect option has the same meaning as the
[similar config option](#omit_none-config-option) but for the dialect scope.

#### `omit_default` dialect option

This dialect option has the same meaning as the
[similar config option](#omitdefault-config-option) but for the dialect scope.

#### `namedtuple_as_dict` dialect option

This dialect option has the same meaning as the
[similar config option](#namedtuple_as_dict-config-option)
but for the dialect scope.

#### `no_copy_collections` dialect option

By default, all collection data types are serialized as a copy to prevent
mutation of the original collection. As an example, if a dataclass contains
a field of type `list[str]`, then it will be serialized as a copy of the
original list, so you can safely mutate it after. The downside is that copying
is always slower than using a reference to the original collection. In some
cases we know beforehand that mutation doesn't take place or is even desirable,
so we can benefit from avoiding unnecessary copies by setting
`no_copy_collections` to a sequence of origin collection data types.
This is applicable only for collections containing elements that do not
require conversion.

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
from mashumaro.dialect import Dialect

class NoCopyDialect(Dialect):
    no_copy_collections = (list, dict, set)

@dataclass
class DataClass(DataClassDictMixin):
    simple_list: list[str]
    simple_dict: dict[str, str]
    simple_set: set[str]

    class Config(BaseConfig):
        dialect = NoCopyDialect

obj = DataClass(["foo"], {"bar": "baz"}, {"foobar"})
data = obj.to_dict()

assert data["simple_list"] is obj.simple_list
assert data["simple_dict"] is obj.simple_dict
assert data["simple_set"] is obj.simple_set
```

This option is enabled for `list` and `dict` in the default dialects that
belong to mixins and codecs for the following formats:
* [JSON (orjson library)](#orjson-library)
* [TOML](#toml)
* [MessagePack](#messagepack)

#### Changing the default dialect

You can change the default serialization and deserialization methods not only
in the [`serialization_strategy`](#serialization_strategy-config-option) config
option but also using the `dialect` config option. If you have multiple
dataclasses without a common parent class the default dialect can help you
to reduce the number of code lines written:

```python
@dataclass
class Entity(DataClassDictMixin):
    dt: date

    class Config:
        dialect = JapaneseDialect

entity = Entity(date(2021, 12, 31))
entity.to_dict()  # {'dt': '2021年12月31日'}
assert Entity.from_dict({'dt': '2021年12月31日'}) == entity
```

Default dialect can also be set when using codecs:
```python
from mashumaro.codecs import BasicDecoder, BasicEncoder

@dataclass
class Entity:
    dt: date

decoder = BasicDecoder(Entity, default_dialect=JapaneseDialect)
encoder = BasicEncoder(Entity, default_dialect=JapaneseDialect)

entity = Entity(date(2021, 12, 31))
encoder.encode(entity) # {'dt': '2021年12月31日'}
assert decoder.decode({'dt': '2021年12月31日'}) == entity
```

### Discriminator

There is a special `Discriminator` class that allows you to customize how
a union of dataclasses or their hierarchy will be deserialized.
It has the following parameters that affects class selection rules:

* `field` — optional name of the input dictionary key (also known as tag)
  by which all the variants can be distinguished
* `include_subtypes` — allow to deserialize subclasses
* `include_supertypes` — allow to deserialize superclasses
* `variant_tagger_fn` — a custom function used to generate tag values
  associated with a variant

By default, each variant that you want to discriminate by tags should have a
class-level attribute containing an associated tag value. This attribute should
have a name defined by `field` parameter. The tag value coule be in the
following forms:

* without annotations: `type = 42`
* annotated as ClassVar: `type: ClassVar[int] = 42`
* annotated as Final: `type: Final[int] = 42`
* annotated as Literal: `type: Literal[42] = 42`
* annotated as StrEnum: `type: ResponseType = ResponseType.OK`

> [!NOTE]\
> Keep in mind that by default only Final, Literal and StrEnum fields are
> processed during serialization.

However, it is possible to use discriminator without the class-level
attribute. You can provide a custom function that generates one or many variant
tag values. This function should take a class as the only argument and return
either a single value of the basic type like `str` or `int` or a list of them
to associate multiple tags with a variant. The common practice is to use
a class name as a single tag value:

```python
variant_tagger_fn = lambda cls: cls.__name__
```

Next, we will look at different use cases, as well as their pros and cons.

#### Subclasses distinguishable by a field

Often you have a base dataclass and multiple subclasses that are easily
distinguishable from each other by the value of a particular field.
For example, there may be different events, messages or requests with
a discriminator field "event_type", "message_type" or just "type". You could've
listed all of them within `Union` type, but it would be too verbose and
impractical. Moreover, deserialization of the union would be slow, since we
need to iterate over each variant in the list until we find the right one.

We can improve subclass deserialization using `Discriminator` as annotation
within `Annotated` type. We will use `field` parameter and set
`include_subtypes` to `True`.

> [!IMPORTANT]\
> The discriminator field should be accessible from the `__dict__` attribute
> of a specific descendant, i.e. defined at the level of that descendant.
> A descendant class without a discriminator field will be ignored, but
> its descendants won't.

Suppose we have a hierarchy of client events distinguishable by a class
attribute "type":

```python
from dataclasses import dataclass
from ipaddress import IPv4Address
from mashumaro import DataClassDictMixin

@dataclass
class ClientEvent(DataClassDictMixin):
    pass

@dataclass
class ClientConnectedEvent(ClientEvent):
    type = "connected"
    client_ip: IPv4Address

@dataclass
class ClientDisconnectedEvent(ClientEvent):
    type = "disconnected"
    client_ip: IPv4Address
```

We use base dataclass `ClientEvent` for a field of another dataclass:

```python
from typing import Annotated, List
# or from typing_extensions import Annotated
from mashumaro.types import Discriminator


@dataclass
class AggregatedEvents(DataClassDictMixin):
    list: List[
        Annotated[
            ClientEvent, Discriminator(field="type", include_subtypes=True)
        ]
    ]
```

Now we can deserialize events based on "type" value:

```python
events = AggregatedEvents.from_dict(
    {
        "list": [
            {"type": "connected", "client_ip": "10.0.0.42"},
            {"type": "disconnected", "client_ip": "10.0.0.42"},
        ]
    }
)
assert events == AggregatedEvents(
    list=[
        ClientConnectedEvent(client_ip=IPv4Address("10.0.0.42")),
        ClientDisconnectedEvent(client_ip=IPv4Address("10.0.0.42")),
    ]
)
```

#### Subclasses without a common field

In rare cases you have to deal with subclasses that don't have a common field
name which they can be distinguished by. Since `Discriminator` can be
initialized without "field" parameter you can use it with only
`include_subclasses` enabled. The drawback is that we will have to go through all
the subclasses until we find the suitable one. It's almost like using `Union`
type but with subclasses support.

Suppose we're making a brunch. We have some ingredients:

```python
@dataclass
class Ingredient(DataClassDictMixin):
    name: str

@dataclass
class Hummus(Ingredient):
    made_of: Literal["chickpeas", "beet", "artichoke"]
    grams: int

@dataclass
class Celery(Ingredient):
    pieces: int
```

Let's create a plate:

```python
@dataclass
class Plate(DataClassDictMixin):
    ingredients: List[
        Annotated[Ingredient, Discriminator(include_subtypes=True)]
    ]
```

And now we can put our ingredients on the plate:

```python
plate = Plate.from_dict(
    {
        "ingredients": [
            {
                "name": "hummus from the shop",
                "made_of": "chickpeas",
                "grams": 150,
            },
            {"name": "celery from my garden", "pieces": 5},
        ]
    }
)
assert plate == Plate(
    ingredients=[
        Hummus(name="hummus from the shop", made_of="chickpeas", grams=150),
        Celery(name="celery from my garden", pieces=5),
    ]
)
```

In some cases it's necessary to fall back to the base class if there is no
suitable subclass. We can set `include_supertypes` to `True`:

```python
@dataclass
class Plate(DataClassDictMixin):
    ingredients: List[
        Annotated[
            Ingredient,
            Discriminator(include_subtypes=True, include_supertypes=True),
        ]
    ]

plate = Plate.from_dict(
    {
        "ingredients": [
            {
                "name": "hummus from the shop",
                "made_of": "chickpeas",
                "grams": 150,
            },
            {"name": "celery from my garden", "pieces": 5},
            {"name": "cumin"}  # <- new unknown ingredient
        ]
    }
)
assert plate == Plate(
    ingredients=[
        Hummus(name="hummus from the shop", made_of="chickpeas", grams=150),
        Celery(name="celery from my garden", pieces=5),
        Ingredient(name="cumin"),  # <- unknown ingredient added
    ]
)
```

#### Class level discriminator

It may often be more convenient to specify a `Discriminator` once at the class
level and use that class without `Annotated` type for subclass deserialization.
Depending on the `Discriminator` parameters, it can be used as a replacement for
[subclasses distinguishable by a field](#subclasses-distinguishable-by-a-field)
as well as for [subclasses without a common field](#subclasses-without-a-common-field).
The only difference is that you can't use `include_supertypes=True` because
it would lead to a recursion error.

Reworked example will look like this:

```python
from dataclasses import dataclass
from ipaddress import IPv4Address
from typing import List
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
from mashumaro.types import Discriminator

@dataclass
class ClientEvent(DataClassDictMixin):
    class Config(BaseConfig):
        discriminator = Discriminator(  # <- add discriminator
            field="type",
            include_subtypes=True,
        )

@dataclass
class ClientConnectedEvent(ClientEvent):
    type = "connected"
    client_ip: IPv4Address

@dataclass
class ClientDisconnectedEvent(ClientEvent):
    type = "disconnected"
    client_ip: IPv4Address

@dataclass
class AggregatedEvents(DataClassDictMixin):
    list: List[ClientEvent]  # <- use base class here
```

And now we can deserialize events based on "type" value as we did earlier:

```python
events = AggregatedEvents.from_dict(
    {
        "list": [
            {"type": "connected", "client_ip": "10.0.0.42"},
            {"type": "disconnected", "client_ip": "10.0.0.42"},
        ]
    }
)
assert events == AggregatedEvents(
    list=[
        ClientConnectedEvent(client_ip=IPv4Address("10.0.0.42")),
        ClientDisconnectedEvent(client_ip=IPv4Address("10.0.0.42")),
    ]
)
```

What's more interesting is that you can now deserialize subclasses simply by
calling the superclass `from_*` method, which is very useful:
```python
disconnected_event = ClientEvent.from_dict(
    {"type": "disconnected", "client_ip": "10.0.0.42"}
)
assert disconnected_event == ClientDisconnectedEvent(IPv4Address("10.0.0.42"))
```

The same is applicable for subclasses without a common field:

```python
@dataclass
class Ingredient(DataClassDictMixin):
    name: str

    class Config:
        discriminator = Discriminator(include_subtypes=True)

...

celery = Ingredient.from_dict({"name": "celery from my garden", "pieces": 5})
assert celery == Celery(name="celery from my garden", pieces=5)
```

#### Working with union of classes

Deserialization of union of types distinguishable by a particular field will
be much faster using `Discriminator` because there will be no traversal
of all classes and an attempt to deserialize each of them.
Usually this approach can be used when you have multiple classes without a
common superclass or when you only need to deserialize some of the subclasses.
In the following example we will use `include_supertypes=True` to
deserialize two subclasses out of three:

```python
from dataclasses import dataclass
from typing import Annotated, Literal, Union
# or from typing_extensions import Annotated
from mashumaro import DataClassDictMixin
from mashumaro.types import Discriminator

@dataclass
class Event(DataClassDictMixin):
    pass

@dataclass
class Event1(Event):
    code: Literal[1] = 1
    ...

@dataclass
class Event2(Event):
    code: Literal[2] = 2
    ...

@dataclass
class Event3(Event):
    code: Literal[3] = 3
    ...

@dataclass
class Message(DataClassDictMixin):
    event: Annotated[
        Union[Event1, Event2],
        Discriminator(field="code", include_supertypes=True),
    ]

event1_msg = Message.from_dict({"event": {"code": 1, ...}})
event2_msg = Message.from_dict({"event": {"code": 2, ...}})
assert isinstance(event1_msg.event, Event1)
assert isinstance(event2_msg.event, Event2)

# raises InvalidFieldValue:
Message.from_dict({"event": {"code": 3, ...}})
```

Again, it's not necessary to have a common superclass. If you have a union of
dataclasses without a field that they can be distinguishable by, you can still
use `Discriminator`, but deserialization will almost be the same as for `Union`
type without `Discriminator` except that it could be possible to deserialize
subclasses with `include_subtypes=True`.

> [!IMPORTANT]\
> When both `include_subtypes` and `include_supertypes` are enabled,
> all subclasses will be attempted to be deserialized first,
> superclasses — at the end.

In the following example you can see how priority works — first we try
to deserialize `ChickpeaHummus`, and if it fails, then we try `Hummus`:

```python
@dataclass
class Hummus(DataClassDictMixin):
    made_of: Literal["chickpeas", "artichoke"]
    grams: int

@dataclass
class ChickpeaHummus(Hummus):
    made_of: Literal["chickpeas"]

@dataclass
class Celery(DataClassDictMixin):
    pieces: int

@dataclass
class Plate(DataClassDictMixin):
    ingredients: List[
        Annotated[
            Union[Hummus, Celery],
            Discriminator(include_subtypes=True, include_supertypes=True),
        ]
    ]

plate = Plate.from_dict(
    {
        "ingredients": [
            {"made_of": "chickpeas", "grams": 100},
            {"made_of": "artichoke", "grams": 50},
            {"pieces": 4},
        ]
    }
)
assert plate == Plate(
    ingredients=[
        ChickpeaHummus(made_of='chickpeas', grams=100),  # <- subclass
        Hummus(made_of='artichoke', grams=50),  # <- superclass
        Celery(pieces=4),
    ]
)
```

#### Using a custom variant tagger function

Sometimes it is impractical to have a class-level attribute with a tag value,
especially when you have a lot of classes. We can have a custom tagger
function instead. This method is applicable for all scenarios of using
the discriminator, but for demonstration purposes, let's focus only on one
of them.

Suppose we want to use the middle part of `Client*Event` as a tag value:

```python
from dataclasses import dataclass
from ipaddress import IPv4Address
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
from mashumaro.types import Discriminator


def client_event_tagger(cls):
    # not the best way of doing it, it's just a demo
    return cls.__name__[6:-5].lower()

@dataclass
class ClientEvent(DataClassDictMixin):
    class Config(BaseConfig):
        discriminator = Discriminator(
            field="type",
            include_subtypes=True,
            variant_tagger_fn=client_event_tagger,
        )

@dataclass
class ClientConnectedEvent(ClientEvent):
    client_ip: IPv4Address

@dataclass
class ClientDisconnectedEvent(ClientEvent):
    client_ip: IPv4Address
```

We can now deserialize subclasses as we did it earlier
[without variant tagger](#class-level-discriminator):
```python
disconnected_event = ClientEvent.from_dict(
    {"type": "disconnected", "client_ip": "10.0.0.42"}
)
assert disconnected_event == ClientDisconnectedEvent(IPv4Address("10.0.0.42"))
```

If we need to associate multiple tags with a single variant, we can return
a list of tags:

```python
def client_event_tagger(cls):
    name = cls.__name__[6:-5]
    return [name.lower(), name.upper()]
```

### Code generation options

#### Add `omit_none` keyword argument

If you want to have control over whether to skip `None` values on serialization
you can add `omit_none` parameter to `to_*` methods using the
`code_generation_options` list. The default value of `omit_none`
parameter depends on whether the [`omit_none`](#omit_none-config-option)
config option or [`omit_none`](#omit_none-dialect-option) dialect option is enabled.

```python
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig, TO_DICT_ADD_OMIT_NONE_FLAG

@dataclass
class Inner(DataClassDictMixin):
    x: int = None
    # "x" won't be omitted since there is no TO_DICT_ADD_OMIT_NONE_FLAG here

@dataclass
class Model(DataClassDictMixin):
    x: Inner
    a: int = None
    b: str = None  # will be omitted

    class Config(BaseConfig):
        code_generation_options = [TO_DICT_ADD_OMIT_NONE_FLAG]

Model(x=Inner(), a=1).to_dict(omit_none=True)  # {'x': {'x': None}, 'a': 1}
```

#### Add `by_alias` keyword argument

If you want to have control over whether to serialize fields by their
[aliases](#field-aliases) you can add `by_alias` parameter to `to_*` methods
using the `code_generation_options` list. The default value of `by_alias`
parameter depends on whether the [`serialize_by_alias`](#serialize_by_alias-config-option)
config option is enabled.

```python
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin, field_options
from mashumaro.config import BaseConfig, TO_DICT_ADD_BY_ALIAS_FLAG

@dataclass
class DataClass(DataClassDictMixin):
    field_a: int = field(metadata=field_options(alias="FieldA"))

    class Config(BaseConfig):
        code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]

DataClass(field_a=1).to_dict()  # {'field_a': 1}
DataClass(field_a=1).to_dict(by_alias=True)  # {'FieldA': 1}
```

#### Add `dialect` keyword argument

Support for [dialects](#dialects) is disabled by default for performance reasons. You can enable
it using a `ADD_DIALECT_SUPPORT` constant:
```python
from dataclasses import dataclass
from datetime import date
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig, ADD_DIALECT_SUPPORT

@dataclass
class Entity(DataClassDictMixin):
    dt: date

    class Config(BaseConfig):
        code_generation_options = [ADD_DIALECT_SUPPORT]
```

#### Add `context` keyword argument

Sometimes it's needed to pass a "context" object to the serialization hooks
that will take it into account. For example, you could want to have an option
to remove sensitive data from the serialization result if you need to.
You can add `context` parameter to `to_*` methods that will be passed to
[`__pre_serialize__`](#before-serialization) and
[`__post_serialize__`](#after-serialization) hooks. The type of this context
as well as its mutability is up to you.

```python
from dataclasses import dataclass
from typing import Dict, Optional
from uuid import UUID
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig, ADD_SERIALIZATION_CONTEXT

class BaseModel(DataClassDictMixin):
    class Config(BaseConfig):
        code_generation_options = [ADD_SERIALIZATION_CONTEXT]

@dataclass
class Account(BaseModel):
    id: UUID
    username: str
    name: str

    def __pre_serialize__(self, context: Optional[Dict] = None):
        return self

    def __post_serialize__(self, d: Dict, context: Optional[Dict] = None):
        if context and context.get("remove_sensitive_data"):
            d["username"] = "***"
            d["name"] = "***"
        return d

@dataclass
class Session(BaseModel):
    id: UUID
    key: str
    account: Account

    def __pre_serialize__(self, context: Optional[Dict] = None):
        return self

    def __post_serialize__(self, d: Dict, context: Optional[Dict] = None):
        if context and context.get("remove_sensitive_data"):
            d["key"] = "***"
        return d


foo = Session(
    id=UUID('03321c9f-6a97-421e-9869-918ff2867a71'),
    key="VQ6Q9bX4c8s",
    account=Account(
        id=UUID('4ef2baa7-edef-4d6a-b496-71e6d72c58fb'),
        username="john_doe",
        name="John"
    )
)
assert foo.to_dict() == {
    'id': '03321c9f-6a97-421e-9869-918ff2867a71',
    'key': 'VQ6Q9bX4c8s',
    'account': {
        'id': '4ef2baa7-edef-4d6a-b496-71e6d72c58fb',
        'username': 'john_doe',
        'name': 'John'
    }
}
assert foo.to_dict(context={"remove_sensitive_data": True}) == {
    'id': '03321c9f-6a97-421e-9869-918ff2867a71',
    'key': '***',
    'account': {
        'id': '4ef2baa7-edef-4d6a-b496-71e6d72c58fb',
        'username': '***',
        'name': '***'
    }
}
```

### Generic dataclasses

Along with [user-defined generic types](#user-defined-generic-types)
implementing `SerializableType` interface, generic and variadic
generic dataclasses can also be used. There are two applicable scenarios
for them.

#### Generic dataclass inheritance

If you have a generic dataclass and want to serialize and deserialize its
instances depending on the concrete types, you can use inheritance for that:

```python
from dataclasses import dataclass
from datetime import date
from typing import Generic, Mapping, TypeVar, TypeVarTuple
from mashumaro import DataClassDictMixin

KT = TypeVar("KT")
VT = TypeVar("VT", date, str)
Ts = TypeVarTuple("Ts")

@dataclass
class GenericDataClass(Generic[KT, VT, *Ts]):
    x: Mapping[KT, VT]
    y: Tuple[*Ts, KT]

@dataclass
class ConcreteDataClass(
    GenericDataClass[str, date, *Tuple[float, ...]],
    DataClassDictMixin,
):
    pass

ConcreteDataClass.from_dict({"x": {"a": "2021-01-01"}, "y": [1, 2, "a"]})
# ConcreteDataClass(x={'a': datetime.date(2021, 1, 1)}, y=(1.0, 2.0, 'a'))
```

You can override `TypeVar` field with a concrete type or another `TypeVar`.
Partial specification of concrete types is also allowed. If a generic dataclass
is inherited without type overriding the types of its fields remain untouched.

#### Generic dataclass in a field type

Another approach is to specify concrete types in the field type hints. This can
help to have different versions of the same generic dataclass:

```python
from dataclasses import dataclass
from datetime import date
from typing import Generic, TypeVar
from mashumaro import DataClassDictMixin

T = TypeVar('T')

@dataclass
class GenericDataClass(Generic[T], DataClassDictMixin):
    x: T

@dataclass
class DataClass(DataClassDictMixin):
    date: GenericDataClass[date]
    str: GenericDataClass[str]

instance = DataClass(
    date=GenericDataClass(x=date(2021, 1, 1)),
    str=GenericDataClass(x='2021-01-01'),
)
dictionary = {'date': {'x': '2021-01-01'}, 'str': {'x': '2021-01-01'}}
assert DataClass.from_dict(dictionary) == instance
```

### GenericSerializableType interface

There is a generic alternative to [`SerializableType`](#serializabletype-interface)
called `GenericSerializableType`. It makes it possible to decide yourself how
to serialize and deserialize input data depending on the types provided:

```python
from dataclasses import dataclass
from datetime import date
from typing import Dict, TypeVar
from mashumaro import DataClassDictMixin
from mashumaro.types import GenericSerializableType

KT = TypeVar("KT")
VT = TypeVar("VT")

class DictWrapper(Dict[KT, VT], GenericSerializableType):
    __packers__ = {date: lambda x: x.isoformat(), str: str}
    __unpackers__ = {date: date.fromisoformat, str: str}

    def _serialize(self, types) -> Dict[KT, VT]:
        k_type, v_type = types
        k_conv = self.__packers__[k_type]
        v_conv = self.__packers__[v_type]
        return {k_conv(k): v_conv(v) for k, v in self.items()}

    @classmethod
    def _deserialize(cls, value, types) -> "DictWrapper[KT, VT]":
        k_type, v_type = types
        k_conv = cls.__unpackers__[k_type]
        v_conv = cls.__unpackers__[v_type]
        return cls({k_conv(k): v_conv(v) for k, v in value.items()})

@dataclass
class DataClass(DataClassDictMixin):
    x: DictWrapper[date, str]
    y: DictWrapper[str, date]

input_data = {
    "x": {"2022-12-07": "2022-12-07"},
    "y": {"2022-12-07": "2022-12-07"},
}
obj = DataClass.from_dict(input_data)
assert obj == DataClass(
    x=DictWrapper({date(2022, 12, 7): "2022-12-07"}),
    y=DictWrapper({"2022-12-07": date(2022, 12, 7)}),
)
assert obj.to_dict() == input_data
```

As you can see, the code turns out to be massive compared to the
[alternative](#user-defined-generic-types) but in rare cases such flexibility
can be useful. You should think twice about whether it's really worth using it.

### Serialization hooks

In some cases you need to prepare input / output data or do some extraordinary
actions at different stages of the deserialization / serialization lifecycle.
You can do this with different types of hooks.

#### Before deserialization

For doing something with a dictionary that will be passed to deserialization
you can use `__pre_deserialize__` class method:

```python
@dataclass
class A(DataClassJSONMixin):
    abc: int

    @classmethod
    def __pre_deserialize__(cls, d: Dict[Any, Any]) -> Dict[Any, Any]:
        return {k.lower(): v for k, v in d.items()}

print(DataClass.from_dict({"ABC": 123}))    # DataClass(abc=123)
print(DataClass.from_json('{"ABC": 123}'))  # DataClass(abc=123)
```

#### After deserialization

For doing something with a dataclass instance that was created as a result
of deserialization you can use `__post_deserialize__` class method:

```python
@dataclass
class A(DataClassJSONMixin):
    abc: int

    @classmethod
    def __post_deserialize__(cls, obj: 'A') -> 'A':
        obj.abc = 456
        return obj

print(DataClass.from_dict({"abc": 123}))    # DataClass(abc=456)
print(DataClass.from_json('{"abc": 123}'))  # DataClass(abc=456)
```

#### Before serialization

For doing something before serialization you can use `__pre_serialize__`
method:

```python
@dataclass
class A(DataClassJSONMixin):
    abc: int
    counter: ClassVar[int] = 0

    def __pre_serialize__(self) -> 'A':
        self.counter += 1
        return self

obj = DataClass(abc=123)
obj.to_dict()
obj.to_json()
print(obj.counter)  # 2
```

Note that you can add an additional `context` argument using the
[corresponding](#add-context-keyword-argument) code generation option.

#### After serialization

For doing something with a dictionary that was created as a result of
serialization you can use `__post_serialize__` method:

```python
@dataclass
class A(DataClassJSONMixin):
    user: str
    password: str

    def __post_serialize__(self, d: Dict[Any, Any]) -> Dict[Any, Any]:
        d.pop('password')
        return d

obj = DataClass(user="name", password="secret")
print(obj.to_dict())  # {"user": "name"}
print(obj.to_json())  # '{"user": "name"}'
```

Note that you can add an additional `context` argument using the
[corresponding](#add-context-keyword-argument) code generation option.

JSON Schema
-------------------------------------------------------------------------------

You can build JSON Schema not only for dataclasses but also for any other
[supported](#supported-data-types) data
types. There is support for the following standards:
* [Draft 2020-12](https://json-schema.org/specification.html)
* [OpenAPI Specification 3.1.1](https://spec.openapis.org/oas/v3.1.1)

### Building JSON Schema

For simple one-time cases it's recommended to start from using a configurable
`build_json_schema` function. It returns `JSONSchema` object that can be
serialized to json or to dict:

```python
from dataclasses import dataclass, field
from typing import List
from uuid import UUID

from mashumaro.jsonschema import build_json_schema


@dataclass
class User:
    id: UUID
    name: str = field(metadata={"description": "User name"})


print(build_json_schema(List[User]).to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "array",
    "items": {
        "type": "object",
        "title": "User",
        "properties": {
            "id": {
                "type": "string",
                "format": "uuid"
            },
            "name": {
                "type": "string",
                "description": "User name"
            }
        },
        "additionalProperties": false,
        "required": [
            "id",
            "name"
        ]
    }
}
```
</details>

Additional validation keywords ([see below](#json-schema-constraints))
can be added using annotations:

```python
from typing import Annotated, List
from mashumaro.jsonschema import build_json_schema
from mashumaro.jsonschema.annotations import Maximum, MaxItems

print(
    build_json_schema(
        Annotated[
            List[Annotated[int, Maximum(42)]],
            MaxItems(4)
        ]
    ).to_json()
)
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "array",
    "items": {
        "type": "integer",
        "maximum": 42
    },
    "maxItems": 4
}
```
</details>

The [`$schema`](https://json-schema.org/draft/2020-12/json-schema-core.html#name-the-schema-keyword)
keyword can be added by setting `with_dialect_uri` to True:

```python
print(build_json_schema(str, with_dialect_uri=True).to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "string"
}
```
</details>

By default, Draft 2022-12 dialect is being used, but you can change it to
another one by setting `dialect` parameter:

```python
from mashumaro.jsonschema import OPEN_API_3_1

print(
    build_json_schema(
        str, dialect=OPEN_API_3_1, with_dialect_uri=True
    ).to_json()
)
```

<details>
<summary>Click to show the result</summary>

```json
{
    "$schema": "https://spec.openapis.org/oas/3.1/dialect/base",
    "type": "string"
}
```
</details>

All dataclass JSON Schemas can or can not be placed in the
[definitions](https://json-schema.org/draft/2020-12/json-schema-core.html#name-schema-re-use-with-defs)
section, depending on the `all_refs` parameter, which default value comes
from a dialect used (`False` for Draft 2022-12, `True` for OpenAPI
Specification 3.1.1):

```python
print(build_json_schema(List[User], all_refs=True).to_json())
```
<details>
<summary>Click to show the result</summary>

```json
{
    "type": "array",
    "$defs": {
        "User": {
            "type": "object",
            "title": "User",
            "properties": {
                "id": {
                    "type": "string",
                    "format": "uuid"
                },
                "name": {
                    "type": "string"
                }
            },
            "additionalProperties": false,
            "required": [
                "id",
                "name"
            ]
        }
    },
    "items": {
        "$ref": "#/$defs/User"
    }
}
```
</details>

The definitions section can be omitted from the final document by setting
`with_definitions` parameter to `False`:

```python
print(
    build_json_schema(
        List[User], dialect=OPEN_API_3_1, with_definitions=False
    ).to_json()
)
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "array",
    "items": {
        "$ref": "#/components/schemas/User"
    }
}
```
</details>

Reference prefix can be changed by using `ref_prefix` parameter:

```python
print(
    build_json_schema(
        List[User],
        all_refs=True,
        with_definitions=False,
        ref_prefix="#/components/responses",
    ).to_json()
)
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "array",
    "items": {
        "$ref": "#/components/responses/User"
    }
}
```
</details>

The omitted definitions could be found later in the `Context` object that
you could have created and passed to the function, but it could be easier
to use `JSONSchemaBuilder` for that. For example, you might found it handy
to build OpenAPI Specification step by step passing your models to the builder
and get all the registered definitions later. This builder has reasonable
defaults but can be customized if necessary.

```python
from mashumaro.jsonschema import JSONSchemaBuilder, OPEN_API_3_1

builder = JSONSchemaBuilder(OPEN_API_3_1)

@dataclass
class User:
    id: UUID
    name: str

@dataclass
class Device:
    id: UUID
    model: str

print(builder.build(List[User]).to_json())
print(builder.build(List[Device]).to_json())
print(builder.get_definitions().to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "array",
    "items": {
        "$ref": "#/components/schemas/User"
    }
}
```
```json
{
    "type": "array",
    "items": {
        "$ref": "#/components/schemas/Device"
    }
}
```
```json
{
    "User": {
        "type": "object",
        "title": "User",
        "properties": {
            "id": {
                "type": "string",
                "format": "uuid"
            },
            "name": {
                "type": "string"
            }
        },
        "additionalProperties": false,
        "required": [
            "id",
            "name"
        ]
    },
    "Device": {
        "type": "object",
        "title": "Device",
        "properties": {
            "id": {
                "type": "string",
                "format": "uuid"
            },
            "model": {
                "type": "string"
            }
        },
        "additionalProperties": false,
        "required": [
            "id",
            "model"
        ]
    }
}
```
</details>

### JSON Schema constraints

Apart from required keywords, that are added automatically for certain data
types, you're free to use additional validation keywords.
They're presented by the corresponding classes in
[`mashumaro.jsonschema.annotations`](https://github.com/Fatal1ty/mashumaro/blob/master/mashumaro/jsonschema/annotations.py):

Number constraints:
* [`Minimum`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minimum)
* [`Maximum`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maximum)
* [`ExclusiveMinimum`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-exclusiveminimum)
* [`ExclusiveMaximum`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-exclusivemaximum)
* [`MultipleOf`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-multipleof)

String constraints:
* [`MinLength`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minlength)
* [`MaxLength`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxlength)
* [`Pattern`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-pattern)

Array constraints:
* [`MinItems`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minitems)
* [`MaxItems`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxitems)
* [`UniqueItems`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-uniqueitems)
* [`Contains`](https://json-schema.org/draft/2020-12/json-schema-core.html#name-contains)
* [`MinContains`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-mincontains)
* [`MaxContains`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxcontains)

Object constraints:
* [`MaxProperties`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxproperties)
* [`MinProperties`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minproperties)
* [`DependentRequired`](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-dependentrequired)

### JSON Schema plugins

If the built-in functionality doesn't meet your needs, you can customize the
JSON Schema generation or add support for additional types using plugins.
The [`mashumaro.jsonschema.plugins.BasePlugin`](https://github.com/Fatal1ty/mashumaro/blob/32179eac1927483f4da015f711b07c04e8b2b2b9/mashumaro/jsonschema/plugins.py#L9-L16)
class provides a `get_schema` method that you can override to implement custom
behavior.

The plugin system works by iterating through all registered plugins and calling
their `get_schema` methods. If a plugin's `get_schema` method raises a
`NotImplementedError` or returns `None`, it indicates that the plugin doesn't
provide the required functionality for that particular case.

You can apply multiple plugins sequentially, allowing each to modify the schema
in turn. This approach enables a step-by-step transformation of the schema,
with each plugin contributing its specific modifications.

Plugins can be registered using the `plugins` argument in either the
`build_json_schema` function or the `JSONSchemaBuilder` class.

The [`mashumaro.jsonschema.plugins`](https://github.com/Fatal1ty/mashumaro/blob/master/mashumaro/jsonschema/plugins.py)
module contains several built-in plugins. Currently, one of these plugins adds
descriptions to JSON schemas using docstrings from dataclasses:

```python
from dataclasses import dataclass

from mashumaro.jsonschema import build_json_schema
from mashumaro.jsonschema.plugins import DocstringDescriptionPlugin


@dataclass
class MyClass:
    """My class"""

    x: int


schema = build_json_schema(MyClass, plugins=[DocstringDescriptionPlugin()])
print(schema.to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "object",
    "title": "MyClass",
    "description": "My class",
    "properties": {
        "x": {
            "type": "integer"
        }
    },
    "additionalProperties": false,
    "required": [
        "x"
    ]
}
```
</details>

Creating your own custom plugin is straightforward. For instance, if you want
to add support for Pydantic models, you could write a plugin similar to the
following:

```python
from dataclasses import dataclass

from pydantic import BaseModel

from mashumaro.jsonschema import build_json_schema
from mashumaro.jsonschema.models import Context, JSONSchema
from mashumaro.jsonschema.plugins import BasePlugin
from mashumaro.jsonschema.schema import Instance


class PydanticSchemaPlugin(BasePlugin):
    def get_schema(
        self,
        instance: Instance,
        ctx: Context,
        schema: JSONSchema | None = None,
    ) -> JSONSchema | None:
        try:
            if issubclass(instance.type, BaseModel):
                pydantic_schema = instance.type.model_json_schema()
                return JSONSchema.from_dict(pydantic_schema)
        except TypeError:
            return None


class MyPydanticClass(BaseModel):
    x: int


@dataclass
class MyDataClass:
    y: MyPydanticClass


schema = build_json_schema(MyDataClass, plugins=[PydanticSchemaPlugin()])
print(schema.to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "object",
    "title": "MyDataClass",
    "properties": {
        "y": {
            "type": "object",
            "title": "MyPydanticClass",
            "properties": {
                "x": {
                    "type": "integer",
                    "title": "X"
                }
            },
            "required": [
                "x"
            ]
        }
    },
    "additionalProperties": false,
    "required": [
        "y"
    ]
}
```
</details>


### Extending JSON Schema

Using a `Config` class it is possible to override some parts of the schema.
Currently, you can do the following:
* override some field schemas using the "properties" key
* change `additionalProperties` using the "additionalProperties" key

```python
from dataclasses import dataclass
from mashumaro.jsonschema import build_json_schema

@dataclass
class FooBar:
    foo: str
    bar: int

    class Config:
        json_schema = {
            "properties": {
                "foo": {
                    "type": "string",
                    "description": "bar"
                }
            },
            "additionalProperties": True,
        }

print(build_json_schema(FooBar).to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "object",
    "title": "FooBar",
    "properties": {
        "foo": {
            "type": "string",
            "description": "bar"
        },
        "bar": {
            "type": "integer"
        }
    },
    "additionalProperties": true,
    "required": [
        "foo",
        "bar"
    ]
}
```
</details>

You can also change the "additionalProperties" key to a specific schema
by passing it a `JSONSchema` instance instead of a bool value.

### JSON Schema and custom serialization methods

Mashumaro provides different ways to override default serialization methods for
dataclass fields or specific data types. In order for these overrides to be
reflected in the schema, you need to make sure that the methods have
annotations of the return value type.

```python
from dataclasses import dataclass, field
from mashumaro.config import BaseConfig
from mashumaro.jsonschema import build_json_schema

def str_as_list(s: str) -> list[str]:
    return list(s)

def int_as_str(i: int) -> str:
    return str(i)

@dataclass
class FooBar:
    foo: str = field(metadata={"serialize": str_as_list})
    bar: int

    class Config(BaseConfig):
        serialization_strategy = {
            int: {
                "serialize": int_as_str
            }
        }

print(build_json_schema(FooBar).to_json())
```

<details>
<summary>Click to show the result</summary>

```json
{
    "type": "object",
    "title": "FooBar",
    "properties": {
        "foo": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "bar": {
            "type": "string"
        }
    },
    "additionalProperties": false,
    "required": [
        "foo",
        "bar"
    ]
}
```
</details>