@@ -9,8 +9,8 @@ title = "POJOs"
9
9
10
10
## POJOs - Plain Old Java Objects
11
11
12
- The 3.5 release of the driver adds POJO support via the [ ` PojoCodec ` ] ({{<apiref "org/bson/codecs/pojo/PojoCodec .html">}}), which allows for
13
- direct serialization of POJOs and Java Beans to and from BSON. Internally, each ` PojoCodec ` utilizes a
12
+ The 3.5 release of the driver adds POJO support via the [ ` PojoCodecProvider ` ] ({{<apiref "org/bson/codecs/pojo/PojoCodecProvider .html">}}),
13
+ which allows for direct serialization of POJOs and Java Beans to and from BSON. Internally, the ` Codec ` for each POJO utilizes a
14
14
[ ` ClassModel ` ] ({{<apiref "org/bson/codecs/pojo/ClassModel.html">}}) instance to store metadata about how the POJO should be serialized.
15
15
16
16
A ` ClassModel ` for a POJO includes:
@@ -43,7 +43,7 @@ ClassModels are built using the [`ClassModelBuilder`]({{<apiref "org/bson/codecs
43
43
the [ ` ClassModel.builder(clazz) ` ] ({{<apiref "org/bson/codecs/pojo/ClassModel.html#builder-java.lang.Class-">}}) method. The builder
44
44
initially uses reflection to create the required metadata.
45
45
46
- ` PojoCodec ` instances are created by the [ ` PojoCodecProvider ` ] ({{<apiref "org/bson/codecs/pojo/PojoCodecProvider.html">}}) which is a
46
+ POJO ` Codec ` instances are created by the [ ` PojoCodecProvider ` ] ({{<apiref "org/bson/codecs/pojo/PojoCodecProvider.html">}}) which is a
47
47
` CodecProvider ` . CodecProviders are used by the ` CodecRegistry ` to find the correct ` Codec ` for any given class.
48
48
49
49
{{% note class="important" %}}
@@ -58,8 +58,7 @@ encoded and decoded.
58
58
## POJO support
59
59
60
60
Automatic POJO support can be provided by setting ` PojoCodecProvider.Builder#automatic(true) ` , once built the ` PojoCodecProvider ` will
61
- automatically create a ` PojoCodec ` for any class that contains at least one serializable or deserializable
62
- property.
61
+ automatically create a POJO ` Codec ` for any class that contains at least one serializable or deserializable property.
63
62
64
63
The entry point for customisable POJO support is the ` PojoCodecProvider ` . New instances can be created via the
65
64
[ ` PojoCodecProvider.builder() ` ] ({{<apiref "org/bson/codecs/pojo/PojoCodecProvider.html#builder">}}) method. The ` builder ` allows users to
@@ -70,8 +69,8 @@ register any combination of:
70
69
* ` ClassModel ` instances which allow fine grained control over how a POJO is encoded and decoded.
71
70
72
71
The ` builder ` also allows the user to register default [ Conventions] ( #conventions ) for any POJOs that are automatically mapped, either
73
- the individual POJO classes or POJOs found from registered packages. The ` PojoCodecProvider ` will lookup PojoCodecs and return the first
74
- that matches the POJO class:
72
+ the individual POJO classes or POJOs found from registered packages. The ` PojoCodecProvider ` will lookup POJO ` Codec ` instances and return
73
+ the first that matches the POJO class:
75
74
76
75
* Registered ClassModels
77
76
* Registered POJO classes
@@ -96,14 +95,14 @@ CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), from
96
95
{{% note class="tip" %}}
97
96
In general only one instance of a ` PojoCodecProvider ` should be created.
98
97
99
- This is because each ` PojoCodecProvider ` instance contains a look up table for discriminator names. If multiple PojoCodecProviders are
98
+ This is because each ` PojoCodecProvider ` instance contains a look up table for discriminator names. If multiple ` PojoCodecProvider ` s are
100
99
used, care should be taken to ensure that each provider contains a holistic view of POJO classes, otherwise discriminator lookups can fail.
101
100
Alternatively, using the full class name as the discriminator value will ensure successful POJO lookups.
102
101
{{% /note %}}
103
102
104
103
## Default configuration
105
104
106
- By default the ` PojoCodec ` will not store ` null ` values or a discriminator when converting a POJO to BSON.
105
+ By default the POJO ` Codec ` will not store ` null ` values or a discriminator when converting a POJO to BSON.
107
106
108
107
Take the following ` Person ` class:
109
108
@@ -195,8 +194,8 @@ public final class Tree extends GenericTree<Integer, String> {
195
194
The ` Tree ` POJO is serializable because it doesn't have any unknown type parameters. The ` left ` , ` right ` and ` genericClass ` properties are
196
195
all serializable because they are bound to the concrete types ` Integer ` , ` String ` and ` Long ` .
197
196
198
- On their own, instances of ` GenericTree ` or ` GenericClass ` are not serializable by the ` PojoCodec ` . This is because the runtime type parameter
199
- information is erased by the JVM, and the type parameters cannot be specialized accurately.
197
+ On their own, instances of ` GenericTree ` or ` GenericClass ` are not serializable by the POJO ` Codec ` . This is because the runtime type
198
+ parameter information is erased by the JVM, and the type parameters cannot be specialized accurately.
200
199
201
200
The 3.6 release of the driver further improves generic support with the addition of PropertyCodecProviders. The ` PropertyCodecProvider ` API
202
201
allows type-safe support of container types by providing concrete type parameters for the generic types as declared in the POJO.
@@ -273,8 +272,8 @@ public class Person {
273
272
274
273
### Enum support
275
274
276
- 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
277
- value by the codec using the static ` Enum.valueOf ` method.
275
+ Enums are fully supported. The POJO ` Codec ` uses the name of the enum constant as the property value. This is then converted back into an
276
+ Enum value by the codec using the static ` Enum.valueOf ` method.
278
277
279
278
Take the following example:
280
279
@@ -427,7 +426,7 @@ CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), from
427
426
428
427
### Supporting POJOs without no args constructors
429
428
430
- By default PojoCodecs work with POJOs that have an empty, no arguments, constructor. POJOs with alternative constructors are supported
429
+ By default a POJO ` Codec ` works with POJOs that have an empty, no arguments, constructor. POJOs with alternative constructors are supported
431
430
via the ` ANNOTATION_CONVENTION ` and the ` @BsonCreator ` annotation. Any parameters for the creator constructor should be annotated with the
432
431
` @BsonProperty ` annotation. Below is an example of a ` Person ` POJO that contains final fields that are set via the annotated constructor:
433
432
0 commit comments