Skip to content

Commit c233f4f

Browse files
authored
DOCSP-49078: Extended JSON (#619)
1 parent 473761b commit c233f4f

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

source/data-formats/extended-json.txt

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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>`__
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.IO;
3+
using MongoDB.Bson.Serialization;
4+
5+
public class ExtendedJson
6+
{
7+
public static void Main(string[] args)
8+
{
9+
{
10+
// start-read-ejson
11+
var ejson = "{\n\"_id\": { \"$oid\": \"573a1391f29313caabcd9637\" },\n \"createdAt\": { \"$date\": { \"$numberLong\": \"1601499609\" }},\n\"numViews\": { \"$numberLong\": \"36520312\" }\n}\n\n";
12+
13+
var document = BsonSerializer.Deserialize<BsonDocument>(ejson);
14+
Console.WriteLine(document.ToJson());
15+
// end-read-ejson
16+
}
17+
18+
{
19+
// start-write-ejson
20+
var document = new MyDocument();
21+
document.Id = ObjectId.GenerateNewId();
22+
document.CreatedAt = DateTime.UtcNow;
23+
document.NumViews = 1234567890;
24+
25+
var json = document.ToJson(new JsonWriterSettings
26+
{
27+
OutputMode = JsonOutputMode.CanonicalExtendedJson
28+
});
29+
Console.WriteLine(json);
30+
// end-write-ejson
31+
}
32+
}
33+
}
34+
35+
// start-custom-class
36+
public class MyDocument
37+
{
38+
public ObjectId Id { get; set; }
39+
public DateTime CreatedAt { get; set; }
40+
public long NumViews { get; set; }
41+
}
42+
// end-custom-class

0 commit comments

Comments
 (0)