code: def encode4js(obj):
      """Prepare an object for JSON encoding.
  
      It has special handling for many Python types, including:
      - pandas DataFrames and Series
      - NumPy ndarrays
      - complex numbers
  
      """
      if isinstance(obj, DataFrame):
          return dict(__class__="PDataFrame", value=obj.to_json())
      if isinstance(obj, Series):
          return dict(__class__="PSeries", value=obj.to_json())
      if isinstance(obj, uncertainties.core.AffineScalarFunc):
          return dict(
              __class__="UFloat", val=obj.nominal_value, err=obj.std_dev
          )
      if isinstance(obj, np.ndarray):
          if "complex" in obj.dtype.name:
              val = [(obj.real).tolist(), (obj.imag).tolist()]
          elif obj.dtype.name == "object":
              val = [encode4js(item) for item in obj]
          else:
              val = obj.flatten().tolist()
          return dict(
              __class__="NDArray",
              __shape__=obj.shape,
              __dtype__=obj.dtype.name,
              value=val,
          )
      if isinstance(obj, float):
          return float(obj)
      if isinstance(obj, int):
          return int(obj)
      if isinstance(obj, str):
          try:
              return str(obj)
          except UnicodeError:
              return obj
      if isinstance(obj, complex):
          return dict(__class__="Complex", value=(obj.real, obj.imag))
      if isinstance(obj, (tuple, list)):
          ctype = "List"
          if isinstance(obj, tuple):
              ctype = "Tuple"
          val = [encode4js(item) for item in obj]
          return dict(__class__=ctype, value=val)
      if isinstance(obj, dict):
          out = dict(__class__="Dict")
          for key, val in obj.items():
              out[encode4js(key)] = encode4js(val)
          return out
      if callable(obj):
          value = (
              str(b64encode(dill.dumps(obj)), "utf-8")
              if HAS_DILL
              else None
          )
          return dict(
              __class__="Callable",
              __name__=obj.__name__,
              pyversion=pyvers,
              value=value,
              importer=find_importer(obj),
          )
      return obj
  
file_path: lmfit-py/lmfit/jsonutils.py
imports: from base64 import b64decode, b64encode
  from io import StringIO
  import sys
  import warnings
  import numpy
  import uncertainties
label_desc: Use image node_id 3 for calling a global function with example usage: encode4js(obj) and returns: obj, dict, dict, dict, dict, float, int, dict, dict, out, dict, str, obj