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()));
}
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);
}
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' }");
}
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);
}
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;
}
}
Aggregations