|
| 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/>` |
0 commit comments