code: def to_json(self) -> dict[str, Any]:
      json: dict[str, Any] = {}
  
      if self.docid is not None:
          json["docid"] = self.docid
      else:
          json["token"] = self.token
  
      if self.elementid is not None:
          json["elementid"] = self.elementid
  
      if self.roots:
          json["roots"] = self.roots.to_json()
          json["root_ids"] = [root.id for root in self.roots]
  
      if self.use_for_title is not None:
          json["use_for_title"] = self.use_for_title
  
      return json
  
imports: from __future__ import annotations
  import logging
  import re
  from contextlib import contextmanager
  from typing import TYPE_CHECKING, Any, Iterator, Sequence
  from weakref import WeakKeyDictionary
  from ..core.types import ID
  from ..document.document import Document
  from ..model import Model, collect_models
  from ..settings import settings
  from ..themes.theme import Theme
  from ..util.dataclasses import dataclass, field
  from ..util.serialization import make_globally_unique_css_safe_id, make_globally_unique_id
label_desc: Use image node_id 2 for calling the RenderItem obj's underlying member method code with example usage: obj.to_json() and returns: json