import math


def WrapSpecialFloats(obj):
  """Replaces all instances of Infinity/-Infinity/NaN with strings."""
  if obj == float('inf'):
    return 'Infinity'
  elif obj == float('-inf'):
    return '-Infinity'
  elif isinstance(obj, float) and math.isnan(obj):
    return 'NaN'
  elif isinstance(obj, list) or isinstance(obj, tuple):
    return list(map(WrapSpecialFloats, obj))
  elif isinstance(obj, dict):
    return {
        WrapSpecialFloats(k): WrapSpecialFloats(v)
        for k, v in obj.items()
    }
  else:
    return obj