import json


def ConvertToHtmlString(trace_result):
  """Convert a trace result to the format to be output into HTML.

  If the trace result is a dictionary or list, JSON-encode it.
  If the trace result is a string, leave it unchanged.
  """
  if isinstance(trace_result, dict) or isinstance(trace_result, list):
    return json.dumps(trace_result)
  elif isinstance(trace_result, str):
    return trace_result
  else:
    raise ValueError('Invalid trace result format for HTML output')