Search in sources :

Example 16 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class MongoCollectionTest method shouldBeAbleToQueryTypedCollectionAndMapResultsIntoTypedLists.

@Test
public void shouldBeAbleToQueryTypedCollectionAndMapResultsIntoTypedLists() {
    // given
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new ConcreteCodecProvider()));
    MongoCollection<Concrete> collection = database.getCollection(getCollectionName()).withDocumentClass(Concrete.class).withCodecRegistry(codecRegistry).withReadPreference(ReadPreference.primary()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
    Concrete firstItem = new Concrete("first", 1, 2L, 3.0, 5L);
    collection.insertOne(firstItem);
    Concrete secondItem = new Concrete("second", 7, 11L, 13.0, 17L);
    collection.insertOne(secondItem);
    // when
    List<String> listOfStringObjectIds = collection.find(new Document("i", 1)).map(new Function<Concrete, ObjectId>() {

        @Override
        public ObjectId apply(final Concrete concrete) {
            return concrete.getId();
        }
    }).map(new Function<ObjectId, String>() {

        @Override
        public String apply(final ObjectId objectId) {
            return objectId.toString();
        }
    }).into(new ArrayList<String>());
    // then
    assertThat(listOfStringObjectIds.size(), is(1));
    assertThat(listOfStringObjectIds.get(0), is(firstItem.getId().toString()));
    // when
    List<ObjectId> listOfObjectIds = collection.find(new Document("i", 1)).map(new Function<Concrete, ObjectId>() {

        @Override
        public ObjectId apply(final Concrete concrete) {
            return concrete.getId();
        }
    }).into(new ArrayList<ObjectId>());
    // then
    assertThat(listOfObjectIds.size(), is(1));
    assertThat(listOfObjectIds.get(0), is(firstItem.getId()));
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) RawBsonDocument(org.bson.RawBsonDocument) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Function(com.mongodb.Function) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 17 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class PojoCodecProviderTest method testAutomatic.

@Test
public void testAutomatic() {
    PojoCodecProvider provider = PojoCodecProvider.builder().automatic(true).build();
    CodecRegistry registry = fromProviders(provider, new ValueCodecProvider());
    Codec<SimpleModel> codec = provider.get(SimpleModel.class, registry);
    assertNotNull(codec);
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) SimpleModel(org.bson.codecs.pojo.entities.SimpleModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Test(org.junit.Test)

Example 18 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class PojoCustomTest method testEnumSupportWithFallback.

@Test
public void testEnumSupportWithFallback() {
    // Create a registry without EnumCodecProvider, to test the fallback in EnumPropertyCodecProvider#get
    CodecRegistry registry = fromRegistries(fromProviders(new ValueCodecProvider(), getPojoCodecProviderBuilder(SimpleEnumModel.class).build()));
    roundTrip(registry, new SimpleEnumModel(SimpleEnum.BRAVO), "{ 'myEnum': 'BRAVO' }");
}
Also used : BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) SimpleEnumModel(org.bson.codecs.pojo.entities.SimpleEnumModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Test(org.junit.Test)

Example 19 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class PojoCustomTest method testMultiplePojoProviders.

@Test
public void testMultiplePojoProviders() {
    NestedGenericHolderFieldWithMultipleTypeParamsModel model = getNestedGenericHolderFieldWithMultipleTypeParamsModel();
    PojoCodecProvider provider1 = PojoCodecProvider.builder().register(NestedGenericHolderFieldWithMultipleTypeParamsModel.class).build();
    PojoCodecProvider provider2 = PojoCodecProvider.builder().register(PropertyWithMultipleTypeParamsModel.class).build();
    PojoCodecProvider provider3 = PojoCodecProvider.builder().register(SimpleGenericsModel.class).build();
    PojoCodecProvider provider4 = PojoCodecProvider.builder().register(GenericHolderModel.class).build();
    CodecRegistry registry = fromProviders(provider1, provider2, provider3, provider4);
    CodecRegistry actualRegistry = fromRegistries(fromProviders(new BsonValueCodecProvider(), new ValueCodecProvider()), registry);
    String json = "{'nested': {'myGenericField': {_t: 'PropertyWithMultipleTypeParamsModel', " + "'simpleGenericsModel': {_t: 'org.bson.codecs.pojo.entities.SimpleGenericsModel', 'myIntegerField': 42, " + "'myGenericField': {'$numberLong': '101'}, 'myListField': ['B', 'C'], 'myMapField': {'D': 2, 'E': 3, 'F': 4 }}}," + "'myLongField': {'$numberLong': '42'}}}";
    roundTrip(actualRegistry, model, json);
}
Also used : BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) NestedGenericHolderFieldWithMultipleTypeParamsModel(org.bson.codecs.pojo.entities.NestedGenericHolderFieldWithMultipleTypeParamsModel) SimpleGenericsModel(org.bson.codecs.pojo.entities.SimpleGenericsModel) BsonRepresentationUnsupportedString(org.bson.codecs.pojo.entities.BsonRepresentationUnsupportedString) PropertyWithMultipleTypeParamsModel(org.bson.codecs.pojo.entities.PropertyWithMultipleTypeParamsModel) GenericHolderModel(org.bson.codecs.pojo.entities.GenericHolderModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 20 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project LanternPowerMonitor by MarkBryanMilligan.

the class DaoSerializer method parseList.

public static List<DaoEntity> parseList(String _json) {
    try {
        List<DaoEntity> entities = new ArrayList<>();
        JsonReader bsonReader = new JsonReader(_json);
        for (Object o : new IterableCodec(CodecRegistries.fromProviders(Arrays.asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider())), new BsonTypeClassMap()).decode(bsonReader, DecoderContext.builder().build())) {
            if (o instanceof Document)
                entities.add(new DaoEntity((Document) o));
        }
        return entities;
    } catch (Exception _e) {
        LOG.error("Failed to parse json", _e);
        return null;
    }
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) ArrayList(java.util.ArrayList) JsonReader(org.bson.json.JsonReader) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Document(org.bson.Document) IterableCodec(org.bson.codecs.IterableCodec) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider)

Aggregations

ValueCodecProvider (org.bson.codecs.ValueCodecProvider)20 Test (org.junit.Test)18 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)16 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)13 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)8 Document (org.bson.Document)7 ObjectId (org.bson.types.ObjectId)5 BsonDocument (org.bson.BsonDocument)4 BsonDocumentWriter (org.bson.BsonDocumentWriter)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)3 Worker (com.mongodb.client.test.Worker)3 WorkerCodecProvider (com.mongodb.client.test.WorkerCodecProvider)3 Date (java.util.Date)3 BsonInt32 (org.bson.BsonInt32)3 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)2 ArrayList (java.util.ArrayList)2 BsonArray (org.bson.BsonArray)2 RawBsonDocument (org.bson.RawBsonDocument)2 SimpleModel (org.bson.codecs.pojo.entities.SimpleModel)2 JsonReader (org.bson.json.JsonReader)2