Skip to content

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
merged 8 commits into from
May 30, 2025
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion source/data-formats/bson.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ BSON Operations

.. default-domain:: mongodb

.. meta::
:keywords: document, BSON, serializer

.. contents:: On this page
:local:
:backlinks: none
Expand Down Expand Up @@ -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 ( ``{}`` ).
Expand Down Expand Up @@ -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
Copy link
Collaborator

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 or IBsonWriter.

If we want to document anything at all about these interfaces it should be:

  1. That IBsonReader/IBsonWriter interfaces can be used to read "documents" represented in several formats
  2. The we have readers and writers for 3 formats: BSON, JSON and in-memory BsonDocument

The three sets of standard readers and writers are:

  1. BsonBinaryReader and BsonBinaryWriter
  2. JsonReader and JsonWriter
  3. BsonDocumentReader and BsonDocumentWriter

They all implement the same interfaces.

Copy link
Collaborator Author

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!

-----------------------------------------

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 Deserialize and Serialize values.

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
Expand All @@ -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>`__
Loading