Skip to content

Commit d9e6a3a

Browse files
committed
Documentation updates for the PojoCodec updates
JAVA-2686
1 parent 6a13877 commit d9e6a3a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

docs/reference/content/bson/pojos.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,79 @@ all serializable because they are bound to the concrete types `Integer`, `String
196196
On their own, instances of `GenericTree` or `GenericClass` are not serializable by the `PojoCodec`. This is because the runtime type parameter
197197
information is erased by the JVM, and the type parameters cannot be specialized accurately.
198198

199+
The 3.6 release of the driver further improves generic support with the addition of PropertyCodecProviders. The `PropertyCodecProvider` API
200+
allows type-safe support of container types by providing concrete type parameters for the generic types as declared in the POJO.
201+
202+
A great use of the `PropertyCodecProvider` API could be to add support for Guava's `Optional` class. The following example creates a
203+
`OptionalPropertyCodecProvider`:
204+
205+
```java
206+
public class OptionalPropertyCodecProvider implements PropertyCodecProvider {
207+
208+
@Override
209+
@SuppressWarnings({"rawtypes", "unchecked"})
210+
public <T> Codec<T> get(final TypeWithTypeParameters<T> type, final PropertyCodecRegistry registry) {
211+
// Check the main type and number of generic parameters
212+
if (Optional.class.isAssignableFrom(type.getType()) && type.getTypeParameters().size() == 1) {
213+
// Get the codec for the concrete type of the Optional, as its declared in the POJO.
214+
Codec<?> valueCodec = registry.get(type.getTypeParameters().get(0));
215+
return new OptionalCodec(type.getType(), valueCodec);
216+
} else {
217+
return null;
218+
}
219+
}
220+
221+
private static final class OptionalCodec<T> implements Codec<Optional<T>> {
222+
private final Class<Optional<T>> encoderClass;
223+
private final Codec<T> codec;
224+
225+
private OptionalCodec(final Class<Optional<T>> encoderClass, final Codec<T> codec) {
226+
this.encoderClass = encoderClass;
227+
this.codec = codec;
228+
}
229+
230+
@Override
231+
public void encode(final BsonWriter writer, final Optional<T> optionalValue, final EncoderContext encoderContext) {
232+
if (optionalValue != null && optionalValue.isPresent()) {
233+
codec.encode(writer, optionalValue.get(), encoderContext);
234+
} else {
235+
writer.writeNull();
236+
}
237+
}
238+
239+
@Override
240+
public Optional<T> decode(final BsonReader reader, final DecoderContext context) {
241+
return Optional.of(codec.decode(reader, context));
242+
}
243+
244+
@Override
245+
public Class<Optional<T>> getEncoderClass() {
246+
return encoderClass;
247+
}
248+
}
249+
}
250+
```
251+
252+
The `OptionalPropertyCodecProvider` can be registered via the `PojoCodecProvider.builder().register` method:
253+
254+
```java
255+
CodecProvider pojoCodecProvider = PojoCodecProvider.builder()
256+
.register("org.example.pojos")
257+
.register(new OptionalPropertyCodecProvider())
258+
.build();
259+
```
260+
261+
Then `Optional` classes are fully supported in the POJOs. In the following example a `Person` has an `Optional` address and an `Optional`
262+
membership id:
263+
264+
```java
265+
public class Person {
266+
...
267+
private Optional<Address> optionalAddress;
268+
private Optional<Integer> optionalMembershipId;
269+
}
270+
```
271+
199272
### Enum support
200273

201274
Enums are fully supported. The `PojoCodec` uses the name of the enum constant as the property value. This is then converted back into an Enum

docs/reference/content/whats-new.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ The 3.6 release adds support for [causally consistency](http://dochub.mongodb.or
4040
The 3.6 release adds support for application-configured control over server selection, using the `serverSelector` option in
4141
[`MongoClientOptions`]({{<apiref "com/mongodb/MongoClientOptions">}}).
4242

43+
### PojoCodec improvements
44+
45+
The 3.6 release brings new improvements to the `PojoCodec`:
46+
47+
* Improved sub-class and discriminator support.
48+
* Support for custom Collection and Map implementations.
49+
* Improvements to the `BsonCreator` annotation, which now supports `@BsonId` and `@BsonProperty` with values that represent the read name of the property.
50+
* A new [`PropertyCodecProvider`]({{<apiref "org/bson/codecs/pojo/PropertyCodecProvider">}}) API, allowing for easy and type-safe handling of container types.
51+
52+
The MongoDB Java drivers team would like to thank both [Joseph Florencio](https://github.com/jflorencio) and [Qi Liu](https://github.com/visualage)
53+
for their excellent contributions to the PojoCodec.
54+
4355
## What's New in 3.5
4456

4557
Key new features of the 3.5 Java driver release:

0 commit comments

Comments
 (0)