@@ -228,10 +228,11 @@ will have a ``to_bson`` method defined for them are: ``Object``, ``Array``,
228228In addition to the core Ruby objects, BSON also provides some special types
229229specific to the specification:
230230
231+
231232``BSON::Binary``
232233````````````````
233234
234- This is a representation of binary data. The raw data and a subtype must be
235+ This is a representation of binary data. The raw data and the subtype must be
235236provided when constructing.
236237
237238.. code-block:: ruby
@@ -241,6 +242,106 @@ provided when constructing.
241242Valid subtypes are: ``:generic``, ``:function``, ``:old``, ``:uuid_old``,
242243``:uuid``, ``:md5``, ``:user``.
243244
245+ UUID Methods
246+ ~~~~~~~~~~~~
247+
248+ To create a UUID BSON::Binary (binary subtype 4) from its RFC 4122-compliant
249+ string representation, use the ``from_uuid`` method:
250+
251+ .. code-block:: ruby
252+
253+ uuid_str = "00112233-4455-6677-8899-aabbccddeeff"
254+ BSON::Binary.from_uuid(uuid_str)
255+ # => <BSON::Binary:0x46986653612880 type=uuid data=0x0011223344556677...>
256+
257+ To stringify a UUID BSON::Binary to an RFC 4122-compliant representation,
258+ use the ``to_uuid`` method:
259+
260+ .. code-block:: ruby
261+
262+ binary = BSON::Binary.new("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF".force_encoding('BINARY'), :uuid)
263+ => <BSON::Binary:0x46942046606480 type=uuid data=0x0011223344556677...>
264+ binary.to_uuid
265+ => "00112233-4455-6677-8899aabbccddeeff"
266+
267+ The standard representation may be explicitly specified when invoking both
268+ ``from_uuid`` and ``to_uuid`` methods:
269+
270+ .. code-block:: ruby
271+
272+ binary = BSON::Binary.from_uuid(uuid_str, :standard)
273+ binary.to_uuid(:standard)
274+
275+ Note that the ``:standard`` representation can only be used with a Binary
276+ of subtype ``:uuid`` (not ``:uuid_old``).
277+
278+ Legacy UUIDs
279+ ~~~~~~~~~~~~
280+
281+ Data stored in BSON::Binary objects of subtype 3 (``:uuid_old``) may be
282+ persisted in one of three different byte orders depending on which driver
283+ created the data. The byte orders are CSharp legacy, Java legacy and Python
284+ legacy. The Python legacy byte order is the same as the standard RFC 4122
285+ byte order; CSharp legacy and Java legacy byte orders have some of the bytes
286+ swapped.
287+
288+ The Binary object containing a legacy UUID does not encode *which* format
289+ the UUID is stored in. Therefore, methods that convert to and from the legacy
290+ UUID format take the desired format, or representation, as their argument.
291+ An application may copy legacy UUID Binary objects without knowing which byte
292+ order they store their data in.
293+
294+ The following methods for working with legacy UUIDs are provided for
295+ interoperability with existing deployments storing data in legacy UUID formats.
296+ It is recommended that new applications use the ``:uuid`` (subtype 4) format
297+ only, which is compliant with RFC 4122.
298+
299+ To stringify a legacy UUID BSON::Binary, use the ``to_uuid`` method specifying
300+ the desired representation. Accepted representations are ``:csharp_legacy``,
301+ ``:java_legacy`` and ``:python_legacy``. Note that a legacy UUID BSON::Binary
302+ cannot be stringified without specifying a representation.
303+
304+ .. code-block:: ruby
305+
306+ binary = BSON::Binary.new("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF".force_encoding('BINARY'), :uuid_old)
307+ => <BSON::Binary:0x46942046606480 type=uuid data=0x0011223344556677...>
308+
309+ binary.to_uuid
310+ # => ArgumentError (Representation must be specified for BSON::Binary objects of type :uuid_old)
311+
312+ binary.to_uuid(:csharp_legacy)
313+ # => "33221100-5544-7766-8899aabbccddeeff"
314+
315+ binary.to_uuid(:java_legacy)
316+ # => "77665544-3322-1100-ffeeddccbbaa9988"
317+
318+ binary.to_uuid(:python_legacy)
319+ # => "00112233-4455-6677-8899aabbccddeeff"
320+
321+ To create a legacy UUID BSON::Binary from the string representation of the
322+ UUID, use the ``from_uuid`` method specifying the desired representation:
323+
324+ .. code-block:: ruby
325+
326+ uuid_str = "00112233-4455-6677-8899-aabbccddeeff"
327+
328+ BSON::Binary.from_uuid(uuid_str, :csharp_legacy)
329+ # => <BSON::Binary:0x46986653650480 type=uuid_old data=0x3322110055447766...>
330+
331+ BSON::Binary.from_uuid(uuid_str, :java_legacy)
332+ # => <BSON::Binary:0x46986653663960 type=uuid_old data=0x7766554433221100...>
333+
334+ BSON::Binary.from_uuid(uuid_str, :python_legacy)
335+ # => <BSON::Binary:0x46986653686300 type=uuid_old data=0x0011223344556677...>
336+
337+ These methods can be used to convert from one representation to another:
338+
339+ .. code-block:: ruby
340+
341+ BSON::Binary.from_uuid('77665544-3322-1100-ffeeddccbbaa9988',:java_legacy).to_uuid(:csharp_legacy)
342+ # => "33221100-5544-7766-8899aabbccddeeff"
343+
344+
244345``BSON::Code``
245346``````````````
246347
0 commit comments