def safe_dumps(*args, **kwargs):
 try:
  dumped = json.dumps(allow_nan=False, *args, **kwargs)
 except ValueError:
  obj = swap_inf_nan(copy.deepcopy(args[0]))
  dumped = json.dumps(obj, allow_nan=False, **kwargs)
 if kwargs.get('escape_closing_tags', True):
  return dumped.replace('</', '<\\/')
 return dumped

def safe_dumps ( * args , ** kwargs ) : try : dumped = json . dumps ( allow_nan = False , * args , ** kwargs ) except ValueError : obj = swap_inf_nan ( copy . deepcopy ( args [ 0 ] ) ) dumped = json . dumps ( obj , allow_nan = False , ** kwargs ) if kwargs . get ( 'escape_closing_tags' , True ) : return dumped . replace ( '</' , '<\\/' ) return dumped
this is a wrapper around dumps that encodes infinity and nan values .