Java Code Examples for org.bson.codecs.configuration.CodecRegistry#get()

The following examples show how to use org.bson.codecs.configuration.CodecRegistry#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityDecoder.java    From morphia with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected Codec<T> getCodecFromDocument(final BsonReader reader, final boolean useDiscriminator, final String discriminatorKey,
                                        final CodecRegistry registry, final DiscriminatorLookup discriminatorLookup,
                                        final Codec<T> defaultCodec) {
    Codec<T> codec = null;
    if (useDiscriminator) {
        BsonReaderMark mark = reader.getMark();
        try {
            reader.readStartDocument();
            while (codec == null && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
                if (discriminatorKey.equals(reader.readName())) {
                    codec = (Codec<T>) registry.get(discriminatorLookup.lookup(reader.readString()));
                } else {
                    reader.skipValue();
                }
            }
        } catch (Exception e) {
            throw new CodecConfigurationException(String.format("Failed to decode '%s'. Decoding errored with: %s",
                morphiaCodec.getEntityModel().getName(), e.getMessage()), e);
        } finally {
            mark.reset();
        }
    }
    return codec != null ? codec : defaultCodec;
}
 
Example 2
Source File: JacksonCodec.java    From EDDI with Apache License 2.0 5 votes vote down vote up
public JacksonCodec(ObjectMapper bsonObjectMapper,
                    CodecRegistry codecRegistry,
                    Class<T> type) {
    this.bsonObjectMapper = bsonObjectMapper;
    this.rawBsonDocumentCodec = codecRegistry.get(RawBsonDocument.class);
    this.type = type;
}
 
Example 3
Source File: JacksonCodec.java    From mongo-jackson-codec with Apache License 2.0 5 votes vote down vote up
public JacksonCodec(ObjectMapper bsonObjectMapper,
                    CodecRegistry codecRegistry,
                    Class<T> type) {
    this.bsonObjectMapper = bsonObjectMapper;
    this.rawBsonDocumentCodec = codecRegistry.get(RawBsonDocument.class);
    this.type = type;
}
 
Example 4
Source File: GsonCodecs.java    From immutables with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
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;
      }

    }
  };
}
 
Example 5
Source File: JacksonCodecs.java    From immutables with Apache License 2.0 5 votes vote down vote up
public static Serializers serializers(final CodecRegistry registry) {
  return new Serializers.Base() {
    @Override
    public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
      try {
        Codec<?> codec = registry.get(type.getRawClass());
        return serializer(codec);
      } catch (CodecConfigurationException e) {
        return null;
      }
    }
  };
}
 
Example 6
Source File: JacksonCodecs.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static <T> Codec<T> findCodecOrNull(CodecRegistry registry, Class<T> type) {
  try {
    return registry.get(type);
  } catch (CodecConfigurationException e) {
    return null;
  }
}
 
Example 7
Source File: UpdateDocument.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) {
    DocumentWriter writer = new DocumentWriter();

    MorphiaCodec codec = (MorphiaCodec) codecRegistry.get(entity.getClass());
    codec.encode(writer, entity, EncoderContext.builder().build());

    Document document = writer.getDocument();
    document.remove("_id");
    MappedField versionField = codec.getMappedClass().getVersionField();
    if (versionField != null) {
        document.remove(versionField.getMappedFieldName());
    }
    return document.toBsonDocument(Document.class, codecRegistry);
}
 
Example 8
Source File: JacksonCodec.java    From clouditor with Apache License 2.0 4 votes vote down vote up
JacksonCodec(ObjectMapper mapper, Class<T> clazz, CodecRegistry registry) {
  this.mapper = mapper;
  this.clazz = clazz;
  this.codec = registry.get(RawBsonDocument.class);
}
 
Example 9
Source File: Stream.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
public Stream(final EventStream eventStream,
              final Class<T> resultClass,
              final CodecRegistry codecRegistry) {
  this.eventStream = eventStream;
  this.decoder = codecRegistry.get(resultClass);
}
 
Example 10
Source File: BsonUtils.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
public static <T> Codec<T> getCodec(
    final CodecRegistry codecRegistry,
    final Class<T> documentClass
) {
  return codecRegistry.get(documentClass);
}
 
Example 11
Source File: JsonObjectBsonAdapter.java    From vertx-mongo-client with Apache License 2.0 4 votes vote down vote up
@Override
public <C> BsonDocument toBsonDocument(Class<C> documentClass, CodecRegistry codecRegistry) {
  return new BsonDocumentWrapper<>(obj, codecRegistry.get(JsonObject.class));
}