1
1
# Converting BSON data through codecs
2
2
3
- The codec system is a more advanced way to convert BSON data to native types and back, designed for libraries with more
4
- advanced use cases, e.g. object mappers. It is designed to decouple the serialisation logic from the data model,
5
- allowing for more flexible implementations.
3
+ Codecs provide a more flexible way to convert BSON data to native types and back and address most of the shortcomings of
4
+ the previous type map system. The codec system is designed to be used by libraries that need to convert BSON data into
5
+ native types, for example object mappers. Unlike the type map system, codecs allow converting any BSON type to a native
6
+ type directly when reading data from the database. Together with lazy decoding of BSON structures, this allows
7
+ for a more flexible and efficient way to handle BSON data.
6
8
7
9
## Encoders and Decoders
8
10
9
- The codec interface is split into two smaller interfaces: encoders and decoders. Both interfaces are marked as internal,
10
- as users are only expected to interact with the Codec interface. The interfaces are typed using Psalm generics, allowing
11
- for better type checking when they are used. Without type annotations, the interfaces are equivalent to the following:
11
+ The codec interface is comprised by two smaller interfaces: encoders and decoders. Both interfaces are marked as
12
+ internal, as users are only expected to interact with the Codec interface. The interfaces are typed as generics through
13
+ ` @template ` annotations, allowing for better type checking when they are used. Without type annotations, the interfaces
14
+ are equivalent to the following:
12
15
13
16
``` php
14
17
namespace MongoDB\Codec;
@@ -32,7 +35,7 @@ interface Encoder
32
35
}
33
36
```
34
37
35
- ## Codec interface
38
+ ## Codec Interface
36
39
37
40
The ` Codec ` interface combines decoding and encoding into a single interface. This will be used for most values except
38
41
for documents where a more specific ` DocumentCodec ` is provided.
@@ -44,23 +47,16 @@ encode or decode a value only if it is supported. If it is not supported, the or
44
47
``` php
45
48
namespace MongoDB\Codec;
46
49
47
- /**
48
- * @psalm-template BSONType
49
- * @psalm-template NativeType
50
- * @template-extends Decoder<BSONType , NativeType >
51
- * @template-extends Encoder<BSONType , NativeType >
52
- */
53
50
interface Codec extends Decoder, Encoder
54
51
{
55
52
}
56
53
```
57
54
58
- ## Document codec
55
+ ## Document Codec
59
56
60
57
The document codec is special as it is guaranteed to always encode to a BSON document instance and decode to a PHP
61
- object. Document codecs can be provided to a ` MongoDB\Collection ` instance to have it automatically decode data read
62
- from the database. Likewise, any supported value is encoded before writing to the database in ` insert ` and ` replace `
63
- operations.
58
+ object. Document codecs will be used by ` MongoDB\Collection ` instances to automatically decode BSON data into PHP
59
+ objects when reading data, and to encode PHP objects when inserting or replacing data.
64
60
65
61
``` php
66
62
namespace MongoDB\Codec;
@@ -76,11 +72,29 @@ interface DocumentCodec extends Codec
76
72
}
77
73
```
78
74
79
- ## Using codecs
75
+ ## Built-in codecs
76
+
77
+ By default, two codecs are provided: an ` ArrayCodec ` and an ` ObjectCodec ` . These two codecs are used to recursively
78
+ encode and decode values in arrays and ` stdClass ` instances, respectively. When encoding or decoding an object,
79
+ ` ObjectCodec ` only handles public properties of the object and ignores private and protected properties.
80
+
81
+ ## Future Work
82
+
83
+ ### Using Codecs
84
+
85
+ The ` MongoDB\Collection ` class and all operations that work with documents now take a ` codec ` option. This option is
86
+ passed along to the various operations that already take a ` typeMap ` option. Collections only support a ` DocumentCodec `
87
+ instance to guarantee that data always encodes to a BSON document and decodes to a PHP object.
88
+
89
+ All operations that return documents will use the codec to decode the documents into PHP objects. This includes
90
+ the various ` find ` and ` findAndModify ` operations in collections as well as the ` aggregate ` and ` watch ` operations in
91
+ collections, databases, and the client object itself.
92
+
93
+ When writing data, any operation that takes an entire document will use the codec to automatically encode the document.
94
+ This is limited to ` insertOne ` , ` insertMany ` , ` replaceOne ` , and ` findOneAndReplace ` operations in collections. ` update `
95
+ operations will not use the codec to encode documents, as they only support update operators and can't work with the
96
+ entire document.
80
97
81
- The ` MongoDB\Collection ` class and all operations that work with documents now take a ` codec ` option. This can be
82
- an instance of a ` DocumentCodec ` that will be used to encode documents (for insert and replace operations) and decode
83
- them into PHP objects when reading data.
84
98
85
99
### Codecs and type maps
86
100
@@ -96,9 +110,3 @@ this case, the type map is used. The precedence order is as follows:
96
110
Codecs are not inherited from the client or the database object, as they are purely used for operations that return
97
111
documents. However, database- or client-level aggregation commands will take an operation-level codec option to
98
112
decode the resulting documents.
99
-
100
- ## Built-in codecs
101
-
102
- By default, two codecs are provided: an ` ArrayCodec ` and an ` ObjectCodec ` . These two codecs are used to recursively
103
- encode and decode values in arrays and ` stdClass ` instances, respectively. ` ObjectCodec ` only handles public properties
104
- in objects and ignores other properties.
0 commit comments