use of org.bson.codecs.DateCodec in project immutables by immutables.
the class BsonModule method defaultRegistry.
private static CodecRegistry defaultRegistry() {
CodecRegistry standard = CodecRegistries.fromProviders(new BsonValueCodecProvider(), new Jsr310CodecProvider());
// avoid codecs for String / Long / Boolean etc. They're already handled by jackson
// choose the ones which need to be natively serialized in non-JSON format (BSON)
CodecRegistry others = CodecRegistries.fromCodecs(new ObjectIdCodec(), new DateCodec(), new UuidCodec(UuidRepresentation.JAVA_LEGACY), new Decimal128Codec(), new PatternCodec(), new BigDecimalCodec(), new ByteArrayCodec());
return CodecRegistries.fromRegistries(standard, others);
}
use of org.bson.codecs.DateCodec in project immutables by immutables.
the class JavaTimeTest method javaUtilDate.
@Test
public void javaUtilDate() throws IOException {
Date now = new Date();
long epoch = now.getTime();
TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec());
// read
Date date = adapter.read(Jsons.readerAt(new BsonDateTime(epoch)));
check(date).is(now);
// write
BsonValue bson = writeAndReadBson(now);
check(bson.getBsonType()).is(BsonType.DATE_TIME);
check(new Date(bson.asDateTime().getValue())).is(now);
}
use of org.bson.codecs.DateCodec in project immutables by immutables.
the class GsonCodecsTest method dateCodec.
@Test
public void dateCodec() throws IOException {
TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec());
Date date = new Date();
BsonDocument doc = new BsonDocument();
BsonDocumentWriter writer = new BsonDocumentWriter(doc);
writer.writeStartDocument();
writer.writeName("$date");
adapter.write(new BsonWriter(writer), date);
writer.writeEndDocument();
check(doc.keySet()).hasSize(1);
check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME);
check(doc.get("$date").asDateTime().getValue()).is(date.getTime());
}
use of org.bson.codecs.DateCodec in project immutables by immutables.
the class JavaTimeTest method setUp.
@Before
public void setUp() {
CodecRegistry registry = CodecRegistries.fromProviders(new Jsr310CodecProvider());
TypeAdapterFactory factory = GsonCodecs.delegatingTypeAdapterFactory(registry);
TypeAdapter<Date> dateTypeAdapter = GsonCodecs.typeAdapterFromCodec(new DateCodec());
gson = new GsonBuilder().registerTypeAdapter(Date.class, dateTypeAdapter).registerTypeAdapterFactory(factory).create();
}
use of org.bson.codecs.DateCodec in project immutables by immutables.
the class SupportTest method unwrapObject.
@Test
public void unwrapObject() throws Exception {
Date date = new Date();
Support.Adapted<Date> adapted = new Support.Adapted<>(new DateCodec(), date);
Object result = Support.unwrapBsonable(adapted);
check(result.toString()).isNonEmpty();
}
Aggregations