def write_gtfsr(feed, path, *, to_json=False):
    """
    Given a GTFSR feed (FeedMessage instance), write it to the given
    file path (string or Path object).
    If ``to_json``, then save the file as JSON;
    otherwise save it as Protocol Buffer.
    """
    path = Path(path)

    if to_json:
        with path.open('w') as tgt:
            tgt.write(json_format.MessageToJson(feed))
    else:
        with path.open('wb') as tgt:
            tgt.write(feed.SerializeToString())