code: def _state_to_json(state: Dict[str, Any]) -> Dict[str, Any]:
      """Utility function to make sure that state dict is json serializable."""
      from lightning.app.storage.path import Path
      from lightning.app.storage.payload import _BasePayload
  
      state_paths_cleaned = apply_to_collection(
          state,
          dtype=(Path, _BasePayload),
          function=lambda x: x.to_dict(),
      )
      return apply_to_collection(
          state_paths_cleaned,
          dtype=type(NotPresent),
          function=lambda x: None,
      )
  
imports: import os
  from contextlib import contextmanager
  from typing import TYPE_CHECKING, Any, Dict, Generator, Optional
  from deepdiff.helper import NotPresent
  from lightning_utilities.core.apply_func import apply_to_collection
  from lightning.app.utilities.app_helpers import is_overridden
  from lightning.app.utilities.enum import ComponentContext
  from lightning.app.utilities.packaging.cloud_compute import CloudCompute
  from lightning.app.utilities.tree import breadth_first
label_desc: Use image node_id 3 for calling a global function with example usage: _state_to_json(state) and returns: apply_to_collection