from typing import Union


def complex_decode(dictionary: dict) -> Union[complex, dict]:
    """Restore a `complex` object from a json dictionary.

    Use this as the optional `object_hook` argument to `json.load` or
    `json.loads` to restore a `complex` object."""
    if tuple(dictionary.keys()) == ('complex', 'real', 'imag'):
        return complex(dictionary['real'], dictionary['imag'])
    return dictionary