Skip to content

Commit 52cf4c1

Browse files
[Ruby] Extended JSON (#168)
* DOCSP-51932: v2.21.2 patch release (#163) * shared content * write * link formatting * link formatting * NR feedback * NR feedback * JB feedback * rephrase * remove extra change --------- Co-authored-by: Nora Reidy <[email protected]>
1 parent 6eace4f commit 52cf4c1

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
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
--------
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. _ruby-extended-json:
2+
3+
=============
4+
Extended JSON
5+
=============
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, json, standard formatting
13+
:description: Learn how to use extended JSON in the MongoDB Ruby Driver.
14+
15+
.. contents:: On this page
16+
:local:
17+
:backlinks: none
18+
:depth: 2
19+
:class: twocols
20+
21+
.. sharedinclude:: dbx/extended-json.rst
22+
23+
Read Extended JSON
24+
------------------
25+
26+
You can read an Extended JSON string into an Ruby array by calling
27+
the ``BSON::ExtJSON.parse`` method. This method parses an Extended
28+
JSON string and returns an array containing the data.
29+
30+
The following example shows how you can read an Extended JSON string into a
31+
array of hashes by using the ``parse`` method:
32+
33+
.. io-code-block::
34+
:copyable:
35+
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:
41+
42+
.. output::
43+
:language: none
44+
:visible: false
45+
46+
{"foo" => [1, 2]}
47+
{"bar" => {"hello" => "world"}}
48+
{"code" => #<BSON::CodeWithScope:0x0000000123f398e0 @javascript="function x() { return 1; }", @scope={}>}
49+
{"bin" => <BSON::Binary:0x7144 type=user data=0x01020304...>}
50+
51+
Write Extended JSON
52+
-------------------
53+
54+
You can write an Extended JSON string by using the ``as_extended_json``
55+
method. By default, this method returns the Extended JSON string in canonical
56+
format, but you can specify relaxed or legacy formats by passing a ``mode`` argument.
57+
58+
.. note:: Legacy Version
59+
60+
The legacy format option tells the {+driver-short+} to serialize BSON types
61+
with the MongoDB Extended JSON v1 format, which predates the current
62+
relaxed and canonical formats.
63+
64+
For more information see, the :manual:`MongoDB Extended JSON v1
65+
</reference/mongodb-extended-json-v1/>` page in the Server manual.
66+
67+
The ``as_extended_json`` method is available for several core and standard
68+
library types, including ``Array`` and ``Hash``. The following example creates
69+
Extended JSON strings in the canonical, relaxed, and legacy formats, from an
70+
array of hashes:
71+
72+
.. io-code-block::
73+
:copyable:
74+
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:
80+
81+
.. output::
82+
:language: none
83+
:visible: false
84+
85+
canonical: [{"foo":[{"$numberInt":"1"},{"$numberInt":"2"}]},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"80"}}},{"number":{"$numberLong":"42"}}]
86+
relaxed: [{"foo":[1,2]},{"bin":{"$binary":{"base64":"AQIDBA==","subType":"80"}}},{"number":42}]
87+
legacy: [{"foo":[1,2]},{"bin":{"$binary":"AQIDBA==","$type":"80"}},{"number":42}]
88+
89+
Additional Information
90+
----------------------
91+
92+
For more information, see the following resources:
93+
94+
API Documentation
95+
~~~~~~~~~~~~~~~~~
96+
97+
- `BSON::ExtJSON.parse <https://www.rubydoc.info/gems/bson/{+bson-version+}/BSON/ExtJSON#parse-class_method>`__
98+
- `#as_extended_json <https://www.rubydoc.info/gems/bson/{+bson-version+}/BSON/Array#as_extended_json-instance_method>`__
99+
100+
Server Manual Pages
101+
~~~~~~~~~~~~~~~~~~~
102+
103+
- :manual:`MongoDB Extended JSON (v2)</reference/mongodb-extended-json/>`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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_string_canonical = hash_array.as_extended_json
33+
json_string_relaxed = hash_array.as_extended_json(mode: :relaxed)
34+
json_string_legacy = hash_array.as_extended_json(mode: :legacy)
35+
36+
puts "canonical:\t #{json_string_canonical}"
37+
puts "relaxed:\t #{json_string_relaxed}"
38+
puts "legacy:\t\t #{json_string_legacy}"
39+
# end-ex-json-write

0 commit comments

Comments
 (0)