code: def _validate_and_cast(cls, json_value: Any, expected_type: Any):
      type_cls = (
          typing.get_origin(expected_type) or expected_type
      )  # typing.get_origin() returns None for non-generic types like `Any` and `int`
      type_args = typing.get_args(expected_type)
      if type_cls is typing.Any:
          return json_value
      if type_cls is float and isinstance(json_value, (int, float)):
          return float(json_value)
      elif type_cls in (int, str, bool) and isinstance(
          json_value, type_cls
      ):
          return json_value
      elif type_cls is None and json_value is None:
          return None
      elif type_cls is typing.Union:
          for arg in type_args:
              try:
                  return cls._validate_and_cast(json_value, arg)
              except TypeError:
                  continue
      elif type_cls is dict and isinstance(json_value, dict):
          if not type_args:
              type_args = (Any, Any)
          type_key, type_value = type_args
          return {
              cls._validate_and_cast(
                  key, type_key
              ): cls._validate_and_cast(value, type_value)
              for key, value in json_value.items()
          }
      elif type_cls is list and isinstance(json_value, list):
          if not type_args:
              type_args = [Any]
          return [
              cls._validate_and_cast(x, type_args[0])
              for x in json_value
          ]
      elif (
          type_cls is tuple
          and isinstance(json_value, list)
          and (type_args is None or len(type_args) == len(json_value))
      ):
          if type_args is None:
              type_args = [Any] * len(json_value)
          return tuple(
              type_cls(
                  cls._validate_and_cast(item, type_arg)
                  for item, type_arg in zip(json_value, type_args)
              )
          )
      raise TypeError(
          f"Cannot cast {json_value} with type {type(json_value)} to {expected_type}"
      )
  
file_path: ml-cvnets/options/parse_args.py
imports: import json
  import typing
  from typing import Any
label_desc: Use image node_id 2 for calling the JsonValidator obj's underlying member method code with example usage: obj._validate_and_cast(cls, json_value, expected_type) and returns: json_value, float, json_value, None, unknown, cls, unknown, tuple