def to_json(obj: Any, filename: str=None) -> Union[(str, None)]:
    if (filename is not None):
        with open(filename, 'w') as f:
            json.dump(obj, f, cls=JSONEncoder)
    else:
        return json.dumps(obj, cls=JSONEncoder)
def to_json(obj: Any, filename: str=None) -> Union[(str, None)]:
    if (filename is not None):
        with open(filename, 'w') as f:
            json.dump(obj, f, cls=JSONEncoder)
    else:
        return json.dumps(obj, cls=JSONEncoder)