(json, typ)
| 26 | |
| 27 | |
| 28 | def dejsonize(json, typ): |
| 29 | if isinstance(typ, str) and typ[:4] == 'uint': |
| 30 | return json |
| 31 | elif typ == 'bool': |
| 32 | assert json in (True, False) |
| 33 | return json |
| 34 | elif isinstance(typ, list): |
| 35 | return [dejsonize(element, typ[0]) for element in json] |
| 36 | elif isinstance(typ, str) and typ[:4] == 'byte': |
| 37 | return bytes.fromhex(json[2:]) |
| 38 | elif hasattr(typ, 'fields'): |
| 39 | temp = {} |
| 40 | for field, subtype in typ.fields.items(): |
| 41 | temp[field] = dejsonize(json[field], subtype) |
| 42 | if field + "_hash_tree_root" in json: |
| 43 | assert(json[field + "_hash_tree_root"][2:] == |
| 44 | hash_tree_root(temp[field], subtype).hex()) |
| 45 | ret = typ(**temp) |
| 46 | if "hash_tree_root" in json: |
| 47 | assert(json["hash_tree_root"][2:] == |
| 48 | hash_tree_root(ret, typ).hex()) |
| 49 | return ret |
| 50 | else: |
| 51 | print(json, typ) |
| 52 | raise Exception("Type not recognized") |
no test coverage detected