import json


def printjson(toprint, exception_conversions={}):
    """print anything in json format. Can override printing for custom
    types using a dictionary of types to lambda conversion functions.
    Examples:
        printjson(data)
        printjson(data,{sp.DenseSparseMatrix: lambda x: f"matrix  = {x}" })
    """

    def serialize(x):
        conversion = exception_conversions.get(type(x))
        if conversion is None:
            try:
                return json.dumps(x)
            except Exception:
                return str(x)
        else:
            return conversion(x)

    print(json.dumps(toprint, indent=4, default=serialize))