@@ -31,29 +31,13 @@ The following example shows how you can read an Extended JSON string into a
3131array of hashes by using the ``parse()`` method:
3232
3333.. io-code-block::
34+ :copyable:
3435
35- .. input::
36- :language: ruby
37-
38- require 'bson'
39-
40- ex_json = '''[
41- {"foo": [1, 2]},
42- {"bar": {"hello": "world"}},
43- {"code": {
44- "$scope": {},
45- "$code": "function x() { return 1; }"
46- }},
47- {"bin": {
48- "$type": "80",
49- "$binary": "AQIDBA=="
50- }}
51- ]'''
52-
53- doc = BSON::ExtJSON.parse(ex_json)
54-
55- puts doc.class
56- puts doc
36+ .. input:: /includes/data-formats/extended-json.rb
37+ :start-after: start-ex-json-read
38+ :end-before: end-ex-json-read
39+ :language: ruby
40+ :dedent:
5741
5842 .. output::
5943 :language: none
@@ -68,47 +52,31 @@ Write Extended JSON
6852-------------------
6953
7054You can write an Extended JSON string from a hash by using the
71- ``as_extended_json()`` method. Using the Ruby ``map{}`` method, you can create
72- an Extended JSON string from an array of hashes. By default, this method returns
55+ ``as_extended_json()`` method. By default, this method returns
7356the Extended JSON string in canonical format, but you can specify relaxed or
74- legacy formats by passing a ``mode: `` argument.
57+ legacy formats by passing a ``mode`` argument.
7558
7659.. note:: Legacy Version
7760
7861 The legacy format option tells the {+driver-short+} to serialize BSON types
79- using MongoDB Extended JSON v1 format, which predates the current
62+ with the MongoDB Extended JSON v1 format, which predates the current
8063 relaxed and canonical formats.
8164
82- For more information see the :manual:`MongoDB Extended JSON v1 (legacy
83- format) </reference/mongodb-extended-json-v1/>` page in the Server manual.
65+ For more information see the :manual:`MongoDB Extended JSON v1
66+ </reference/mongodb-extended-json-v1/>` page in the Server manual.
8467
8568The following example outputs an Extended JSON string in the canonical, relaxed
86- and legacy formats:
69+ and legacy formats. It calls the ``as_extended_json()`` method from inside a
70+ Ruby ``map{}`` block to create the Extended JSON string from an array of hashes:
8771
8872.. io-code-block::
73+ :copyable:
8974
90- .. input::
91- :language: ruby
92-
93- require 'bson'
94-
95- hash_array = [
96- { "foo" => [1, 2] },
97- { "bin" => BSON::Binary.new("\x01\x02\x03\x04", :user) },
98- { "number" => BSON::Int64.new(42) }
99- ]
100-
101- json_array_canonical = hash_array.map(&:as_extended_json)
102- json_array_relaxed = hash_array.map{ |hash| hash.as_extended_json(mode: :relaxed) }
103- json_array_legacy = hash_array.map{ |hash| hash.as_extended_json(mode: :legacy) }
104-
105- json_string_canonical = JSON.generate(json_array_canonical)
106- json_string_relaxed = JSON.generate(json_array_relaxed)
107- json_string_legacy = JSON.generate(json_array_legacy)
108-
109- puts "canonical:\t #{json_string_canonical}"
110- puts "relaxed:\t #{json_string_relaxed}"
111- puts "legacy:\t\t #{json_string_legacy}"
75+ .. input:: /includes/data-formats/extended-json.rb
76+ :start-after: start-ex-json-write
77+ :end-before: end-ex-json-write
78+ :language: ruby
79+ :dedent:
11280
11381 .. output::
11482 :language: none
@@ -118,8 +86,8 @@ and legacy formats:
11886 relaxed: [{"foo":[1,2]},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"80"}}},{"number":42}]
11987 legacy: [{"foo":[1,2]},{"bin":{"$binary":"AQIDBA==","$type":"80"}},{"number":42}]
12088
121- More Information
122- ----------------
89+ Additional Information
90+ ----------------------
12391
12492For more information, see the following resources:
12593
@@ -130,5 +98,6 @@ API Documentation
13098- `#as_extended_json() <https://www.rubydoc.info/gems/bson/{+bson-version+}/BSON/Hash#as_extended_json-instance_method>`__
13199
132100Server Manual Pages
101+ ~~~~~~~~~~~~~~~~~~~
133102
134103- :manual:`MongoDB Extended JSON (v2)</reference/mongodb-extended-json/>`
0 commit comments