Skip to content

Commit 1335359

Browse files
committed
NR feedback
1 parent c50faa4 commit 1335359

File tree

3 files changed

+66
-53
lines changed

3 files changed

+66
-53
lines changed

source/data-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Data Formats
2424

2525
Time Series Data </data-formats/time-series>
2626
BSON </data-formats/bson>
27+
Extended JSON </data-formats/extended-json>
2728

2829
Overview
2930
--------

source/data-formats/extended-json.txt

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,13 @@ The following example shows how you can read an Extended JSON string into a
3131
array 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

7054
You 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
7356
the 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

8568
The 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

12492
For 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

132100
Server Manual Pages
101+
~~~~~~~~~~~~~~~~~~~
133102

134103
- :manual:`MongoDB Extended JSON (v2)</reference/mongodb-extended-json/>`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# start-ex-json-read
2+
require 'bson'
3+
4+
ex_json = '''[
5+
{"foo": [1, 2]},
6+
{"bar": {"hello": "world"}},
7+
{"code": {
8+
"$scope": {},
9+
"$code": "function x() { return 1; }"
10+
}},
11+
{"bin": {
12+
"$type": "80",
13+
"$binary": "AQIDBA=="
14+
}}
15+
]'''
16+
17+
doc = BSON::ExtJSON.parse(ex_json)
18+
19+
puts doc.class
20+
puts doc
21+
# end-ex-json-read
22+
23+
# start-ex-json-write
24+
require 'bson'
25+
26+
hash_array = [
27+
{ "foo" => [1, 2] },
28+
{ "bin" => BSON::Binary.new("\x01\x02\x03\x04", :user) },
29+
{ "number" => BSON::Int64.new(42) }
30+
]
31+
32+
json_array_canonical = hash_array.map(&:as_extended_json)
33+
json_array_relaxed = hash_array.map{ |hash| hash.as_extended_json(mode: :relaxed) }
34+
json_array_legacy = hash_array.map{ |hash| hash.as_extended_json(mode: :legacy) }
35+
36+
json_string_canonical = JSON.generate(json_array_canonical)
37+
json_string_relaxed = JSON.generate(json_array_relaxed)
38+
json_string_legacy = JSON.generate(json_array_legacy)
39+
40+
puts "canonical:\t #{json_string_canonical}"
41+
puts "relaxed:\t #{json_string_relaxed}"
42+
puts "legacy:\t\t #{json_string_legacy}"
43+
# end-ex-json-write

0 commit comments

Comments
 (0)