Last change
on this file since 958 was 141, checked in by wouter, 4 years ago |
#60 change packson3 to pyson to avoid suggesting we're implementing jackson
|
File size:
1.7 KB
|
Line | |
---|
1 | from pyson.JsonSubTypes import JsonSubTypes
|
---|
2 | from pyson.JsonTypeInfo import JsonTypeInfo
|
---|
3 | from pyson.JsonTypeInfo import Id,As
|
---|
4 |
|
---|
5 | import unittest
|
---|
6 |
|
---|
7 | class JsonSubTypesTest(unittest.TestCase):
|
---|
8 | def test1(self):
|
---|
9 | @JsonSubTypes(["first.sub.class","subclss2"])
|
---|
10 | class superclass:
|
---|
11 | def print(self, *args):
|
---|
12 | for arg in args:
|
---|
13 | print(arg)
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | class derivedclass(superclass):
|
---|
18 | pass
|
---|
19 |
|
---|
20 | print(type(superclass))
|
---|
21 | print(superclass.__jsonsubtypes__)
|
---|
22 |
|
---|
23 | pr = superclass()
|
---|
24 | pr.print(1, 2, 3)
|
---|
25 | print(type(pr))
|
---|
26 | print(pr.__jsonsubtypes__)
|
---|
27 |
|
---|
28 | print("checking __jsonsubtypes__ function")
|
---|
29 | # WHY is self not added in this call???
|
---|
30 | print(superclass.__jsonsubtypes__)
|
---|
31 |
|
---|
32 | print(derivedclass.__jsonsubtypes__)
|
---|
33 |
|
---|
34 | class super2nojason:
|
---|
35 | pass
|
---|
36 |
|
---|
37 | # even with mixedclass, we inherit the annotation
|
---|
38 | class mixedclass(superclass, super2nojason):
|
---|
39 | pass
|
---|
40 |
|
---|
41 | print(mixedclass.__jsonsubtypes__)
|
---|
42 |
|
---|
43 |
|
---|
44 | @JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
|
---|
45 | @JsonSubTypes(["Cat"])
|
---|
46 | class Animal:
|
---|
47 | def __init__(self, age:int):
|
---|
48 | self._age=age
|
---|
49 | def getage(self):
|
---|
50 | return self_age
|
---|
51 |
|
---|
52 |
|
---|
53 | class Cat(Animal):
|
---|
54 | def __init__(self, age:int, color:str):
|
---|
55 | super().__init__(age)
|
---|
56 | self._color=color
|
---|
57 | def getcolor(self):
|
---|
58 | return self._color
|
---|
59 |
|
---|
60 | cat=Cat(1,"brown")
|
---|
61 | print(cat.__jsonsubtypes__)
|
---|
62 | print(str(cat.__jsontypeinfo__))
|
---|
63 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.