code: def is_json_compatible(
      typehint: Type,  # pylint: disable=g-bare-generic
  ) -> bool:
      """Check if a type hint represents a JSON-compatible type.
  
      Currently, 'JSON-compatible' can be the following two cases:
        1. A type conforms with T, where T is defined
        as `X = Union['T', int, float, str, bool, NoneType]`,
        `T = Union[List['X'], Dict[str, 'X']]`, It can be Optional. ForwardRef is
        not allowed.
        2. Use `Any` to indicate any type conforms with case 1. Note Any is only
        allowed with Dict or List. A standalone `Any` is invalid.
  
      Args:
        typehint: The typehint to check.
      Returns:
        True if typehint is a JSON-compatible type.
      """
  
      def check(typehint: Any, not_primitive: bool = True) -> bool:
          origin = _convert_typing_to_builtin(typehint)
          args = getattr(typehint, "__args__", None)
          if origin is dict or origin is list or origin is Union:
              # Starting from Python 3.9 Dict won't have default args (~KT, ~VT)
              # and List won't have default args (~T).
              if not args:
                  return False
              elif origin is dict and args[0] is not str:
                  return False
              elif origin is dict and args[0] is str:
                  return check(typehint=args[1], not_primitive=False)
              # Handle top level optional.
              elif origin is Union and not_primitive:
                  return all(
                      [
                          arg is type(None)
                          or check(typehint=arg, not_primitive=True)
                          for arg in args
                      ]
                  )
              else:
                  return all(
                      [
                          check(typehint=arg, not_primitive=False)
                          for arg in args
                      ]
                  )
          else:
              return (
                  not not_primitive
                  and origin in _JSON_COMPATIBLE_PRIMITIVES
              )
  
      return check(typehint, not_primitive=True)
  
imports: from typing import Any, Type, Union, get_args, get_origin
label_desc: Use image node_id 2 for calling a global function with example usage: is_json_compatible(typehint) and returns: check, unknown, False, False, check, all, all