Skip to content

Commit

Permalink
docs: add FAQ about toBSON() (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored and daprahamian committed May 31, 2018
1 parent e7a8558 commit 00a7f57
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,21 @@ The BSON `deserializeStream` method takes a Node.js Buffer, `startIndex` and all
#### Why does `undefined` get converted to `null`?

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.

#### How do I add custom serialization logic?

This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.

```javascript
var bson = new BSON();

class CustomSerialize {
toBSON() {
return 42;
}
}

const obj = { answer: new CustomSerialize() };
// "{ answer: 42 }"
console.log(bson.deserialize(bson.serialize(obj)));
```

0 comments on commit 00a7f57

Please sign in to comment.