@@ -128,7 +128,7 @@ corresponds to the format of the example you want to see:
128
128
.. code-block:: json
129
129
130
130
{
131
- "_id: ": ObjectId("573a1391f29313caabcd9637"),
131
+ "_id": ObjectId("573a1391f29313caabcd9637"),
132
132
"createdAt": ISODate("2020-09-30T18:22:51.648Z"),
133
133
"numViews": NumberLong("36520312")
134
134
}
@@ -139,7 +139,7 @@ corresponds to the format of the example you want to see:
139
139
.. code-block:: json
140
140
141
141
{
142
- "_id: ": { "$oid": "573a1391f29313caabcd9637" },
142
+ "_id": { "$oid": "573a1391f29313caabcd9637" },
143
143
"createdAt": { "$date": 1601499609 },
144
144
"numViews": { "$numberLong": "36520312" }
145
145
}
@@ -161,20 +161,22 @@ The following example shows how you can use the ``Document`` class to read
161
161
an example Extended JSON string into a ``Document`` object using the
162
162
``parse()`` method:
163
163
164
- .. code-block:: java
164
+ .. io- code-block::
165
165
166
- String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
167
- "\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
166
+ .. input::
167
+ :language: java
168
168
169
- Document doc = Document.parse( ejsonStr);
170
- System.out.println(doc) ;
169
+ String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
170
+ "\"myNumber\": {\"$numberLong\": \"4794261\" }}}" ;
171
171
172
- The output of the preceding code should look something like this:
172
+ Document doc = Document.parse(ejsonStr);
173
+ System.out.println(doc);
173
174
174
- .. code-block:: none
175
- :copyable: False
175
+ .. output::
176
+ :language: none
177
+ :visible: false
176
178
177
- Document{{_id=507f1f77bcf86cd799439011, myNumber=4794261}}
179
+ Document{{_id=507f1f77bcf86cd799439011, myNumber=4794261}}
178
180
179
181
For more information, see our Fundamentals page
180
182
on :doc:`Documents </fundamentals/data-formats/documents>`.
@@ -191,34 +193,36 @@ The driver's document classes also use this class to parse Extended JSON.
191
193
The following code example shows how you can use the ``JsonReader`` class to convert
192
194
an Extended JSON string into Java objects:
193
195
194
- .. code-block:: java
196
+ .. io- code-block::
195
197
196
- String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
197
- "\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
198
+ .. input::
199
+ :language: java
198
200
199
- JsonReader jsonReader = new JsonReader(ejsonStr);
201
+ String ejsonStr = "{ \"_id\": { \"$oid\": \"507f1f77bcf86cd799439011\"}," +
202
+ "\"myNumber\": {\"$numberLong\": \"4794261\" }}}";
200
203
201
- jsonReader.readStartDocument( );
204
+ JsonReader jsonReader = new JsonReader(ejsonStr );
202
205
203
- jsonReader.readName("_id");
204
- ObjectId id = jsonReader.readObjectId();
205
- jsonReader.readName("myNumber");
206
- Long myNumber = jsonReader.readInt64();
206
+ jsonReader.readStartDocument();
207
207
208
- jsonReader.readEndDocument();
208
+ jsonReader.readName("_id");
209
+ ObjectId id = jsonReader.readObjectId();
210
+ jsonReader.readName("myNumber");
211
+ Long myNumber = jsonReader.readInt64();
209
212
210
- System.out.println(id + " is type: " + id.getClass().getName());
211
- System.out.println(myNumber + " is type: " + myNumber.getClass().getName());
213
+ jsonReader.readEndDocument();
212
214
213
- jsonReader.close();
215
+ System.out.println(id + " is type: " + id.getClass().getName());
216
+ System.out.println(myNumber + " is type: " + myNumber.getClass().getName());
214
217
215
- The output of this code example should look something like this:
218
+ jsonReader.close();
216
219
217
- .. code-block:: none
218
- :copyable: False
220
+ .. output::
221
+ :language: none
222
+ :visible: false
219
223
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
222
226
223
227
224
228
For more information, see the `JsonReader
@@ -236,20 +240,22 @@ instance of ``JsonWriterSettings`` to specify the Extended JSON format.
236
240
237
241
In this example, we output the Extended JSON in the Relaxed mode format.
238
242
239
- .. code-block:: java
243
+ .. io- code-block::
240
244
241
- Document myDoc = new Document();
242
- myDoc.append("_id", new ObjectId("507f1f77bcf86cd799439012")).append("myNumber", 11223344);
245
+ .. input::
246
+ :language: java
243
247
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 );
246
250
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));
248
253
249
- .. code-block:: none
250
- :copyable: false
254
+ .. output::
255
+ :language: none
256
+ :visible: false
251
257
252
- {"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": 11223344}
258
+ {"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": 11223344}
253
259
254
260
Using the BSON Library
255
261
~~~~~~~~~~~~~~~~~~~~~~
@@ -266,24 +272,26 @@ The following code example shows how you can use ``JsonWriter`` to create an
266
272
Extended JSON string and output it to ``System.out``. We specify the format
267
273
by passing the ``outputMode()`` builder method the ``JsonMode.EXTENDED`` constant:
268
274
269
- .. code-block:: java
275
+ .. io- code-block::
270
276
271
- JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
277
+ .. input::
278
+ :language: java
272
279
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();
280
281
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
+ }
282
289
283
- .. code-block:: none
284
- :copyable: false
290
+ .. output::
291
+ :language: none
292
+ :visible: false
285
293
286
- {"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": {"$numberLong": "11223344"}}
294
+ {"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": {"$numberLong": "11223344"}}
287
295
288
296
For more information about the methods and classes mentioned in this section,
289
297
see the following API Documentation:
@@ -321,14 +329,14 @@ expressions, to simplify the Relaxed mode JSON output.
321
329
322
330
System.out.println(doc.toJson(settings)));
323
331
324
- The output of this code should look something like this :
332
+ The output of this code resembles the following text :
325
333
326
334
.. code-block:: json
327
335
:copyable: false
328
336
329
337
{"_id": "507f1f77bcf86cd799439012", "createdAt": "2020-09-30T21:00:09Z", "myNumber": 4794261}
330
338
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 :
332
340
333
341
.. code-block:: json
334
342
:copyable: false
0 commit comments