def message_to_json()

in python/moz/l10n/message/to_json.py [0:0]


def message_to_json(msg: Message) -> list[Any] | dict[str, Any]:
    """
    Represent a Message as a JSON-serializable value.

    The JSON Schema of the output is provided as [schema.json](./schema.json).
    """
    json_declarations = {
        name: _expression_to_json(expr) for name, expr in msg.declarations.items()
    }
    if isinstance(msg, PatternMessage):
        if not json_declarations:
            return _pattern_to_json(msg.pattern)
        return {
            "decl": json_declarations,
            "msg": _pattern_to_json(msg.pattern),
        }
    else:
        assert isinstance(msg, SelectMessage)
        return {
            "decl": json_declarations,
            "sel": [sel.name for sel in msg.selectors],
            "alt": [
                {
                    "keys": [
                        key if isinstance(key, str) else {"*": key.value or ""}
                        for key in keys
                    ],
                    "pat": _pattern_to_json(pattern),
                }
                for keys, pattern in msg.variants.items()
            ],
        }