Skip to content

Commit ffed2c4

Browse files
vkarpov15mbroadst
authored andcommitted
docs: add FAQ about toBSON() (#231)
1 parent 36e328e commit ffed2c4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,21 @@ The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and all
150150
#### Why does `undefined` get converted to `null`?
151151

152152
The `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys.
153+
154+
#### How do I add custom serialization logic?
155+
156+
This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.
157+
158+
```javascript
159+
var bson = new BSON();
160+
161+
class CustomSerialize {
162+
toBSON() {
163+
return 42;
164+
}
165+
}
166+
167+
const obj = { answer: new CustomSerialize() };
168+
// "{ answer: 42 }"
169+
console.log(bson.deserialize(bson.serialize(obj)));
170+
```

0 commit comments

Comments
 (0)