Skip to content

Commit 84a2569

Browse files
author
Chris Cho
committed
DOCSP-33241: fix extended JSON example id field (#447)
* DOCSP-33241: fix extended JSON example id field (cherry picked from commit f4e1889)
1 parent 1c6effb commit 84a2569

File tree

1 file changed

+61
-53
lines changed

1 file changed

+61
-53
lines changed

source/fundamentals/data-formats/document-data-format-extended-json.txt

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ corresponds to the format of the example you want to see:
128128
.. code-block:: json
129129

130130
{
131-
"_id:": ObjectId("573a1391f29313caabcd9637"),
131+
"_id": ObjectId("573a1391f29313caabcd9637"),
132132
"createdAt": ISODate("2020-09-30T18:22:51.648Z"),
133133
"numViews": NumberLong("36520312")
134134
}
@@ -139,7 +139,7 @@ corresponds to the format of the example you want to see:
139139
.. code-block:: json
140140

141141
{
142-
"_id:": { "$oid": "573a1391f29313caabcd9637" },
142+
"_id": { "$oid": "573a1391f29313caabcd9637" },
143143
"createdAt": { "$date": 1601499609 },
144144
"numViews": { "$numberLong": "36520312" }
145145
}
@@ -161,20 +161,22 @@ The following example shows how you can use the ``Document`` class to read
161161
an example Extended JSON string into a ``Document`` object using the
162162
``parse()`` method:
163163

164-
.. code-block:: java
164+
.. io-code-block::
165165

166-
String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
167-
"\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
166+
.. input::
167+
:language: java
168168

169-
Document doc = Document.parse(ejsonStr);
170-
System.out.println(doc);
169+
String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
170+
"\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
171171

172-
The output of the preceding code should look something like this:
172+
Document doc = Document.parse(ejsonStr);
173+
System.out.println(doc);
173174

174-
.. code-block:: none
175-
:copyable: False
175+
.. output::
176+
:language: none
177+
:visible: false
176178

177-
Document{{_id=507f1f77bcf86cd799439011, myNumber=4794261}}
179+
Document{{_id=507f1f77bcf86cd799439011, myNumber=4794261}}
178180

179181
For more information, see our Fundamentals page
180182
on :doc:`Documents </fundamentals/data-formats/documents>`.
@@ -191,34 +193,36 @@ The driver's document classes also use this class to parse Extended JSON.
191193
The following code example shows how you can use the ``JsonReader`` class to convert
192194
an Extended JSON string into Java objects:
193195

194-
.. code-block:: java
196+
.. io-code-block::
195197

196-
String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
197-
"\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
198+
.. input::
199+
:language: java
198200

199-
JsonReader jsonReader = new JsonReader(ejsonStr);
201+
String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
202+
"\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
200203

201-
jsonReader.readStartDocument();
204+
JsonReader jsonReader = new JsonReader(ejsonStr);
202205

203-
jsonReader.readName("_id");
204-
ObjectId id = jsonReader.readObjectId();
205-
jsonReader.readName("myNumber");
206-
Long myNumber = jsonReader.readInt64();
206+
jsonReader.readStartDocument();
207207

208-
jsonReader.readEndDocument();
208+
jsonReader.readName("_id");
209+
ObjectId id = jsonReader.readObjectId();
210+
jsonReader.readName("myNumber");
211+
Long myNumber = jsonReader.readInt64();
209212

210-
System.out.println(id + " is type: " + id.getClass().getName());
211-
System.out.println(myNumber + " is type: " + myNumber.getClass().getName());
213+
jsonReader.readEndDocument();
212214

213-
jsonReader.close();
215+
System.out.println(id + " is type: " + id.getClass().getName());
216+
System.out.println(myNumber + " is type: " + myNumber.getClass().getName());
214217

215-
The output of this code example should look something like this:
218+
jsonReader.close();
216219

217-
.. code-block:: none
218-
:copyable: False
220+
.. output::
221+
:language: none
222+
:visible: false
219223

220-
507f1f77bcf86cd799439011 is type: org.bson.types.ObjectId
221-
4794261 is type: java.lang.Long
224+
507f1f77bcf86cd799439011 is type: org.bson.types.ObjectId
225+
4794261 is type: java.lang.Long
222226

223227

224228
For more information, see the `JsonReader
@@ -236,20 +240,22 @@ instance of ``JsonWriterSettings`` to specify the Extended JSON format.
236240

237241
In this example, we output the Extended JSON in the Relaxed mode format.
238242

239-
.. code-block:: java
243+
.. io-code-block::
240244

241-
Document myDoc = new Document();
242-
myDoc.append("_id", new ObjectId("507f1f77bcf86cd799439012")).append("myNumber", 11223344);
245+
.. input::
246+
:language: java
243247

244-
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
245-
System.out.println(doc.toJson(settings));
248+
Document myDoc = new Document();
249+
myDoc.append("_id", new ObjectId("507f1f77bcf86cd799439012")).append("myNumber", 11223344);
246250

247-
The output of this code example should look something like this:
251+
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
252+
System.out.println(doc.toJson(settings));
248253

249-
.. code-block:: none
250-
:copyable: false
254+
.. output::
255+
:language: none
256+
:visible: false
251257

252-
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": 11223344}
258+
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": 11223344}
253259

254260
Using the BSON Library
255261
~~~~~~~~~~~~~~~~~~~~~~
@@ -266,24 +272,26 @@ The following code example shows how you can use ``JsonWriter`` to create an
266272
Extended JSON string and output it to ``System.out``. We specify the format
267273
by passing the ``outputMode()`` builder method the ``JsonMode.EXTENDED`` constant:
268274

269-
.. code-block:: java
275+
.. io-code-block::
270276

271-
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
277+
.. input::
278+
:language: java
272279

273-
try (JsonWriter jsonWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(System.out)), settings)) {
274-
jsonWriter.writeStartDocument();
275-
jsonWriter.writeObjectId("_id", new ObjectId("507f1f77bcf86cd799439012"));
276-
jsonWriter.writeInt64("myNumber", 11223344);
277-
jsonWriter.writeEndDocument();
278-
jsonWriter.flush();
279-
}
280+
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
280281

281-
The output of this code example should look something like this:
282+
try (JsonWriter jsonWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(System.out)), settings)) {
283+
jsonWriter.writeStartDocument();
284+
jsonWriter.writeObjectId("_id", new ObjectId("507f1f77bcf86cd799439012"));
285+
jsonWriter.writeInt64("myNumber", 11223344);
286+
jsonWriter.writeEndDocument();
287+
jsonWriter.flush();
288+
}
282289

283-
.. code-block:: none
284-
:copyable: false
290+
.. output::
291+
:language: none
292+
:visible: false
285293

286-
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": {"$numberLong": "11223344"}}
294+
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": {"$numberLong": "11223344"}}
287295

288296
For more information about the methods and classes mentioned in this section,
289297
see the following API Documentation:
@@ -321,14 +329,14 @@ expressions, to simplify the Relaxed mode JSON output.
321329

322330
System.out.println(doc.toJson(settings)));
323331

324-
The output of this code should look something like this:
332+
The output of this code resembles the following text:
325333

326334
.. code-block:: json
327335
:copyable: false
328336

329337
{"_id": "507f1f77bcf86cd799439012", "createdAt": "2020-09-30T21:00:09Z", "myNumber": 4794261}
330338

331-
Without specifying the converters, the Relaxed mode JSON output should look something like this:
339+
Without specifying the converters, the Relaxed mode JSON output resembles the following text:
332340

333341
.. code-block:: json
334342
:copyable: false

0 commit comments

Comments
 (0)