code: public static TypeAdapterFactory delegatingTypeAdapterFactory(final CodecRegistry registry) {
      Preconditions.checkNotNull(registry, "registry");
      return new TypeAdapterFactory() {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
          boolean hasAdapter;
          try {
            TypeAdapter<T> adapter = gson.getDelegateAdapter(this, type);
            hasAdapter = !isReflectiveTypeAdapter(adapter);
          } catch (IllegalArgumentException e) {
            hasAdapter = false;
          }
  
          if (hasAdapter) {
            return null;
          }
  
          try {
            @SuppressWarnings("unchecked")
            Codec<T> codec = (Codec<T>) registry.get(type.getRawType());
            return typeAdapterFromCodec(codec);
          } catch (CodecConfigurationException e1) {
            return null;
          }
  
        }
      };
    }
docs: Gson Factory which gives preference to existing adapters from {@code gson} instance. However,
  if type is not supported it will query {@link CodecRegistry} to create one (if possible).
  
  <p>This allows supporting Bson types by Gson natively (eg. for {@link org.bson.types.ObjectId}).
  
  @param registry existing registry which will be used if type is unknown to {@code gson}.
  @return factory which delegates to {@code registry} for unknown types.