Search in sources :

Example 1 with DateCodec

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);
}
Also used : ByteArrayCodec(org.bson.codecs.ByteArrayCodec) UuidCodec(org.bson.codecs.UuidCodec) Jsr310CodecProvider(org.bson.codecs.jsr310.Jsr310CodecProvider) ObjectIdCodec(org.bson.codecs.ObjectIdCodec) DateCodec(org.bson.codecs.DateCodec) Decimal128Codec(org.bson.codecs.Decimal128Codec) BigDecimalCodec(org.bson.codecs.BigDecimalCodec) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) PatternCodec(org.bson.codecs.PatternCodec) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider)

Example 2 with DateCodec

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);
}
Also used : BsonDateTime(org.bson.BsonDateTime) DateCodec(org.bson.codecs.DateCodec) Date(java.util.Date) LocalDate(java.time.LocalDate) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 3 with DateCodec

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());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) DateCodec(org.bson.codecs.DateCodec) Date(java.util.Date) Test(org.junit.Test)

Example 4 with DateCodec

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();
}
Also used : TypeAdapterFactory(com.google.gson.TypeAdapterFactory) GsonBuilder(com.google.gson.GsonBuilder) Jsr310CodecProvider(org.bson.codecs.jsr310.Jsr310CodecProvider) DateCodec(org.bson.codecs.DateCodec) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) LocalDate(java.time.LocalDate) Before(org.junit.Before)

Example 5 with DateCodec

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();
}
Also used : DateCodec(org.bson.codecs.DateCodec) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DateCodec (org.bson.codecs.DateCodec)5 Date (java.util.Date)4 Test (org.junit.Test)3 LocalDate (java.time.LocalDate)2 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)2 Jsr310CodecProvider (org.bson.codecs.jsr310.Jsr310CodecProvider)2 GsonBuilder (com.google.gson.GsonBuilder)1 TypeAdapterFactory (com.google.gson.TypeAdapterFactory)1 BsonDateTime (org.bson.BsonDateTime)1 BsonDocument (org.bson.BsonDocument)1 BsonDocumentWriter (org.bson.BsonDocumentWriter)1 BsonValue (org.bson.BsonValue)1 BigDecimalCodec (org.bson.codecs.BigDecimalCodec)1 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)1 ByteArrayCodec (org.bson.codecs.ByteArrayCodec)1 Decimal128Codec (org.bson.codecs.Decimal128Codec)1 ObjectIdCodec (org.bson.codecs.ObjectIdCodec)1 PatternCodec (org.bson.codecs.PatternCodec)1 UuidCodec (org.bson.codecs.UuidCodec)1 Before (org.junit.Before)1