From 01f22c2515a772b9a98a452de67a3dd2bc59b536 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 28 May 2025 09:58:48 -0400 Subject: [PATCH 1/8] DOCSP-49876: Add IBsonReader and IBsonWriter info --- source/data-formats/bson.txt | 21 ++++++++++++++++++- .../connection/ConnectionStringConfig.cs | 0 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 source/includes/fundamentals/code-examples/connection/ConnectionStringConfig.cs diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index e04523f2..96e5a366 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -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,19 @@ 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. +Implementing 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 +interacting with a MongoDB deployment. + +To learn more about serialization and deserialization in {+language+}, see the +:ref:`csharp-serialization` guide. + .. _csharp-bson-api: API Documentation @@ -224,3 +241,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>`__ \ No newline at end of file diff --git a/source/includes/fundamentals/code-examples/connection/ConnectionStringConfig.cs b/source/includes/fundamentals/code-examples/connection/ConnectionStringConfig.cs new file mode 100644 index 00000000..e69de29b From 92659e7e04e4b31d98dc21d588a30b47626bfc68 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 28 May 2025 10:00:01 -0400 Subject: [PATCH 2/8] Fix --- source/data-formats/bson.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 96e5a366..1e246fc7 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -226,7 +226,7 @@ custom reader and writer by implementing the a custom serializer or if you want to directly read or write BSON or JSON data without interacting with a MongoDB deployment. -To learn more about serialization and deserialization in {+language+}, see the +To learn more about serialization and deserialization in the {+driver-short+}, see the :ref:`csharp-serialization` guide. .. _csharp-bson-api: From e61d57f36681b734efbd4c9999f385716faf4275 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 28 May 2025 10:10:19 -0400 Subject: [PATCH 3/8] Fix --- source/data-formats/bson.txt | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 1e246fc7..1a091b44 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -216,8 +216,8 @@ 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. -Implementing a Custom BSON Reader and Writer --------------------------------------------- +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 @@ -226,6 +226,26 @@ custom reader and writer by implementing the a custom serializer or if you want to directly read or write BSON or JSON data without 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 + { + 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. From f3153fab2bb16695f1d54ad6b087aaf40a43c0e8 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 28 May 2025 10:17:37 -0400 Subject: [PATCH 4/8] Fix --- source/data-formats/bson.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 1a091b44..7bb72478 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -233,6 +233,8 @@ The following code sample shows a custom reader that inherits from the public class CustomReader : IBsonReader { + private readonly BinaryReader _reader; + public CustomReader(Stream stream) { _reader = new BinaryReader(stream); From bace6e09f9c9bba42e847ee0ceb0bd3ea414db1c Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 28 May 2025 15:12:07 -0400 Subject: [PATCH 5/8] Fix --- .../code-examples/connection/ConnectionStringConfig.cs | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 source/includes/fundamentals/code-examples/connection/ConnectionStringConfig.cs diff --git a/source/includes/fundamentals/code-examples/connection/ConnectionStringConfig.cs b/source/includes/fundamentals/code-examples/connection/ConnectionStringConfig.cs deleted file mode 100644 index e69de29b..00000000 From 58b7d83a10c67e35147ae578ab7f70a95feef4ca Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Thu, 29 May 2025 14:37:55 -0400 Subject: [PATCH 6/8] RS feedback --- source/data-formats/bson.txt | 43 ++++++++++-------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 7bb72478..85310243 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -216,40 +216,21 @@ 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 ------------------------------------------ +Read and Write Other Formats +---------------------------- -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 -interacting with a MongoDB deployment. +The preceding examples show how to read and write BSON data by using the +``BsonBinaryReader`` and ``BsonBinaryWriter`` classes. These classes implement the +``IBsonReader`` and ``IBsonWriter`` interfaces. To read and write data in other formats, +the {+driver-short+} provides the following alternative implementations of the ``IBsonReader`` +and ``IBsonWriter`` interfaces: -The following code sample shows a custom reader that inherits from the -``IBsonReader`` interface: +- ``JsonReader`` and ``JsonWriter``: Read and write JSON data +- ``BsonDocumentReader`` and ``BsonDocumentWriter``: Read and write BSON data +contained in a ``BsonDocument`` object -.. 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. +Because these classes implement the same interfaces, you can call their methods in the same way +as the preceding ``BsonBinaryReader`` and ``BsonBinaryWriter`` examples. .. _csharp-bson-api: From 95e27b5893d2de72e0263e588f4d273b8ca6b009 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Thu, 29 May 2025 14:44:35 -0400 Subject: [PATCH 7/8] Fix --- source/data-formats/bson.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index 85310243..b658837b 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -225,9 +225,9 @@ The preceding examples show how to read and write BSON data by using the the {+driver-short+} provides the following alternative implementations of the ``IBsonReader`` and ``IBsonWriter`` interfaces: -- ``JsonReader`` and ``JsonWriter``: Read and write JSON data +- ``JsonReader`` and ``JsonWriter``: Read and write JSON data. - ``BsonDocumentReader`` and ``BsonDocumentWriter``: Read and write BSON data -contained in a ``BsonDocument`` object + contained in a ``BsonDocument`` object. Because these classes implement the same interfaces, you can call their methods in the same way as the preceding ``BsonBinaryReader`` and ``BsonBinaryWriter`` examples. From 3302d29565982bd68be4bc90c88e6cd1ed5fa16b Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Fri, 30 May 2025 13:48:18 -0400 Subject: [PATCH 8/8] MW feedback --- source/data-formats/bson.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/data-formats/bson.txt b/source/data-formats/bson.txt index b658837b..a8f12abd 100644 --- a/source/data-formats/bson.txt +++ b/source/data-formats/bson.txt @@ -225,9 +225,9 @@ The preceding examples show how to read and write BSON data by using the the {+driver-short+} provides the following alternative implementations of the ``IBsonReader`` and ``IBsonWriter`` interfaces: -- ``JsonReader`` and ``JsonWriter``: Read and write JSON data. +- ``JsonReader`` and ``JsonWriter``: Read and write JSON data - ``BsonDocumentReader`` and ``BsonDocumentWriter``: Read and write BSON data - contained in a ``BsonDocument`` object. + contained in a ``BsonDocument`` object Because these classes implement the same interfaces, you can call their methods in the same way as the preceding ``BsonBinaryReader`` and ``BsonBinaryWriter`` examples.