-
Notifications
You must be signed in to change notification settings - Fork 40
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 #280 from yukinarit/add-init-class-var-examples
test: Add InitVar and ClassVar examples
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from dataclasses import dataclass | ||
from typing import ClassVar, Optional | ||
|
||
from serde import serde | ||
from serde.json import from_json, to_json | ||
|
||
|
||
@serde | ||
@dataclass | ||
class Foo: | ||
i: int | ||
j: Optional[int] = None | ||
k: ClassVar[int] = 100 | ||
|
||
|
||
def main(): | ||
f = Foo(i=10, j=20) | ||
print(f"Into Json: {to_json(f)}") | ||
|
||
s = '{"i": 10, "j": 20}' | ||
print(f"From Json: {from_json(Foo, s)}") | ||
|
||
print(f"Class variable: Foo.k={f.k}") | ||
|
||
|
||
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,25 @@ | ||
from dataclasses import InitVar, dataclass | ||
from typing import Optional | ||
|
||
from serde import serde | ||
from serde.json import from_json, to_json | ||
|
||
|
||
@serde | ||
@dataclass | ||
class Foo: | ||
i: int | ||
j: Optional[int] = None | ||
k: InitVar[Optional[int]] = None | ||
|
||
|
||
def main(): | ||
f = Foo(i=10, j=20) | ||
print(f"Into Json: {to_json(f)}") | ||
|
||
s = '{"i": 10, "j": 20}' | ||
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