code: def json_dumps(obj):
      
      try:
          return json.dumps(obj, indent=2, sort_keys=True, allow_nan=False)
      except ValueError:
          pass
      # we don't want to call do_map on the original object since it can
      # contain objects that need to be converted for JSON. after reading
      # in the created JSON we get a limited set of possible types we
      # can encounter
      json_str = json.dumps(obj, indent=2, sort_keys=True, allow_nan=True)
      json_obj = json.loads(json_str)
  
      def do_map(obj):
          if obj is None:
              return None
          if isinstance(obj, basestring):
              return obj
          if isinstance(obj, dict):
              res = {}
              for (key, value) in obj.items():
                  res[key] = do_map(value)
              return res
          if isinstance(obj, collections.Iterable):
              res = []
              for el in obj:
                  res.append(do_map(el))
              return res
          # diverging numbers need to be passed as strings otherwise it
          # will throw a parsing error on the ECMAscript consumer side
          if math.isnan(obj):
              return "NaN"
          if math.isinf(obj):
              return "Infinity" if obj > 0 else "-Infinity"
          return obj
  
      return json.dumps(
          do_map(json_obj), indent=2, sort_keys=True, allow_nan=False)
docs: A safe JSON dump function that provides correct diverging numbers for a
         ECMAscript consumer.