<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_sync_limit(self, limit: int) -> Optional[int]: """ Sets the events limit per room for sync and return previous limit """

        try:
            prev_limit = json.loads(self.sync_filter)['room']['timeline']['limit']
        except (json.JSONDecodeError, KeyError):
            prev_limit = None
        self.sync_filter = json.dumps({'room': {'timeline': {'limit': limit}}})
        return prev_limit