-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from yukinarit/defaultdict
feat: Support DefaultDict
- Loading branch information
Showing
8 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from collections import defaultdict | ||
from dataclasses import dataclass | ||
from typing import DefaultDict, List, Optional | ||
|
||
from serde import serde | ||
from serde.json import from_json, to_json | ||
|
||
|
||
@serde | ||
@dataclass | ||
class Bar: | ||
v: Optional[int] = None | ||
|
||
|
||
@serde | ||
@dataclass | ||
class Foo: | ||
a: DefaultDict[str, List[int]] | ||
b: DefaultDict[str, int] | ||
c: DefaultDict[str, Bar] | ||
|
||
|
||
def main(): | ||
a = defaultdict(list) | ||
a["a"].append(1) | ||
b = defaultdict(int) | ||
b["b"] | ||
c = defaultdict(Bar) | ||
c["c"].v = 10 | ||
|
||
f = Foo(a, b, c) | ||
print(f"Into Json: {to_json(f)}") | ||
|
||
s = '{"a": {"a": [1, 2]}, "b": {"b": 10}, "c": {"c": {}}}' | ||
print(f"From Json: {from_json(Foo, s)}") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from dataclasses import dataclass | ||
from typing import FrozenSet | ||
|
||
from serde import serde | ||
from serde.json import from_json, to_json | ||
|
||
|
||
@serde | ||
@dataclass | ||
class Foo: | ||
i: FrozenSet[int] | ||
|
||
|
||
def main(): | ||
f = Foo(i={1, 2}) | ||
print(f"Into Json: {to_json(f)}") | ||
|
||
s = '{"i": [1, 2]}' | ||
print(f"From Json: {from_json(Foo, s)}") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters