Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 17 additions & 1 deletion source/data-formats/extended-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------

Expand Down Expand Up @@ -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>`__
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Rishit and Boris, this isn't worth discussing.

- `JsonOutputMode <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonOutputMode.html>`__
13 changes: 13 additions & 0 deletions source/includes/fundamentals/code-examples/ExtendedJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ public static void Main(string[] args)
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<BsonDocument>(context);
Console.WriteLine(document.ToJson());
}
// end-read-ejson-reader
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a much easier way to parse JSON into a BsonDocument:

var document = BsonDocument.Parse(json);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the point of this example is. It's just a harder way to do something that can be done more simply.

Copy link
Collaborator Author

@mcmorisi mcmorisi May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for context, the topic list I've been working off of lists JsonReader as a class to discuss.

If you don't feel that a JsonReader example is necessary for understanding Extended JSON then I'm fine to cancel this PR.


{
// start-write-ejson
Expand Down
Loading