class_docstr: A WBValue stored as a file outside JSON that can be rendered in a media panel.
  
  If necessary, we move or copy the file into the Run's media directory so that it
  gets uploaded.
class_docstr_tok: ["A","WBValue","stored","as","a","file","outside","JSON","that","can","be","rendered","in","a","media","panel",".","If","necessary",",","we","move","or","copy","the","file","into","the","Run","'s","media","directory","so","that","it","gets","uploaded","."]
docstr: Serialize the object into a JSON blob.
  
  Uses run or artifact to store additional data. If `run_or_artifact` is a
  wandb.Run then `self.bind_to_run()` must have been previously been called.
  
  Args:
      run_or_artifact (wandb.Run | wandb.Artifact): the Run or Artifact for which
          this object should be generating JSON for - this is useful to store
          additional data if needed.
  
  Returns:
      dict: JSON representation
docstr_tok: ["Serialize","the","object","into","a","JSON","blob",".","Uses","run","or","artifact","to","store","additional","data",".","If","`","run_or_artifact","`","is","a","wandb.Run","then","`","self.bind_to_run","(",")","`","must","have","been","previously","been","called",".","Args",":","run_or_artifact","(","wandb.Run","|","wandb.Artifact",")",":","the","Run","or","Artifact","for","which","this","object","should","be","generating","JSON","for","-","this","is","useful","to","store","additional","data","if","needed",".","Returns",":","dict",":","JSON","representation"]
code: def to_json(self, run: Union["LocalRun", "Artifact"]) -> dict:
      """Serialize the object into a JSON blob.
  
      Uses run or artifact to store additional data. If `run_or_artifact` is a
      wandb.Run then `self.bind_to_run()` must have been previously been called.
  
      Args:
          run_or_artifact (wandb.Run | wandb.Artifact): the Run or Artifact for which
              this object should be generating JSON for - this is useful to store
              additional data if needed.
  
      Returns:
          dict: JSON representation
      """
      # NOTE: uses of Audio in this class are a temporary hack -- when Ref support moves up
      # into Media itself we should get rid of them
      from wandb import Image
      from wandb.data_types import Audio
  
      json_obj = {}
      if isinstance(run, wandb.wandb_sdk.wandb_run.Run):
          json_obj.update(
              {
                  "_type": "file",  # TODO(adrian): This isn't (yet) a real media type we support on the frontend.
                  "sha256": self._sha256,
                  "size": self._size,
              }
          )
          artifact_entry_url = self._get_artifact_entry_ref_url()
          if artifact_entry_url is not None:
              json_obj["artifact_path"] = artifact_entry_url
          artifact_entry_latest_url = (
              self._get_artifact_entry_latest_ref_url()
          )
          if artifact_entry_latest_url is not None:
              json_obj[
                  "_latest_artifact_path"
              ] = artifact_entry_latest_url
  
          if artifact_entry_url is None or self.is_bound():
              assert (
                  self.is_bound()
              ), "Value of type {} must be bound to a run with bind_to_run() before being serialized to JSON.".format(
                  type(self).__name__
              )
  
              assert (
                  self._run is run
              ), "We don't support referring to media files across runs."
  
              # The following two assertions are guaranteed to pass
              # by definition is_bound, but are needed for
              # mypy to understand that these are strings below.
              assert isinstance(self._path, str)
              json_obj["path"] = LogicalPath(
                  os.path.relpath(self._path, self._run.dir)
              )
  
      elif isinstance(run, wandb.Artifact):
          if self.file_is_set():
              # The following two assertions are guaranteed to pass
              # by definition of the call above, but are needed for
              # mypy to understand that these are strings below.
              assert isinstance(self._path, str)
              assert isinstance(self._sha256, str)
              artifact = run  # Checks if the concrete image has already been added to this artifact
              name = artifact.get_added_local_path_name(self._path)
              if name is None:
                  if self._is_tmp:
                      name = os.path.join(
                          self.get_media_subdir(),
                          os.path.basename(self._path),
                      )
                  else:
                      # If the files is not temporary, include the first 8 characters of the file's SHA256 to
                      # avoid name collisions. This way, if there are two images `dir1/img.png` and `dir2/img.png`
                      # we end up with a unique path for each.
                      name = os.path.join(
                          self.get_media_subdir(),
                          self._sha256[:20],
                          os.path.basename(self._path),
                      )
  
                  # if not, check to see if there is a source artifact for this object
                  if (
                      self._artifact_source
                      is not None
                      # and self._artifact_source.artifact != artifact
                  ):
                      default_root = (
                          self._artifact_source.artifact._default_root()
                      )
                      # if there is, get the name of the entry (this might make sense to move to a helper off artifact)
                      if self._path.startswith(default_root):
                          name = self._path[len(default_root) :]
                          name = name.lstrip(os.sep)
  
                      # Add this image as a reference
                      path = self._artifact_source.artifact.get_entry(
                          name
                      )
                      artifact.add_reference(path.ref_url(), name=name)
                  elif (
                      isinstance(self, Audio) or isinstance(self, Image)
                  ) and self.path_is_reference(self._path):
                      artifact.add_reference(self._path, name=name)
                  else:
                      entry = artifact.add_file(
                          self._path, name=name, is_tmp=self._is_tmp
                      )
                      name = entry.path
  
              json_obj["path"] = name
              json_obj["sha256"] = self._sha256
          json_obj["_type"] = self._log_type
      return json_obj
  
imports: import hashlib
  import os
  import platform
  import re
  import shutil
  from typing import TYPE_CHECKING, Optional, Sequence, Type, Union, cast
  import wandb
  from wandb import util
  from wandb._globals import _datatypes_callback
  from wandb.sdk.lib import filesystem
  from wandb.sdk.lib.paths import LogicalPath
  from .wb_value import WBValue
label_desc: Use image node_id 8 for calling the Media obj's underlying member method code with example usage: obj.to_json(run) and returns: json_obj