def truncated_json_dumps(obj, max_length, key, ensure_ascii=False):
 orig = json.dumps(obj, ensure_ascii=ensure_ascii)
 diff = (len(orig) - max_length)
 if (diff < 0):
  return orig
 dupe = json.loads(orig)
 if (len(dupe[key]) < diff):
  raise TruncationException("Can't truncate enough to satisfy `max_length`.")
 dupe[key] = dupe[key][:(- diff)]
 return json.dumps(dupe, ensure_ascii=ensure_ascii)

def truncated_json_dumps ( obj , max_length , key , ensure_ascii = False ) : orig = json . dumps ( obj , ensure_ascii = ensure_ascii ) diff = ( len ( orig ) - max_length ) if ( diff < 0 ) : return orig dupe = json . loads ( orig ) if ( len ( dupe [ key ] ) < diff ) : raise TruncationException ( "Can't truncate enough to satisfy `max_length`." ) dupe [ key ] = dupe [ key ] [ : ( - diff ) ] return json . dumps ( dupe , ensure_ascii = ensure_ascii )
dump an object to json .