bson-codecs-jsr310
bson-codecs-jsr310 copied to clipboard
BSON codecs for Java 8 Date and Time API (JSR-310).
BSON codecs for Java 8 Date and Time API (JSR-310)
The library provides codecs for the following JSR-310 classes:
java.time.DayOfWeekjava.time.Durationjava.time.Instantjava.time.LocalDatejava.time.LocalDateTimejava.time.LocalTimejava.time.Monthjava.time.MonthDayjava.time.OffsetDateTimejava.time.OffsetTimejava.time.Periodjava.time.Yearjava.time.YearMonthjava.time.ZonedDateTimejava.time.ZoneIdjava.time.ZoneOffset
Usage
In order to utilize the codecs one can use CodecRegistries helper, for example
in case of Mongo synchronous client:
MongoClient mongoClient = MongoClients.create();
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
MongoClientSettings.getDefaultCodecRegistry(),
CodecRegistries.fromCodecs(new DurationAsDecimal128Codec())
);
MongoDatabase database = client
.getDatabase(...)
.withCodecRegistry(codecRegistry);
Note that depending on a context a different set of codecs might be necessary. There are three main factors to consider when choosing codecs:
- queryability - how much stored values are capable of being searched;
- sortability - how much stored values are capable of being ordered;
- readability - how much stored values are readable for human.
The table below presents the recommendations for all the factors. The more + signs a cell contains the better a row codec is in terms of a column factor:
| Codec | Queryability | Sortability | Readability |
|---|---|---|---|
DayOfWeekAsInt32Codec |
++ | ++ | + |
DayOfWeekAsStringCodec |
+ | ++ | |
DurationAsDecimal128Codec |
+ | ++ | |
DurationAsDocumentCodec |
++ | + | + |
DurationAsStringCodec |
++ | ||
InstantAsDateTimeCodec |
+ | ++ | + |
InstantAsDocumentCodec |
++ | + | + |
InstantAsStringCodec |
++ | ||
LocalDateAsDateTimeCodec |
+ | ++ | + |
LocalDateAsDocumentCodec |
++ | + | + |
LocalDateAsStringCodec |
++ | ||
LocalDateTimeAsDateTimeCodec |
+ | ++ | + |
LocalDateTimeAsDocumentCodec |
++ | + | + |
LocalDateTimeAsStringCodec |
++ | ||
LocalTimeAsDateTimeCodec |
+ | ++ | + |
LocalTimeAsDocumentCodec |
++ | + | + |
LocalTimeAsInt64Codec |
++ | ++ | |
LocalTimeAsStringCodec |
++ | ||
MonthAsInt32Codec |
++ | ++ | + |
MonthAsStringCodec |
+ | ++ | |
MonthDayAsDecimal128Codec |
+ | ++ | ++ |
MonthDayAsDocumentCodec |
++ | + | + |
MonthDayAsStringCodec |
++ | ||
OffsetDateTimeAsDocumentCodec |
++ | + | + |
OffsetDateTimeAsStringCodec |
++ | ||
OffsetTimeAsDocumentCodec |
++ | + | + |
OffsetTimeAsStringCodec |
++ | ||
PeriodAsDocumentCodec |
+ | + | |
PeriodAsStringCodec |
++ | ||
YearAsInt32Codec |
++ | ++ | ++ |
YearMonthAsDecimal128Codec |
+ | ++ | ++ |
YearMonthAsDocumentCodec |
++ | + | + |
YearMonthAsStringCodec |
++ | ||
ZonedDateTimeAsDocumentCodec |
++ | + | + |
ZonedDateTimeAsStringCodec |
++ | ||
ZoneIdAsStringCodec |
+ | ++ | |
ZoneOffsetAsInt32Codec |
++ | ++ | |
ZoneOffsetAsStringCodec |
+ | ++ |