@@ -8,4 +8,84 @@ Extended JSON
88 :local:
99 :backlinks: none
1010 :depth: 2
11- :class: singlecol
11+ :class: singlecol
12+
13+ .. facet::
14+ :name: genre
15+ :values: reference
16+
17+ .. meta::
18+ :keywords: code examples, bson, relaxed, canonical, legacy
19+
20+ .. sharedinclude:: dbx/extended-json.rst
21+
22+ .. replacement:: driver-specific-text-relaxed
23+
24+ The {+driver-short+} uses Relaxed mode by default.
25+
26+
27+ Read Extended JSON
28+ ------------------
29+
30+ You can read an Extended JSON documents into a {+language+} object by using the
31+ ``BsonSerializer.Deserialize<T>()`` method. The following example reads an
32+ Extended JSON document into a ``BsonDocument`` object:
33+
34+ .. io-code-block::
35+
36+ .. input:: /includes/fundamentals/code-examples/ExtendedJson.cs
37+ :language: csharp
38+ :start-after: start-read-ejson
39+ :end-before: end-read-ejson
40+ :dedent:
41+
42+ .. output::
43+ :visible: false
44+
45+ { "_id" : { "$oid" : "573a1391f29313caabcd9637" }, "createdAt" : { "$date" : "1970-01-19T12:51:39.609Z" }, "numViews" : 36520312 }
46+
47+ Write Extended JSON
48+ -------------------
49+
50+ You can write an Extended JSON string by calling the ``ToJson()`` method on a
51+ ``BsonDocument`` object or custom class. You must specify a ``JsonWriterSettings`` object
52+ with the ``OutputMode`` property set to the desired Extended JSON format as a parameter.
53+
54+ Consider the following custom class:
55+
56+ .. literalinclude:: /includes/fundamentals/code-examples/ExtendedJson.cs
57+ :language: csharp
58+ :start-after: start-custom-class
59+ :end-before: end-custom-class
60+
61+ The following example outputs an instance of ``MyDocument`` in
62+ Extended JSON format by specifying the ``CanonicalExtendedJson`` value as an ``OutputMode``
63+ property:
64+
65+ .. io-code-block::
66+
67+ .. input:: /includes/fundamentals/code-examples/ExtendedJson.cs
68+ :language: csharp
69+ :start-after: start-write-ejson
70+ :end-before: end-write-ejson
71+ :dedent:
72+
73+ .. output::
74+ :visible: false
75+
76+ { "_id" : { "$oid" : "68094769744af81f368ff1c1" }, "CreatedAt" : { "$date" : { "$numberLong" : "1745438569994" } }, "NumViews" : { "$numberLong" : "1234567890" } }
77+
78+ API Documentation
79+ -----------------
80+
81+ To learn more about the methods and classes used on this page, see the following API
82+ documentation:
83+
84+ - `BsonDocument <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonDocument.html>`__
85+ - `BsonSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.BsonSerializer.html>`__
86+ - `ToJson() <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonExtensionMethods.ToJson.html>`__
87+ - `JsonWriter <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonWriter.html>`__
88+ - `JsonReader <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonReader.html>`__
89+ - `JsonWriterSettings <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonWriterSettings.html>`__
90+ - `JsonReaderSettings <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonReaderSettings.html>`__
91+ - `JsonOutputMode <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.IO.JsonOutputMode.html>`__
0 commit comments