import json


def _serialize_to_json(shell_var):
  """Serializes the python variable into a JSON string.

  Args:
    shell_var: ipython shell python object.

  Returns:
    A JSON string.

  Raises:
    ValueError: When serializing a type other than str, dict, or list.
    JSONDecodeError: When unable to encode the variable into JSON.
  """
  if isinstance(shell_var, str):
    return shell_var
  elif isinstance(shell_var, dict) or isinstance(shell_var, list):
    return json.dumps(shell_var)
  else:
    raise ValueError("variable {} is not json decodable".format(shell_var))