From 75972fa7b3a8f0d98edcc6b3bcf2c9bcdde935bf Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 20 May 2025 12:11:43 -0400 Subject: [PATCH 1/4] DOCSP-49867: Add JsonReader example --- source/data-formats/extended-json.txt | 18 +++++++++++++++++- .../fundamentals/code-examples/ExtendedJson.cs | 13 +++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/source/data-formats/extended-json.txt b/source/data-formats/extended-json.txt index 3d62ec4e..bedfcb21 100644 --- a/source/data-formats/extended-json.txt +++ b/source/data-formats/extended-json.txt @@ -44,6 +44,23 @@ Extended JSON document into a ``BsonDocument`` object: { "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 } +Alternatively, you can use the ``JsonReader`` class to read Extended JSON documents. The +following example reads uses the ``JsonReader`` class to read the same Extended JSON document +as the preceding example into a ``BsonDocument`` object: + +.. io-code-block:: + + .. input:: /includes/fundamentals/code-examples/ExtendedJson.cs + :language: csharp + :start-after: start-read-ejson-reader + :end-before: end-read-ejson-reader + :dedent: + + .. output:: + :visible: false + + { "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 } + Write Extended JSON ------------------- @@ -87,5 +104,4 @@ documentation: - `JsonWriter <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonWriter.html>`__ - `JsonReader <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonReader.html>`__ - `JsonWriterSettings <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonWriterSettings.html>`__ -- `JsonReaderSettings <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonReaderSettings.html>`__ - `JsonOutputMode <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonOutputMode.html>`__ \ No newline at end of file diff --git a/source/includes/fundamentals/code-examples/ExtendedJson.cs b/source/includes/fundamentals/code-examples/ExtendedJson.cs index 1f0f1a8e..fd8bc326 100644 --- a/source/includes/fundamentals/code-examples/ExtendedJson.cs +++ b/source/includes/fundamentals/code-examples/ExtendedJson.cs @@ -14,6 +14,19 @@ public static void Main(string[] args) Console.WriteLine(document.ToJson()); // end-read-ejson } + + { + // start-read-json-reader + var ejson = "{\n\"_id\": { \"$oid\": \"573a1391f29313caabcd9637\" },\n \"createdAt\": { \"$date\": { \"$numberLong\": \"1601499609\" }},\n\"numViews\": { \"$numberLong\": \"36520312\" }\n}\n\n"; + var subject = new BsonDocumentSerializer(); + using (var reader = new JsonReader(ejson)) + { + var context = BsonDeserializationContext.CreateRoot(reader); + var document = subject.Deserialize(context); + Console.WriteLine(document.ToJson()); + } + // end-read-json-reader + } { // start-write-ejson From 7cb727a1dc7b7e3167edd324112f740aa87b2643 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 20 May 2025 12:15:48 -0400 Subject: [PATCH 2/4] Fix --- source/includes/fundamentals/code-examples/ExtendedJson.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/includes/fundamentals/code-examples/ExtendedJson.cs b/source/includes/fundamentals/code-examples/ExtendedJson.cs index fd8bc326..d6c62441 100644 --- a/source/includes/fundamentals/code-examples/ExtendedJson.cs +++ b/source/includes/fundamentals/code-examples/ExtendedJson.cs @@ -16,7 +16,7 @@ public static void Main(string[] args) } { - // start-read-json-reader + // start-read-ejson-reader var ejson = "{\n\"_id\": { \"$oid\": \"573a1391f29313caabcd9637\" },\n \"createdAt\": { \"$date\": { \"$numberLong\": \"1601499609\" }},\n\"numViews\": { \"$numberLong\": \"36520312\" }\n}\n\n"; var subject = new BsonDocumentSerializer(); using (var reader = new JsonReader(ejson)) @@ -25,7 +25,7 @@ public static void Main(string[] args) var document = subject.Deserialize(context); Console.WriteLine(document.ToJson()); } - // end-read-json-reader + // end-read-ejson-reader } { From a25be856954769be0d96d155a4bd3aa2842e82c5 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Tue, 20 May 2025 13:49:59 -0400 Subject: [PATCH 3/4] RR feedback --- source/data-formats/extended-json.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/data-formats/extended-json.txt b/source/data-formats/extended-json.txt index bedfcb21..c57a8ffc 100644 --- a/source/data-formats/extended-json.txt +++ b/source/data-formats/extended-json.txt @@ -44,9 +44,8 @@ Extended JSON document into a ``BsonDocument`` object: { "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 } -Alternatively, you can use the ``JsonReader`` class to read Extended JSON documents. The -following example reads uses the ``JsonReader`` class to read the same Extended JSON document -as the preceding example into a ``BsonDocument`` object: +Alternatively, you can use the ``JsonReader`` class to read Extended JSON documents +into other formats such as ``BsonDocument``, as shown in the following example: .. io-code-block:: From 57b50294d727fd79b2034a7aa010c0929fde4232 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Thu, 22 May 2025 12:26:23 -0400 Subject: [PATCH 4/4] Technical feedback --- source/data-formats/extended-json.txt | 20 ++----------------- .../code-examples/ExtendedJson.cs | 17 +--------------- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/source/data-formats/extended-json.txt b/source/data-formats/extended-json.txt index c57a8ffc..010910ff 100644 --- a/source/data-formats/extended-json.txt +++ b/source/data-formats/extended-json.txt @@ -28,7 +28,7 @@ Read Extended JSON ------------------ You can read an Extended JSON documents into a {+language+} object by using the -``BsonSerializer.Deserialize()`` method. The following example reads an +``BsonDocument.Parse()`` method. The following example reads an Extended JSON document into a ``BsonDocument`` object: .. io-code-block:: @@ -44,22 +44,6 @@ Extended JSON document into a ``BsonDocument`` object: { "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 } -Alternatively, you can use the ``JsonReader`` class to read Extended JSON documents -into other formats such as ``BsonDocument``, as shown in the following example: - -.. io-code-block:: - - .. input:: /includes/fundamentals/code-examples/ExtendedJson.cs - :language: csharp - :start-after: start-read-ejson-reader - :end-before: end-read-ejson-reader - :dedent: - - .. output:: - :visible: false - - { "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 } - Write Extended JSON ------------------- @@ -94,7 +78,7 @@ property: API Documentation ----------------- -To learn more about the methods and classes used on this page, see the following API +To learn more about the methods and classes you can use to work with JSON documents, see the following API documentation: - `BsonDocument <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonDocument.html>`__ diff --git a/source/includes/fundamentals/code-examples/ExtendedJson.cs b/source/includes/fundamentals/code-examples/ExtendedJson.cs index d6c62441..7a24d794 100644 --- a/source/includes/fundamentals/code-examples/ExtendedJson.cs +++ b/source/includes/fundamentals/code-examples/ExtendedJson.cs @@ -1,6 +1,4 @@ using MongoDB.Bson; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization; public class ExtendedJson { @@ -10,24 +8,11 @@ public static void Main(string[] args) // start-read-ejson var ejson = "{\n\"_id\": { \"$oid\": \"573a1391f29313caabcd9637\" },\n \"createdAt\": { \"$date\": { \"$numberLong\": \"1601499609\" }},\n\"numViews\": { \"$numberLong\": \"36520312\" }\n}\n\n"; - var document = BsonSerializer.Deserialize(ejson); + var document = BsonDocument.Parse(ejson); Console.WriteLine(document.ToJson()); // end-read-ejson } - { - // start-read-ejson-reader - var ejson = "{\n\"_id\": { \"$oid\": \"573a1391f29313caabcd9637\" },\n \"createdAt\": { \"$date\": { \"$numberLong\": \"1601499609\" }},\n\"numViews\": { \"$numberLong\": \"36520312\" }\n}\n\n"; - var subject = new BsonDocumentSerializer(); - using (var reader = new JsonReader(ejson)) - { - var context = BsonDeserializationContext.CreateRoot(reader); - var document = subject.Deserialize(context); - Console.WriteLine(document.ToJson()); - } - // end-read-ejson-reader - } - { // start-write-ejson var document = new MyDocument();