import io
import json


def read_json_object(json_filename, encoding="utf-8"):
  """Reads and returns a single JSON object from the specified text file,
  raising an error if the file cannot be parsed or if it does not contain
  exactly one JSON object as a dictionary."""

  # Strip comments while keeping line and column numbers.
  stripped_str = ""
  with io.open(json_filename, "r", encoding=encoding) as f_in:
    for line in f_in:
      line = line.rstrip()
      comment_pos = line.find("//")
      if comment_pos > -1:
        line = line[:comment_pos]
      stripped_str += line + "\n"

  obj = json.loads(stripped_str)

  if not isinstance(obj, dict):
    raise ValueError("File %s contains an invalid JSON object." % json_filename)

  return obj