Load a block from the JSON dictionary. :param data: a dictionary (not list) :return: The new Block object
(data: dict)
| 400 | |
| 401 | @staticmethod |
| 402 | def from_json(data: dict) -> Block: |
| 403 | """ |
| 404 | Load a block from the JSON dictionary. |
| 405 | :param data: a dictionary (not list) |
| 406 | :return: The new Block object |
| 407 | """ |
| 408 | _opcode = data["opcode"] |
| 409 | |
| 410 | _x, _y = data.get("x"), data.get("y") |
| 411 | |
| 412 | _next_id = data.get("next") |
| 413 | _parent_id = data.get("parent") |
| 414 | |
| 415 | _shadow = data.get("shadow", False) |
| 416 | _top_level = data.get("topLevel", _parent_id is None) |
| 417 | |
| 418 | _inputs = {} |
| 419 | for _input_code, _input_data in data.get("inputs", {}).items(): |
| 420 | _inputs[_input_code] = inputs.Input.from_json(_input_data) |
| 421 | |
| 422 | _fields = {} |
| 423 | for _field_code, _field_data in data.get("fields", {}).items(): |
| 424 | _fields[_field_code] = field.Field.from_json(_field_data) |
| 425 | |
| 426 | if "mutation" in data: |
| 427 | _mutation = mutation.Mutation.from_json(data["mutation"]) |
| 428 | else: |
| 429 | _mutation = None |
| 430 | |
| 431 | return Block( |
| 432 | _opcode, _shadow, _top_level, _mutation, _fields, _inputs, _x, _y, _next_id=_next_id, _parent_id=_parent_id |
| 433 | ) |
| 434 | |
| 435 | def to_json(self) -> dict: |
| 436 | """ |