Add "from_json" and "to_json" methods.
(self)
| 70 | class DeriveMixins(object): |
| 71 | '''Helpers for easily bindings #[derive]'d methods.''' |
| 72 | def serialize(self): |
| 73 | '''Add "from_json" and "to_json" methods.''' |
| 74 | |
| 75 | args = [Var(self.program.strref.type, "s")] |
| 76 | type = self.type.result() |
| 77 | self.methods.append(Method( |
| 78 | type, self.c_name, "from_json", args, |
| 79 | make_safe_call(type, f'serde_json::from_str::<{self.module}::{self.name}>', args), |
| 80 | docs=f'Deserialize a {self.type.to_python()} from a JSON string', |
| 81 | static=True |
| 82 | )) |
| 83 | |
| 84 | args = [Var(self.type.mut_ref(), 'this')] |
| 85 | type = self.program.string.type.result() |
| 86 | self.methods.append(Method(type, self.c_name, "to_json", args, |
| 87 | make_safe_call(type, 'serde_json::to_string', args), docs=f'Serialize a {self.type.to_python()} to a JSON string' |
| 88 | )) |
| 89 | |
| 90 | return self |
| 91 | |
| 92 | def debug(self): |
| 93 | args = [Var(self.type.mut_ref(), 'this')] |