Skip to content

Commit 00a7f57

Browse files
vkarpov15daprahamian
authored andcommitted
docs: add FAQ about toBSON() (#231)
1 parent e7a8558 commit 00a7f57

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,21 @@ The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and all
148148
#### Why does `undefined` get converted to `null`?
149149

150150
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.
151+
152+
#### How do I add custom serialization logic?
153+
154+
This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.
155+
156+
```javascript
157+
var bson = new BSON();
158+
159+
class CustomSerialize {
160+
toBSON() {
161+
return 42;
162+
}
163+
}
164+
165+
const obj = { answer: new CustomSerialize() };
166+
// "{ answer: 42 }"
167+
console.log(bson.deserialize(bson.serialize(obj)));
168+
```

0 commit comments

Comments
 (0)