Restore History from a JSON string. :param history_json: history data as JSON string (generated using to_json()) :return: History object :raises json.JSONDecodeError: if passed invalid JSON string :raises KeyError: if JSON is missing required elements :raises
(history_json: str)
| 349 | |
| 350 | @staticmethod |
| 351 | def from_json(history_json: str) -> "History": |
| 352 | """Restore History from a JSON string. |
| 353 | |
| 354 | :param history_json: history data as JSON string (generated using to_json()) |
| 355 | :return: History object |
| 356 | :raises json.JSONDecodeError: if passed invalid JSON string |
| 357 | :raises KeyError: if JSON is missing required elements |
| 358 | :raises ValueError: if history version in JSON isn't supported |
| 359 | """ |
| 360 | json_dict = json.loads(history_json) |
| 361 | version = json_dict[History._history_version_field] |
| 362 | if version != History._history_version: |
| 363 | raise ValueError( |
| 364 | f"Unsupported history file version: {version}. This application uses version {History._history_version}." |
| 365 | ) |
| 366 | |
| 367 | items = json_dict[History._history_items_field] |
| 368 | history = History() |
| 369 | for hi_dict in items: |
| 370 | history.append(HistoryItem.from_dict(hi_dict)) |
| 371 | |
| 372 | return history |