Skip to content
Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ corresponds to the format of the example you want to see:
.. code-block:: json

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

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

.. code-block:: java
.. io-code-block::

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

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

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

.. code-block:: none
:copyable: False
.. output::
:language: none
:visible: false

Document{{_id=507f1f77bcf86cd799439011, myNumber=4794261}}
Document{{_id=507f1f77bcf86cd799439011, myNumber=4794261}}

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

.. code-block:: java
.. io-code-block::

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

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

jsonReader.readStartDocument();
JsonReader jsonReader = new JsonReader(ejsonStr);

jsonReader.readName("_id");
ObjectId id = jsonReader.readObjectId();
jsonReader.readName("myNumber");
Long myNumber = jsonReader.readInt64();
jsonReader.readStartDocument();

jsonReader.readEndDocument();
jsonReader.readName("_id");
ObjectId id = jsonReader.readObjectId();
jsonReader.readName("myNumber");
Long myNumber = jsonReader.readInt64();

System.out.println(id + " is type: " + id.getClass().getName());
System.out.println(myNumber + " is type: " + myNumber.getClass().getName());
jsonReader.readEndDocument();

jsonReader.close();
System.out.println(id + " is type: " + id.getClass().getName());
System.out.println(myNumber + " is type: " + myNumber.getClass().getName());

The output of this code example should look something like this:
jsonReader.close();

.. code-block:: none
:copyable: False
.. output::
:language: none
:visible: false

507f1f77bcf86cd799439011 is type: org.bson.types.ObjectId
4794261 is type: java.lang.Long
507f1f77bcf86cd799439011 is type: org.bson.types.ObjectId
4794261 is type: java.lang.Long


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

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

.. code-block:: java
.. io-code-block::

Document myDoc = new Document();
myDoc.append("_id", new ObjectId("507f1f77bcf86cd799439012")).append("myNumber", 11223344);
.. input::
:language: java

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

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

.. code-block:: none
:copyable: false
.. output::
:language: none
:visible: false

{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": 11223344}
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": 11223344}

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

.. code-block:: java
.. io-code-block::

JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
.. input::
:language: java

try (JsonWriter jsonWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(System.out)), settings)) {
jsonWriter.writeStartDocument();
jsonWriter.writeObjectId("_id", new ObjectId("507f1f77bcf86cd799439012"));
jsonWriter.writeInt64("myNumber", 11223344);
jsonWriter.writeEndDocument();
jsonWriter.flush();
}
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();

The output of this code example should look something like this:
try (JsonWriter jsonWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(System.out)), settings)) {
jsonWriter.writeStartDocument();
jsonWriter.writeObjectId("_id", new ObjectId("507f1f77bcf86cd799439012"));
jsonWriter.writeInt64("myNumber", 11223344);
jsonWriter.writeEndDocument();
jsonWriter.flush();
}

.. code-block:: none
:copyable: false
.. output::
:language: none
:visible: false

{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": {"$numberLong": "11223344"}}
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "myNumber": {"$numberLong": "11223344"}}

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

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

The output of this code should look something like this:
The output of this code resembles the following text:

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

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

Without specifying the converters, the Relaxed mode JSON output should look something like this:
Without specifying the converters, the Relaxed mode JSON output resembles the following text:

.. code-block:: json
:copyable: false
Expand Down