Skip to content

declearn.communication.messaging.parse_message_from_string

Instantiate a Message from a JSON-serialized string.

Source code in declearn/communication/messaging/_messages.py
274
275
276
277
278
279
280
281
282
283
284
285
def parse_message_from_string(
    string: str,
) -> Message:
    """Instantiate a Message from a JSON-serialized string."""
    data = json.loads(string, object_hook=json_unpack)
    if "typekey" not in data:
        raise KeyError("Missing required 'typekey'")
    typekey = data.pop("typekey")
    cls = MESSAGE_CLASSES.get(typekey)
    if cls is None:
        raise KeyError(f"No Message matches typekey '{typekey}'.")
    return cls.from_kwargs(**data)