-
Notifications
You must be signed in to change notification settings - Fork 25
DOCSP-49876: Add IBsonReader and IBsonWriter info #641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mcmorisi
merged 8 commits into
mongodb:docsp-45382-comp-cvg
from
mcmorisi:DOCSP-49876-bson-fix
May 30, 2025
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
01f22c2
DOCSP-49876: Add IBsonReader and IBsonWriter info
mcmorisi 92659e7
Fix
mcmorisi e61d57f
Fix
mcmorisi f3153fa
Fix
mcmorisi bace6e0
Fix
mcmorisi 58b7d83
RS feedback
mcmorisi 95e27b5
Fix
mcmorisi 3302d29
MW feedback
mcmorisi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ BSON Operations | |
|
||
.. default-domain:: mongodb | ||
|
||
.. meta:: | ||
:keywords: document, BSON, serializer | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
|
@@ -44,7 +47,8 @@ The code samples in this guide use the following BSON document as an example: | |
Create a BSON Document | ||
---------------------- | ||
|
||
To build a BSON document in {+language+}, create an instance of the ``BsonDocument`` class. | ||
To build a representation of a BSON document in {+language+}, create an instance of the | ||
``BsonDocument`` class. | ||
The ``BsonDocument`` constructor accepts ``BsonElement`` arguments that map to the fields | ||
and values in the document. Each ``BsonElement`` can be either an instance of the | ||
``BsonElement`` class or a field-value pair inside curly braces ( ``{}`` ). | ||
|
@@ -212,6 +216,41 @@ BSON document stored in ``myFile.bson``: | |
``System.IO.Stream`` object. This means that you can read or write any location | ||
that can be accessed by a stream. | ||
|
||
Implement a Custom BSON Reader and Writer | ||
----------------------------------------- | ||
|
||
The preceding examples show how to read and write BSON documents by using the | ||
``BsonBinaryReader`` and ``BsonBinaryWriter`` classes. However, you can also implement a | ||
custom reader and writer by implementing the | ||
``IBsonReader`` and ``IBsonWriter`` interfaces. This is useful when you want to define | ||
a custom serializer or if you want to directly read or write BSON or JSON data without | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While writing a custom serializer is an expected (if rare) thing for a user to do, there is no need for custom readers and writers. Custom serializers use the standard reader and writer interfaces to |
||
interacting with a MongoDB deployment. | ||
|
||
The following code sample shows a custom reader that inherits from the | ||
``IBsonReader`` interface: | ||
|
||
.. code-block:: csharp | ||
|
||
public class CustomReader : IBsonReader | ||
{ | ||
private readonly BinaryReader _reader; | ||
|
||
public CustomReader(Stream stream) | ||
{ | ||
_reader = new BinaryReader(stream); | ||
} | ||
|
||
public void ReadStartDocument() | ||
{ | ||
// Implement logic to read the start of a BSON document here | ||
} | ||
|
||
// Implement other methods from the IBsonReader interface here | ||
} | ||
|
||
To learn more about serialization and deserialization in the {+driver-short+}, see the | ||
:ref:`csharp-serialization` guide. | ||
|
||
.. _csharp-bson-api: | ||
|
||
API Documentation | ||
|
@@ -224,3 +263,5 @@ guide, see the following API documentation: | |
- `BsonElement <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonElement.html>`__ | ||
- `BsonBinaryReader <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.BsonBinaryReader.html>`__ | ||
- `BsonBinaryWriter <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.BsonBinaryWriter.html>`__ | ||
- `IBsonReader <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.IBsonReader.html>`__ | ||
- `IBsonWriter <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.IBsonWriter.html>`__ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think any user would (or should) ever want to implement a custom
IBsonReader
orIBsonWriter
.If we want to document anything at all about these interfaces it should be:
IBsonReader
/IBsonWriter
interfaces can be used to read "documents" represented in several formatsBsonDocument
The three sets of standard readers and writers are:
BsonBinaryReader
andBsonBinaryWriter
JsonReader
andJsonWriter
BsonDocumentReader
andBsonDocumentWriter
They all implement the same interfaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will take another crack at this section!