21 | | A complex example showing many things at once (I did not have time to write out many simpler examples) |
| 21 | == Examples |
| 22 | |
| 23 | See [source:packson3/test/ObjectMapperTest.py] for many examples. |
| 24 | |
| 25 | A simple example, deserializng a dict with objects |
| 26 | {{{ |
| 27 | from packson3.ObjectMapper import ObjectMapper |
| 28 | from packson3.JsonTypeInfo import JsonTypeInfo |
| 29 | from packson3.JsonTypeInfo import Id,As |
| 30 | |
| 31 | @JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT) |
| 32 | class Simple: |
| 33 | def __init__(self, a:int): |
| 34 | self._a=a |
| 35 | def geta(self)->int: |
| 36 | return self._a |
| 37 | def __eq__(self, other): |
| 38 | return isinstance(other, self.__class__) and \ |
| 39 | self._a==other._a |
| 40 | def __str__(self): |
| 41 | return self._name+","+str(self._a) |
| 42 | |
| 43 | packson3=ObjectMapper() |
| 44 | objson = { 'a':{"Simple":{'a':1}},'c':{"Simple":{'a':3}}} |
| 45 | obj=packson3.parse(objson, Dict[str,Simple]) |
| 46 | }}} |
| 47 | |
| 48 | |
| 49 | A complex example showing many things at once |