File Coverage

blib/lib/BSON.pm
Criterion Covered Total %
statement 219 313 69.9
branch 116 214 54.2
condition 39 85 45.8
subroutine 27 32 84.3
pod 9 10 90.0
total 410 654 62.6


line stmt bran cond sub pod time code
1 71     71   6324164 use 5.010001;
  71         637  
2 71     71   349 use strict;
  71         145  
  71         1396  
3 71     71   314 use warnings;
  71         121  
  71         2613  
4              
5             package BSON;
6             # ABSTRACT: BSON serialization and deserialization
7              
8 71     71   357 use base 'Exporter';
  71         129  
  71         10234  
9             our @EXPORT_OK = qw/encode decode/;
10              
11 71     71   836 use version;
  71         152065  
  71         357  
12             our $VERSION = 'v1.12.1';
13              
14 71     71   5767 use Carp;
  71         141  
  71         4063  
15 71     71   394 use Config;
  71         123  
  71         2432  
16 71     71   353 use Scalar::Util qw/blessed looks_like_number/;
  71         128  
  71         3495  
17              
18 71     71   663 use Moo 2.002004; # safer generated code
  71         777856  
  71         492  
19 71     71   22382 use boolean;
  71         131234  
  71         762  
20 71     71   4794 use BSON::OID;
  71         289  
  71         4559  
21              
22             use constant {
23             HAS_INT64 => $Config{use64bitint},
24             HAS_LD => $Config{uselongdouble},
25 71     71   636 };
  71         169  
  71         11617  
26              
27 71     71   814 use if !HAS_INT64, "Math::BigInt";
  71         42336  
  71         396  
28              
29             my $bools_re = qr/::(?:Boolean|_Bool|Bool)\z/;
30              
31 71     71   5683 use namespace::clean -except => 'meta';
  71         193  
  71         582  
32              
33             my $max_int32 = 2147483647;
34              
35             # Dependency-free equivalent of what we need from Module::Runtime
36             sub _try_load {
37 141     141   344 my ( $mod, $ver ) = @_;
38 141         776 ( my $file = "$mod.pm" ) =~ s{::}{/}g;
39 141 50       265 my $load = eval { require $file; $mod->VERSION($ver) if defined $ver; 1 };
  141         1289  
  71         485  
  71         233  
40 141 100       63847 delete $INC{$file} if !$load; # for old, broken perls
41 141 100       853 die $@ if !$load;
42 71         508 return 1;
43             }
44              
45             BEGIN {
46 71     71   55179 my ($class, @errs);
47 71 100 33     426 if ( $class = $ENV{PERL_BSON_BACKEND} ) {
    50 33        
    50          
48 1         3 eval { _try_load($class) };
  1         2  
49 1 50       4 if ( my $err = $@ ) {
50 0         0 $err =~ s{ at \S+ line .*}{};
51 0         0 die "Error: PERL_BSON_BACKEND '$class' could not be loaded: $err\n";
52             }
53 1 50 33     27 unless ($class->can("_encode_bson") && $class->can("_decode_bson") ) {
54 0         0 die "Error: PERL_BSON_BACKEND '$class' does not implement the correct API.\n";
55             }
56             }
57 70         230 elsif ( eval { _try_load( $class = "BSON::XS" ) } or do { push @errs, $@; 0 } ) {
  70         191  
  70         243  
58             # module loaded; nothing else to do
59             }
60 70         197 elsif ( eval { _try_load( $class = "BSON::PP" ) } or do { push @errs, $@; 0 } ) {
  0         0  
  0         0  
61             # module loaded; nothing else to do
62             }
63             else {
64 0         0 s/\n/ /g for @errs;
65 0         0 die join( "\n* ", "Error: Couldn't load a BSON backend:", @errs ) . "\n";
66             }
67              
68 71         912 *_encode_bson = $class->can("_encode_bson");
69 71         380 *_decode_bson = $class->can("_decode_bson");
70 71     0   2350 *_backend_class = sub { $class }; # for debugging
  0         0  
71             }
72              
73             # LOAD AFTER XS/PP, so that modules can pick up right version of helpers
74 71     71   623 use BSON::Types (); # loads types for extjson inflation
  71         126  
  71         275501  
75              
76             #--------------------------------------------------------------------------#
77             # public attributes
78             #--------------------------------------------------------------------------#
79              
80             #pod =attr error_callback
81             #pod
82             #pod This attribute specifies a function reference that will be called with
83             #pod three positional arguments:
84             #pod
85             #pod =for :list
86             #pod * an error string argument describing the error condition
87             #pod * a reference to the problematic document or byte-string
88             #pod * the method in which the error occurred (e.g. C or C)
89             #pod
90             #pod Note: for decoding errors, the byte-string is passed as a reference to avoid
91             #pod copying possibly large strings.
92             #pod
93             #pod If not provided, errors messages will be thrown with C.
94             #pod
95             #pod =cut
96              
97             has error_callback => (
98             is => 'ro',
99             isa => sub { die "not a code reference" if defined $_[0] && ! ref $_[0] eq 'CODE' },
100             );
101              
102             #pod =attr invalid_chars
103             #pod
104             #pod A string containing ASCII characters that must not appear in keys. The default
105             #pod is the empty string, meaning there are no invalid characters.
106             #pod
107             #pod =cut
108              
109             has invalid_chars => (
110             is => 'ro',
111             isa => sub { die "not a string" if ! defined $_[0] || ref $_[0] },
112             );
113              
114             #pod =attr max_length
115             #pod
116             #pod This attribute defines the maximum document size. The default is 0, which
117             #pod disables any maximum.
118             #pod
119             #pod If set to a positive number, it applies to both encoding B decoding (the
120             #pod latter is necessary for prevention of resource consumption attacks).
121             #pod
122             #pod =cut
123              
124             has max_length => (
125             is => 'ro',
126             isa => sub { die "not a non-negative number" unless defined $_[0] && $_[0] >= 0 },
127             );
128              
129             #pod =attr op_char
130             #pod
131             #pod This is a single character to use for special MongoDB-specific query
132             #pod operators. If a key starts with C, the C character will
133             #pod be replaced with "$".
134             #pod
135             #pod The default is "$", meaning that no replacement is necessary.
136             #pod
137             #pod =cut
138              
139             has op_char => (
140             is => 'ro',
141             isa => sub { die "not a single character" if defined $_[0] && length $_[0] > 1 },
142             );
143              
144             #pod =attr ordered
145             #pod
146             #pod If set to a true value, then decoding will return a reference to a tied
147             #pod hash that preserves key order. Otherwise, a regular (unordered) hash
148             #pod reference will be returned.
149             #pod
150             #pod B:
151             #pod
152             #pod =for :list
153             #pod * When 'ordered' is true, users must not rely on the return value being any
154             #pod particular tied hash implementation. It may change in the future for
155             #pod efficiency.
156             #pod * Turning this option on entails a significant speed penalty as tied hashes
157             #pod are slower than regular Perl hashes.
158             #pod
159             #pod The default is false.
160             #pod
161             #pod =cut
162              
163             has ordered => (
164             is => 'ro',
165             );
166              
167             #pod =attr prefer_numeric
168             #pod
169             #pod When false, scalar values will be encoded as a number if they were
170             #pod originally a number or were ever used in a numeric context. However, a
171             #pod string that looks like a number but was never used in a numeric context
172             #pod (e.g. "42") will be encoded as a string.
173             #pod
174             #pod If C is set to true, the encoder will attempt to coerce
175             #pod strings that look like a number into a numeric value. If the string
176             #pod doesn't look like a double or integer, it will be encoded as a string.
177             #pod
178             #pod B: the heuristics for determining whether something is a
179             #pod string or number are less accurate on older Perls. See L
180             #pod for wrapper classes that specify exact serialization types.
181             #pod
182             #pod The default is false.
183             #pod
184             #pod =cut
185              
186             has prefer_numeric => (
187             is => 'ro',
188             );
189              
190             #pod =attr wrap_dbrefs
191             #pod
192             #pod If set to true, during decoding, documents with the fields C<'$id'> and
193             #pod C<'$ref'> (literal dollar signs, not variables) will be wrapped as
194             #pod L objects. If false, they are decoded into ordinary hash
195             #pod references (or ordered hashes, if C is true).
196             #pod
197             #pod The default is true.
198             #pod
199             #pod =cut
200              
201             has wrap_dbrefs => (
202             is => 'ro',
203             );
204              
205             #pod =attr wrap_numbers
206             #pod
207             #pod If set to true, during decoding, numeric values will be wrapped into
208             #pod BSON type-wrappers: L, L or L.
209             #pod While very slow, this can help ensure fields can round-trip if unmodified.
210             #pod
211             #pod The default is false.
212             #pod
213             #pod =cut
214              
215             has wrap_numbers => (
216             is => 'ro',
217             );
218              
219             #pod =attr wrap_strings
220             #pod
221             #pod If set to true, during decoding, string values will be wrapped into a BSON
222             #pod type-wrappers, L. While very slow, this can help ensure
223             #pod fields can round-trip if unmodified.
224             #pod
225             #pod The default is false.
226             #pod
227             #pod =cut
228              
229             has wrap_strings => (
230             is => 'ro',
231             );
232              
233             #pod =attr dt_type (Discouraged)
234             #pod
235             #pod Sets the type of object which is returned for BSON DateTime fields. The
236             #pod default is C, which returns objects of type L. This is
237             #pod overloaded to be the integer epoch value when used as a number or string,
238             #pod so is somewhat backwards compatible with C in the L
239             #pod driver.
240             #pod
241             #pod Other acceptable values are L (explicitly), L,
242             #pod L, L, L.
243             #pod
244             #pod Because BSON::Time objects have methods to convert to DateTime,
245             #pod Time::Moment or DateTime::Tiny, use of this field is discouraged. Users
246             #pod should use these methods on demand. This option is provided for backwards
247             #pod compatibility only.
248             #pod
249             #pod =cut
250              
251             has dt_type => (
252             is => 'ro',
253             isa => sub { return if !defined($_[0]); die "not a string" if ref $_[0] },
254             );
255              
256             sub BUILD {
257 761     761 0 3910075 my ($self) = @_;
258 761 50       3345 $self->{wrap_dbrefs} = 1 unless defined $self->{wrap_dbrefs};
259 761 100       5368 $self->{invalid_chars} = "" unless defined $self->{invalid_chars};
260             }
261              
262             #--------------------------------------------------------------------------#
263             # public methods
264             #--------------------------------------------------------------------------#
265              
266             #pod =method encode_one
267             #pod
268             #pod $byte_string = $codec->encode_one( $doc );
269             #pod $byte_string = $codec->encode_one( $doc, \%options );
270             #pod
271             #pod Takes a "document", typically a hash reference, an array reference, or a
272             #pod Tie::IxHash object and returns a byte string with the BSON representation of
273             #pod the document.
274             #pod
275             #pod An optional hash reference of options may be provided. Valid options include:
276             #pod
277             #pod =for :list
278             #pod * first_key – if C is defined, it and C
279             #pod will be encoded first in the output BSON; any matching key found in the
280             #pod document will be ignored.
281             #pod * first_value - value to assign to C; will encode as Null if omitted
282             #pod * error_callback – overrides codec default
283             #pod * invalid_chars – overrides codec default
284             #pod * max_length – overrides codec default
285             #pod * op_char – overrides codec default
286             #pod * prefer_numeric – overrides codec default
287             #pod
288             #pod =cut
289              
290             sub encode_one {
291 2485     2485 1 60265 my ( $self, $document, $options ) = @_;
292 2485         4911 my $type = ref($document);
293              
294 2485 100       5658 Carp::croak "Can't encode scalars" unless $type;
295             # qr// is blessed to 'Regexp';
296 2484 100 100     21598 if ( $type eq "Regexp"
      100        
297             || !( blessed($document) || $type eq 'HASH' || $type eq 'ARRAY' ) )
298             {
299 2         253 Carp::croak "Can't encode non-container of type '$type'";
300             }
301              
302 2482 100       6015 $document = BSON::Doc->new(@$document)
303             if $type eq 'ARRAY';
304              
305 2482 100       13589 my $merged_opts = { %$self, ( $options ? %$options : () ) };
306              
307 2482         4603 my $bson = eval { _encode_bson( $document, $merged_opts ) };
  2482         8451  
308             # XXX this is a late max_length check -- it should be checked during
309             # encoding after each key
310 2482 100 66     12950 if ( $@ or ( $merged_opts->{max_length} && length($bson) > $merged_opts->{max_length} ) ) {
      100        
311 12   66     35 my $msg = $@ || "Document exceeds maximum size $merged_opts->{max_length}";
312 12 50       35 if ( $merged_opts->{error_callback} ) {
313 0         0 $merged_opts->{error_callback}->( $msg, $document, 'encode_one' );
314             }
315             else {
316 12         1214 Carp::croak("During encode_one, $msg");
317             }
318             }
319              
320 2470         13676 return $bson;
321             }
322              
323             #pod =method decode_one
324             #pod
325             #pod $doc = $codec->decode_one( $byte_string );
326             #pod $doc = $codec->decode_one( $byte_string, \%options );
327             #pod
328             #pod Takes a byte string with a BSON-encoded document and returns a
329             #pod hash reference representing the decoded document.
330             #pod
331             #pod An optional hash reference of options may be provided. Valid options include:
332             #pod
333             #pod =for :list
334             #pod * dt_type – overrides codec default
335             #pod * error_callback – overrides codec default
336             #pod * max_length – overrides codec default
337             #pod * ordered - overrides codec default
338             #pod * wrap_dbrefs - overrides codec default
339             #pod * wrap_numbers - overrides codec default
340             #pod * wrap_strings - overrides codec default
341             #pod
342             #pod =cut
343              
344             sub decode_one {
345 2148     2148 1 1324740 my ( $self, $string, $options ) = @_;
346              
347 2148 100       12736 my $merged_opts = { %$self, ( $options ? %$options : () ) };
348              
349 2148 100 66     7417 if ( $merged_opts->{max_length} && length($string) > $merged_opts->{max_length} ) {
350 1         4 my $msg = "Document exceeds maximum size $merged_opts->{max_length}";
351 1 50       4 if ( $merged_opts->{error_callback} ) {
352 0         0 $merged_opts->{error_callback}->( $msg, \$string, 'decode_one' );
353             }
354             else {
355 1         92 Carp::croak("During decode_one, $msg");
356             }
357             }
358              
359 2147         3336 my $document = eval { _decode_bson( $string, $merged_opts ) };
  2147         7121  
360 2147 100       7808 if ( $@ ) {
361 77 100       206 if ( $merged_opts->{error_callback} ) {
362 1         5 $merged_opts->{error_callback}->( $@, \$string, 'decode_one' );
363             }
364             else {
365 76         7865 Carp::croak("During decode_one, $@");
366             }
367             }
368              
369 2071         8947 return $document;
370             }
371              
372             #pod =method clone
373             #pod
374             #pod $copy = $codec->clone( ordered => 1 );
375             #pod
376             #pod Constructs a copy of the original codec, but allows changing
377             #pod attributes in the copy.
378             #pod
379             #pod =cut
380              
381             sub clone {
382 0     0 1 0 my ($self, @args) = @_;
383 0         0 my $class = ref($self);
384 0 0 0     0 if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) {
385 0         0 return $class->new( %$self, %{$args[0]} );
  0         0  
386             }
387              
388 0         0 return $class->new( %$self, @args );
389             }
390              
391              
392             #--------------------------------------------------------------------------#
393             # public class methods
394             #--------------------------------------------------------------------------#
395              
396             #pod =method create_oid
397             #pod
398             #pod $oid = BSON->create_oid;
399             #pod
400             #pod This class method returns a new L. This abstracts OID
401             #pod generation away from any specific Object ID class and makes it an interface
402             #pod on a BSON codec. Alternative BSON codecs should define a similar class
403             #pod method that returns an Object ID of whatever type is appropriate.
404             #pod
405             #pod =cut
406              
407 3     3 1 2564 sub create_oid { return BSON::OID->new }
408              
409             #pod =method inflate_extjson (DEPRECATED)
410             #pod
411             #pod This legacy method does not follow the L
412             #pod specification.
413             #pod
414             #pod Use L instead.
415             #pod
416             #pod =cut
417              
418             sub inflate_extjson {
419 0     0 1 0 my ( $self, $hash ) = @_;
420              
421 0         0 for my $k ( keys %$hash ) {
422 0         0 my $v = $hash->{$k};
423 0 0       0 if ( substr( $k, 0, 1 ) eq '$' ) {
424 0         0 croak "Dollar-prefixed key '$k' is not legal in top-level hash";
425             }
426 0         0 my $type = ref($v);
427 0 0       0 $hash->{$k} =
    0          
    0          
    0          
428             $type eq 'HASH' ? $self->_inflate_hash($v)
429             : $type eq 'ARRAY' ? $self->_inflate_array($v)
430             : $type =~ $bools_re ? ( $v ? true : false )
431             : $v;
432             }
433              
434 0         0 return $hash;
435             }
436              
437             #pod =method perl_to_extjson
438             #pod
439             #pod use JSON::MaybeXS;
440             #pod my $ext = BSON->perl_to_extjson($data, \%options);
441             #pod my $json = encode_json($ext);
442             #pod
443             #pod Takes a perl data structure (i.e. hashref) and turns it into an
444             #pod L
445             #pod structure. Note that the structure will still have to be serialized.
446             #pod
447             #pod Possible options are:
448             #pod
449             #pod =for :list
450             #pod * C A boolean indicating if "relaxed extended JSON" should
451             #pod be generated. If not set, the default value is taken from the
452             #pod C environment variable.
453             #pod
454             #pod =cut
455              
456             my $use_win32_specials = ($^O eq 'MSWin32' && $] lt "5.022");
457              
458             my $is_inf = $use_win32_specials ? qr/^1.\#INF/i : qr/^inf/i;
459             my $is_ninf = $use_win32_specials ? qr/^-1.\#INF/i : qr/^-inf/i;
460             my $is_nan = $use_win32_specials ? qr/^-?1.\#(?:IND|QNAN)/i : qr/^-?nan/i;
461              
462             sub perl_to_extjson {
463 3667     3667 1 38803 my ($class, $data, $options) = @_;
464              
465 3667         17629 local $ENV{BSON_EXTJSON} = 1;
466 3667         11801 local $ENV{BSON_EXTJSON_RELAXED} = $ENV{BSON_EXTJSON_RELAXED};
467 3667         9005 $ENV{BSON_EXTJSON_RELAXED} = $options->{relaxed};
468              
469 3667 100       7084 if (not defined $data) {
470 6         32 return undef; ## no critic
471             }
472              
473 3661 100 100     16664 if (blessed($data) and $data->can('TO_JSON')) {
474 1766         4610 my $json_data = $data->TO_JSON;
475 1766         22726 return $json_data;
476             }
477              
478 1895 100       4210 if (not ref $data) {
479              
480 67 100       198 if (looks_like_number($data)) {
481 25 100       58 if ($ENV{BSON_EXTJSON_RELAXED}) {
482 16         81 return $data;
483             }
484              
485 9 50       67 if ($data =~ m{\A-?[0-9_]+\z}) {
486 9 50       25 if ($data <= $max_int32) {
487 9         75 return { '$numberInt' => "$data" };
488             }
489             else {
490 0         0 return { '$numberLong' => "$data" };
491             }
492             }
493             else {
494 0 0       0 return { '$numberDouble' => 'Infinity' }
495             if $data =~ $is_inf;
496 0 0       0 return { '$numberDouble' => '-Infinity' }
497             if $data =~ $is_ninf;
498 0 0       0 return { '$numberDouble' => 'NaN' }
499             if $data =~ $is_nan;
500 0         0 my $value = "$data";
501 0         0 $value = $value / 1.0;
502 0         0 return { '$numberDouble' => "$value" };
503             }
504             }
505              
506 42         267 return $data;
507             }
508              
509 1828 50       5216 if (boolean::isBoolean($data)) {
510 0         0 return $data;
511             }
512              
513 1828 100       36619 if (ref $data eq 'HASH') {
514 1812         25892 for my $key (keys %$data) {
515 1850         28735 my $value = $data->{$key};
516 1850         13386 $data->{$key} = $class->perl_to_extjson($value, $options);
517             }
518 1812         29159 return $data;
519             }
520              
521 16 100       39 if (ref $data eq 'ARRAY') {
522 10         26 for my $index (0 .. $#$data) {
523 16         23 my $value = $data->[$index];
524 16         27 $data->[$index] = $class->perl_to_extjson($value, $options);
525             }
526 10         62 return $data;
527             }
528              
529 6 100 66     38 if (blessed($data) and $data->isa('JSON::PP::Boolean')) {
530 4         27 return $data;
531             }
532              
533 2 50 33     36 if (
      33        
534             blessed($data) and (
535             $data->isa('Math::BigInt') or
536             $data->isa('Math::BigFloat')
537             )
538             ) {
539 2         43 return $data;
540             }
541              
542 0         0 die sprintf "Unsupported ref value (%s)", ref($data);
543             }
544              
545             #pod =method extjson_to_perl
546             #pod
547             #pod use JSON::MaybeXS;
548             #pod my $ext = decode_json($json);
549             #pod my $data = $bson->extjson_to_perl($ext);
550             #pod
551             #pod Takes an
552             #pod L
553             #pod data structure and inflates it into a Perl data structure. Note that
554             #pod you have to decode the JSON string manually beforehand.
555             #pod
556             #pod Canonically specified numerical values like C<{"$numberInt":"23"}> will
557             #pod be inflated into their respective C wrapper types. Plain numeric
558             #pod values will be left as-is.
559             #pod
560             #pod =cut
561              
562             sub extjson_to_perl {
563 2054     2054 1 3178278 my ($class, $data) = @_;
564             # top level keys are never extended JSON elements, so we wrap the
565             # _extjson_to_perl inflater so it applies only to values, not the
566             # original data structure
567 2054         6928 for my $key (keys %$data) {
568 2102         31765 my $value = $data->{$key};
569 2102         15234 $data->{$key} = $class->_extjson_to_perl($value);
570             }
571 2054         32272 return $data;
572             }
573              
574             sub _extjson_to_perl {
575 2177     2177   4000 my ($class, $data) = @_;
576              
577 2177 100       6399 if (ref $data eq 'HASH') {
578              
579 2101 100       6108 if ( exists $data->{'$oid'} ) {
580 26         143 return BSON::OID->new( oid => pack( "H*", $data->{'$oid'} ) );
581             }
582              
583 2075 100       11935 if ( exists $data->{'$numberInt'} ) {
584 38         804 return BSON::Int32->new( value => $data->{'$numberInt'} );
585             }
586              
587 2037 100       9355 if ( exists $data->{'$numberLong'} ) {
588 27         93 if (HAS_INT64) {
589 27         536 return BSON::Int64->new( value => $data->{'$numberLong'} );
590             }
591             else {
592             return BSON::Int64->new( value => Math::BigInt->new($data->{'$numberLong'}) );
593             }
594             }
595              
596 2010 100       9930 if ( exists $data->{'$binary'} ) {
597 24         188 require MIME::Base64;
598 24 50       58 if (exists $data->{'$type'}) {
599             return BSON::Bytes->new(
600             data => MIME::Base64::decode_base64($data->{'$binary'}),
601 0   0     0 subtype => hex( $data->{'$type'} || 0 ),
602             );
603             }
604             else {
605 24         116 my $value = $data->{'$binary'};
606             return BSON::Bytes->new(
607             data => MIME::Base64::decode_base64($value->{base64}),
608 24   50     185 subtype => hex( $value->{subType} || 0 ),
609             );
610             }
611             }
612              
613 1986 100       8780 if ( exists $data->{'$date'} ) {
614 17         75 my $v = $data->{'$date'};
615 17 100       144 $v = ref($v) eq 'HASH' ? $class->_extjson_to_perl($v) : _iso8601_to_epochms($v);
616 17         312 return BSON::Time->new( value => $v );
617             }
618              
619 1969 100       8822 if ( exists $data->{'$minKey'} ) {
620 4         22 return BSON::MinKey->new;
621             }
622              
623 1965 100       8136 if ( exists $data->{'$maxKey'} ) {
624 4         22 return BSON::MaxKey->new;
625             }
626              
627 1961 100       8947 if ( exists $data->{'$timestamp'} ) {
628             return BSON::Timestamp->new(
629             seconds => $data->{'$timestamp'}{t},
630             increment => $data->{'$timestamp'}{i},
631 10         56 );
632             }
633              
634 1951 50 33     8378 if ( exists $data->{'$regex'} and not ref $data->{'$regex'}) {
635             return BSON::Regex->new(
636             pattern => $data->{'$regex'},
637 0 0       0 ( exists $data->{'$options'} ? ( flags => $data->{'$options'} ) : () ),
638             );
639             }
640              
641 1951 100       9076 if ( exists $data->{'$regularExpression'} ) {
642 24         101 my $value = $data->{'$regularExpression'};
643             return BSON::Regex->new(
644             pattern => $value->{pattern},
645 24 50       170 ( exists $value->{options} ? ( flags => $value->{options} ) : () ),
646             );
647             }
648              
649 1927 100       8904 if ( exists $data->{'$code'} ) {
650             return BSON::Code->new(
651             code => $data->{'$code'},
652             ( exists $data->{'$scope'}
653 20 100       108 ? ( scope => $class->_extjson_to_perl($data->{'$scope'}) )
654             : ()
655             ),
656             );
657             }
658              
659 1907 100       8237 if ( exists $data->{'$undefined'} ) {
660 2         15 return undef; ## no critic
661             }
662              
663 1905 100       8212 if ( exists $data->{'$dbPointer'} ) {
664 8         44 my $data = $data->{'$dbPointer'};
665 8         62 my $id = $data->{'$id'};
666 8 50       71 $id = $class->_extjson_to_perl($id) if ref($id) eq 'HASH';
667             return BSON::DBPointer->new(
668 8         162 '$ref' => $data->{'$ref'},
669             '$id' => $id,
670             );
671             }
672              
673 1897 100       7926 if ( exists $data->{'$ref'} ) {
674 10         55 my $id = delete $data->{'$id'};
675 10 50       252 $id = $class->_extjson_to_perl($id) if ref($id) eq 'HASH';
676             return BSON::DBRef->new(
677             '$ref' => delete $data->{'$ref'},
678             '$id' => $id,
679 10         53 '$db' => delete $data->{'$db'},
680             %$data, # extra
681             );
682             }
683              
684 1887 100       8242 if ( exists $data->{'$numberDecimal'} ) {
685 1823         48641 return BSON::Decimal128->new( value => $data->{'$numberDecimal'} );
686             }
687              
688             # Following extended JSON is non-standard
689              
690 64 100       265 if ( exists $data->{'$numberDouble'} ) {
691 28 0 33     128 if ( $data->{'$numberDouble'} eq '-0' && $] lt '5.014' && ! HAS_LD ) {
      50        
692 0         0 $data->{'$numberDouble'} = '-0.0';
693             }
694 28         706 return BSON::Double->new( value => $data->{'$numberDouble'} );
695             }
696              
697 36 100       151 if ( exists $data->{'$symbol'} ) {
698 12         268 return BSON::Symbol->new(value => $data->{'$symbol'});
699             }
700              
701 24         122 for my $key (keys %$data) {
702 14         190 my $value = $data->{$key};
703 14         103 $data->{$key} = $class->_extjson_to_perl($value);
704             }
705 24         515 return $data;
706             }
707              
708 76 100       154 if (ref $data eq 'ARRAY') {
709 10         26 for my $index (0 .. $#$data) {
710 16         28 my $value = $data->[$index];
711 16 50       38 $data->[$index] = ref($value)
712             ? $class->_extjson_to_perl($value)
713             : $value;
714             }
715 10         44 return $data;
716             }
717              
718 66         188 return $data;
719             }
720              
721             #--------------------------------------------------------------------------#
722             # legacy functional interface
723             #--------------------------------------------------------------------------#
724              
725             #pod =func encode
726             #pod
727             #pod my $bson = encode({ bar => 'foo' }, \%options);
728             #pod
729             #pod This is the legacy, functional interface and is only exported on demand.
730             #pod It takes a hashref and returns a BSON string.
731             #pod It uses an internal codec singleton with default attributes.
732             #pod
733             #pod =func decode
734             #pod
735             #pod my $hash = decode( $bson, \%options );
736             #pod
737             #pod This is the legacy, functional interface and is only exported on demand.
738             #pod It takes a BSON string and returns a hashref.
739             #pod It uses an internal codec singleton with default attributes.
740             #pod
741             #pod =cut
742              
743             {
744             my $CODEC;
745              
746             sub encode {
747 732 50 33 732 1 113924 if ( defined $_[0] && ( $_[0] eq 'BSON' || ( blessed($_[0]) && $_[0]->isa('BSON') ) ) ) {
      33        
748 0         0 Carp::croak("Error: 'encode' is a function, not a method");
749             }
750 732         1871 my $doc = shift;
751 732 100       2194 $CODEC = BSON->new unless defined $CODEC;
752 732 100 66     5100 if ( @_ == 1 && ref( $_[0] ) eq 'HASH' ) {
    50          
753 101         234 return $CODEC->encode_one( $doc, $_[0] );
754             }
755             elsif ( @_ % 2 == 0 ) {
756 631         2841 return $CODEC->encode_one( $doc, {@_} );
757             }
758             else {
759 0         0 Carp::croak("Options for 'encode' must be a hashref or key-value pairs");
760             }
761             }
762              
763             sub decode {
764 688 50 33 688 1 85263 if ( defined $_[0] && ( $_[0] eq 'BSON' || ( blessed($_[0]) && $_[0]->isa('BSON') ) ) ) {
      33        
765 0         0 Carp::croak("Error: 'decode' is a function, not a method");
766             }
767 688         1452 my $doc = shift;
768 688 100       2074 $CODEC = BSON->new unless defined $CODEC;
769 688         1111 my $args;
770 688 50 33     3849 if ( @_ == 1 && ref( $_[0] ) eq 'HASH' ) {
    50          
771 0         0 $args = shift;
772             }
773             elsif ( @_ % 2 == 0 ) {
774 688         1678 $args = { @_ };
775             }
776             else {
777 0         0 Carp::croak("Options for 'decode' must be a hashref or key-value pairs");
778             }
779             $args->{ordered} = delete $args->{ixhash}
780 688 100 66     2447 if exists $args->{ixhash} && !exists $args->{ordered};
781 688         2498 return $CODEC->decode_one( $doc, $args );
782             }
783             }
784              
785             #--------------------------------------------------------------------------#
786             # private functions
787             #--------------------------------------------------------------------------#
788              
789             sub _inflate_hash {
790 0     0   0 my ( $class, $hash ) = @_;
791              
792 0 0       0 if ( exists $hash->{'$oid'} ) {
793 0         0 return BSON::OID->new( oid => pack( "H*", $hash->{'$oid'} ) );
794             }
795              
796 0 0       0 if ( exists $hash->{'$numberInt'} ) {
797 0         0 return BSON::Int32->new( value => $hash->{'$numberInt'} );
798             }
799              
800 0 0       0 if ( exists $hash->{'$numberLong'} ) {
801 0         0 if (HAS_INT64) {
802 0         0 return BSON::Int64->new( value => $hash->{'$numberLong'} );
803             }
804             else {
805             return BSON::Int64->new( value => Math::BigInt->new($hash->{'$numberLong'}) );
806             }
807             }
808              
809 0 0       0 if ( exists $hash->{'$binary'} ) {
810 0         0 require MIME::Base64;
811             return BSON::Bytes->new(
812             data => MIME::Base64::decode_base64($hash->{'$binary'}),
813 0   0     0 subtype => hex( $hash->{'$type'} || 0 )
814             );
815             }
816              
817 0 0       0 if ( exists $hash->{'$date'} ) {
818 0         0 my $v = $hash->{'$date'};
819 0 0       0 $v = ref($v) eq 'HASH' ? BSON->_inflate_hash($v) : _iso8601_to_epochms($v);
820 0         0 return BSON::Time->new( value => $v );
821             }
822              
823 0 0       0 if ( exists $hash->{'$minKey'} ) {
824 0         0 return BSON::MinKey->new;
825             }
826              
827 0 0       0 if ( exists $hash->{'$maxKey'} ) {
828 0         0 return BSON::MaxKey->new;
829             }
830              
831 0 0       0 if ( exists $hash->{'$timestamp'} ) {
832             return BSON::Timestamp->new(
833             seconds => $hash->{'$timestamp'}{t},
834             increment => $hash->{'$timestamp'}{i},
835 0         0 );
836             }
837              
838 0 0       0 if ( exists $hash->{'$regex'} ) {
839             return BSON::Regex->new(
840             pattern => $hash->{'$regex'},
841 0 0       0 ( exists $hash->{'$options'} ? ( flags => $hash->{'$options'} ) : () ),
842             );
843             }
844              
845 0 0       0 if ( exists $hash->{'$code'} ) {
846             return BSON::Code->new(
847             code => $hash->{'$code'},
848 0 0       0 ( exists $hash->{'$scope'} ? ( scope => $hash->{'$scope'} ) : () ),
849             );
850             }
851              
852 0 0       0 if ( exists $hash->{'$undefined'} ) {
853 0         0 return undef; ## no critic
854             }
855              
856 0 0       0 if ( exists $hash->{'$ref'} ) {
857 0         0 my $id = $hash->{'$id'};
858 0 0       0 $id = BSON->_inflate_hash($id) if ref($id) eq 'HASH';
859 0         0 return BSON::DBRef->new( '$ref' => $hash->{'$ref'}, '$id' => $id );
860             }
861              
862 0 0       0 if ( exists $hash->{'$numberDecimal'} ) {
863 0         0 return BSON::Decimal128->new( value => $hash->{'$numberDecimal'} );
864             }
865              
866             # Following extended JSON is non-standard
867              
868 0 0       0 if ( exists $hash->{'$numberDouble'} ) {
869 0 0 0     0 if ( $hash->{'$numberDouble'} eq '-0' && $] lt '5.014' && ! HAS_LD ) {
      0        
870 0         0 $hash->{'$numberDouble'} = '-0.0';
871             }
872 0         0 return BSON::Double->new( value => $hash->{'$numberDouble'} );
873             }
874              
875 0 0       0 if ( exists $hash->{'$symbol'} ) {
876 0         0 return $hash->{'$symbol'};
877             }
878              
879 0         0 return $hash;
880             }
881              
882             sub _inflate_array {
883 0     0   0 my ($class, $array) = @_;
884 0 0       0 if (@$array) {
885 0         0 for my $i ( 0 .. $#$array ) {
886 0         0 my $v = $array->[$i];
887 0 0       0 $array->[$i] =
    0          
888             ref($v) eq 'HASH' ? BSON->_inflate_hash($v)
889             : ref($v) eq 'ARRAY' ? _inflate_array($v)
890             : $v;
891             }
892             }
893 0         0 return $array;
894             }
895              
896             my $iso8601_re = qr{
897             (\d{4}) - (\d{2}) - (\d{2}) T # date
898             (\d{2}) : (\d{2}) : ( \d+ (?:\. \d+ )? ) # time
899             (?: Z | ([+-] \d{2} :? (?: \d{2} )? ) )? # maybe TZ
900             }x;
901              
902             sub _iso8601_to_epochms {
903 2     2   4 my ($date) = shift;
904 2         19 require Time::Local;
905              
906 2         2807 my $zone_offset = 0;;
907 2 50       8 if ( substr($date,-1,1) eq 'Z' ) {
908 2         7 chop($date);
909             }
910              
911 2 50       71 if ( $date =~ /\A$iso8601_re\z/ ) {
912 2         27 my ($Y,$M,$D,$h,$m,$s,$z) = ($1,$2-1,$3,$4,$5,$6,$7);
913 2 50 33     8 if (defined($z) && length($z)) {
914 0         0 $z =~ tr[:][];
915 0 0       0 $z .= "00" if length($z) < 5;
916 0         0 my $zd = substr($z,0,1);
917 0         0 my $zh = substr($z,1,2);
918 0         0 my $zm = substr($z,3,2);
919 0 0       0 $zone_offset = ($zd eq '-' ? -1 : 1 ) * (3600 * $zh + 60 * $zm);
920             }
921 2         8 my $frac = $s - int($s);
922 2         8 my $epoch = Time::Local::timegm(int($s), $m, $h, $D, $M, $Y) - $zone_offset;
923 2         70 $epoch = HAS_INT64 ? 1000 * $epoch : Math::BigInt->new($epoch) * 1000;
924 2         5 $epoch += HAS_INT64 ? $frac * 1000 : Math::BigFloat->new($frac) * 1000;
925 2         7 return $epoch;
926             }
927             else {
928 0           Carp::croak("Couldn't parse '\$date' field: $date\n");
929             }
930             }
931              
932             1;
933              
934             =pod
935              
936             =encoding UTF-8
937              
938             =head1 NAME
939              
940             BSON - BSON serialization and deserialization
941              
942             =head1 VERSION
943              
944             version v1.12.1
945              
946             =head1 END OF LIFE NOTICE
947              
948             Version v1.12.0 is the final feature release of the MongoDB BSON
949             library. The library is now in a 12-month "sunset" period and will
950             receive security patches and critical bug fixes only. The BSON
951             library will be end-of-life and unsupported on August 13, 2020.
952              
953             =head1 SYNOPSIS
954              
955             use BSON;
956             use BSON::Types ':all';
957             use boolean;
958              
959             my $codec = BSON->new;
960              
961             my $document = {
962             _id => bson_oid(),
963             creation_time => bson_time(), # now
964             zip_code => bson_string("08544"),
965             hidden => false,
966             };
967              
968             my $bson = $codec->encode_one( $document );
969             my $doc = $codec->decode_one( $bson );
970              
971             =head1 DESCRIPTION
972              
973             This class implements a BSON encoder/decoder ("codec"). It consumes
974             "documents" (typically hash references) and emits BSON strings and vice
975             versa in accordance with the L.
976              
977             BSON is the primary data representation for L. While this module
978             has several features that support MongoDB-specific needs and conventions,
979             it can be used as a standalone serialization format.
980              
981             The codec may be customized through attributes on the codec option as well
982             as encode/decode specific options on methods:
983              
984             my $codec = BSON->new( \%global_attributes );
985              
986             my $bson = $codec->encode_one( $document, \%encode_options );
987             my $doc = $codec->decode_one( $bson , \%decode_options );
988              
989             Because BSON is strongly-typed and Perl is not, this module supports
990             a number of "type wrappers" – classes that wrap Perl data to indicate how
991             they should serialize. The L module describes these and
992             provides associated helper functions. See L
993             for more details.
994              
995             When decoding, type wrappers are used for any data that has no native Perl
996             representation. Optionally, all data may be wrapped for precise control of
997             round-trip encoding.
998              
999             Please read the configuration attributes carefully to understand more about
1000             how to control encoding and decoding.
1001              
1002             At compile time, this module will select an implementation backend. It
1003             will prefer C (released separately) if available, or will fall
1004             back to L (bundled with this module). See L for
1005             a way to control the selection of the backend.
1006              
1007             =head1 ATTRIBUTES
1008              
1009             =head2 error_callback
1010              
1011             This attribute specifies a function reference that will be called with
1012             three positional arguments:
1013              
1014             =over 4
1015              
1016             =item *
1017              
1018             an error string argument describing the error condition
1019              
1020             =item *
1021              
1022             a reference to the problematic document or byte-string
1023              
1024             =item *
1025              
1026             the method in which the error occurred (e.g. C or C)
1027              
1028             =back
1029              
1030             Note: for decoding errors, the byte-string is passed as a reference to avoid
1031             copying possibly large strings.
1032              
1033             If not provided, errors messages will be thrown with C.
1034              
1035             =head2 invalid_chars
1036              
1037             A string containing ASCII characters that must not appear in keys. The default
1038             is the empty string, meaning there are no invalid characters.
1039              
1040             =head2 max_length
1041              
1042             This attribute defines the maximum document size. The default is 0, which
1043             disables any maximum.
1044              
1045             If set to a positive number, it applies to both encoding B decoding (the
1046             latter is necessary for prevention of resource consumption attacks).
1047              
1048             =head2 op_char
1049              
1050             This is a single character to use for special MongoDB-specific query
1051             operators. If a key starts with C, the C character will
1052             be replaced with "$".
1053              
1054             The default is "$", meaning that no replacement is necessary.
1055              
1056             =head2 ordered
1057              
1058             If set to a true value, then decoding will return a reference to a tied
1059             hash that preserves key order. Otherwise, a regular (unordered) hash
1060             reference will be returned.
1061              
1062             B:
1063              
1064             =over 4
1065              
1066             =item *
1067              
1068             When 'ordered' is true, users must not rely on the return value being any particular tied hash implementation. It may change in the future for efficiency.
1069              
1070             =item *
1071              
1072             Turning this option on entails a significant speed penalty as tied hashes are slower than regular Perl hashes.
1073              
1074             =back
1075              
1076             The default is false.
1077              
1078             =head2 prefer_numeric
1079              
1080             When false, scalar values will be encoded as a number if they were
1081             originally a number or were ever used in a numeric context. However, a
1082             string that looks like a number but was never used in a numeric context
1083             (e.g. "42") will be encoded as a string.
1084              
1085             If C is set to true, the encoder will attempt to coerce
1086             strings that look like a number into a numeric value. If the string
1087             doesn't look like a double or integer, it will be encoded as a string.
1088              
1089             B: the heuristics for determining whether something is a
1090             string or number are less accurate on older Perls. See L
1091             for wrapper classes that specify exact serialization types.
1092              
1093             The default is false.
1094              
1095             =head2 wrap_dbrefs
1096              
1097             If set to true, during decoding, documents with the fields C<'$id'> and
1098             C<'$ref'> (literal dollar signs, not variables) will be wrapped as
1099             L objects. If false, they are decoded into ordinary hash
1100             references (or ordered hashes, if C is true).
1101              
1102             The default is true.
1103              
1104             =head2 wrap_numbers
1105              
1106             If set to true, during decoding, numeric values will be wrapped into
1107             BSON type-wrappers: L, L or L.
1108             While very slow, this can help ensure fields can round-trip if unmodified.
1109              
1110             The default is false.
1111              
1112             =head2 wrap_strings
1113              
1114             If set to true, during decoding, string values will be wrapped into a BSON
1115             type-wrappers, L. While very slow, this can help ensure
1116             fields can round-trip if unmodified.
1117              
1118             The default is false.
1119              
1120             =head2 dt_type (Discouraged)
1121              
1122             Sets the type of object which is returned for BSON DateTime fields. The
1123             default is C, which returns objects of type L. This is
1124             overloaded to be the integer epoch value when used as a number or string,
1125             so is somewhat backwards compatible with C in the L
1126             driver.
1127              
1128             Other acceptable values are L (explicitly), L,
1129             L, L, L.
1130              
1131             Because BSON::Time objects have methods to convert to DateTime,
1132             Time::Moment or DateTime::Tiny, use of this field is discouraged. Users
1133             should use these methods on demand. This option is provided for backwards
1134             compatibility only.
1135              
1136             =head1 METHODS
1137              
1138             =head2 encode_one
1139              
1140             $byte_string = $codec->encode_one( $doc );
1141             $byte_string = $codec->encode_one( $doc, \%options );
1142              
1143             Takes a "document", typically a hash reference, an array reference, or a
1144             Tie::IxHash object and returns a byte string with the BSON representation of
1145             the document.
1146              
1147             An optional hash reference of options may be provided. Valid options include:
1148              
1149             =over 4
1150              
1151             =item *
1152              
1153             first_key – if C is defined, it and C will be encoded first in the output BSON; any matching key found in the document will be ignored.
1154              
1155             =item *
1156              
1157             first_value - value to assign to C; will encode as Null if omitted
1158              
1159             =item *
1160              
1161             error_callback – overrides codec default
1162              
1163             =item *
1164              
1165             invalid_chars – overrides codec default
1166              
1167             =item *
1168              
1169             max_length – overrides codec default
1170              
1171             =item *
1172              
1173             op_char – overrides codec default
1174              
1175             =item *
1176              
1177             prefer_numeric – overrides codec default
1178              
1179             =back
1180              
1181             =head2 decode_one
1182              
1183             $doc = $codec->decode_one( $byte_string );
1184             $doc = $codec->decode_one( $byte_string, \%options );
1185              
1186             Takes a byte string with a BSON-encoded document and returns a
1187             hash reference representing the decoded document.
1188              
1189             An optional hash reference of options may be provided. Valid options include:
1190              
1191             =over 4
1192              
1193             =item *
1194              
1195             dt_type – overrides codec default
1196              
1197             =item *
1198              
1199             error_callback – overrides codec default
1200              
1201             =item *
1202              
1203             max_length – overrides codec default
1204              
1205             =item *
1206              
1207             ordered - overrides codec default
1208              
1209             =item *
1210              
1211             wrap_dbrefs - overrides codec default
1212              
1213             =item *
1214              
1215             wrap_numbers - overrides codec default
1216              
1217             =item *
1218              
1219             wrap_strings - overrides codec default
1220              
1221             =back
1222              
1223             =head2 clone
1224              
1225             $copy = $codec->clone( ordered => 1 );
1226              
1227             Constructs a copy of the original codec, but allows changing
1228             attributes in the copy.
1229              
1230             =head2 create_oid
1231              
1232             $oid = BSON->create_oid;
1233              
1234             This class method returns a new L. This abstracts OID
1235             generation away from any specific Object ID class and makes it an interface
1236             on a BSON codec. Alternative BSON codecs should define a similar class
1237             method that returns an Object ID of whatever type is appropriate.
1238              
1239             =head2 inflate_extjson (DEPRECATED)
1240              
1241             This legacy method does not follow the L
1242             specification.
1243              
1244             Use L instead.
1245              
1246             =head2 perl_to_extjson
1247              
1248             use JSON::MaybeXS;
1249             my $ext = BSON->perl_to_extjson($data, \%options);
1250             my $json = encode_json($ext);
1251              
1252             Takes a perl data structure (i.e. hashref) and turns it into an
1253             L
1254             structure. Note that the structure will still have to be serialized.
1255              
1256             Possible options are:
1257              
1258             =over 4
1259              
1260             =item *
1261              
1262             C A boolean indicating if "relaxed extended JSON" should
1263              
1264             be generated. If not set, the default value is taken from the
1265             C environment variable.
1266              
1267             =back
1268              
1269             =head2 extjson_to_perl
1270              
1271             use JSON::MaybeXS;
1272             my $ext = decode_json($json);
1273             my $data = $bson->extjson_to_perl($ext);
1274              
1275             Takes an
1276             L
1277             data structure and inflates it into a Perl data structure. Note that
1278             you have to decode the JSON string manually beforehand.
1279              
1280             Canonically specified numerical values like C<{"$numberInt":"23"}> will
1281             be inflated into their respective C wrapper types. Plain numeric
1282             values will be left as-is.
1283              
1284             =head1 FUNCTIONS
1285              
1286             =head2 encode
1287              
1288             my $bson = encode({ bar => 'foo' }, \%options);
1289              
1290             This is the legacy, functional interface and is only exported on demand.
1291             It takes a hashref and returns a BSON string.
1292             It uses an internal codec singleton with default attributes.
1293              
1294             =head2 decode
1295              
1296             my $hash = decode( $bson, \%options );
1297              
1298             This is the legacy, functional interface and is only exported on demand.
1299             It takes a BSON string and returns a hashref.
1300             It uses an internal codec singleton with default attributes.
1301              
1302             =for Pod::Coverage BUILD
1303              
1304             =head1 PERL-BSON TYPE MAPPING
1305              
1306             BSON has numerous data types and Perl does not.
1307              
1308             When B, each BSON type should result in a single, predictable
1309             Perl type. Where no native Perl type is appropriate, BSON decodes to an
1310             object of a particular class (a "type wrapper").
1311              
1312             When B, for historical reasons, there may be many Perl
1313             representations that should encode to a particular BSON type. For example,
1314             all the popular "boolean" type modules on CPAN should encode to the BSON
1315             boolean type. Likewise, as this module is intended to supersede the
1316             type wrappers that have shipped with the L module, those
1317             type wrapper are supported by this codec.
1318              
1319             The table below describes the BSON/Perl mapping for both encoding and
1320             decoding.
1321              
1322             On the left are all the Perl types or classes this BSON codec
1323             knows how to serialize to BSON. The middle column is the BSON type for
1324             each class. The right-most column is the Perl type or class that the BSON
1325             type deserializes to. Footnotes indicate variations or special behaviors.
1326              
1327             Perl type/class -> BSON type -> Perl type/class
1328             -------------------------------------------------------------------
1329             float[1] 0x01 DOUBLE float[2]
1330             BSON::Double
1331             -------------------------------------------------------------------
1332             string[3] 0x02 UTF8 string[2]
1333             BSON::String
1334             -------------------------------------------------------------------
1335             hashref 0x03 DOCUMENT hashref[4][5]
1336             BSON::Doc
1337             BSON::Raw
1338             MongoDB::BSON::Raw[d]
1339             Tie::IxHash
1340             -------------------------------------------------------------------
1341             arrayref 0x04 ARRAY arrayref
1342             -------------------------------------------------------------------
1343             BSON::Bytes 0x05 BINARY BSON::Bytes
1344             scalarref
1345             BSON::Binary[d]
1346             MongoDB::BSON::Binary[d]
1347             -------------------------------------------------------------------
1348             n/a 0x06 UNDEFINED[d] undef
1349             -------------------------------------------------------------------
1350             BSON::OID 0x07 OID BSON::OID
1351             BSON::ObjectId[d]
1352             MongoDB::OID[d]
1353             -------------------------------------------------------------------
1354             boolean 0x08 BOOL boolean
1355             BSON::Bool[d]
1356             JSON::XS::Boolean
1357             JSON::PP::Boolean
1358             JSON::Tiny::_Bool
1359             Mojo::JSON::_Bool
1360             Cpanel::JSON::XS::Boolean
1361             Types::Serialiser::Boolean
1362             -------------------------------------------------------------------
1363             BSON::Time 0x09 DATE_TIME BSON::Time
1364             DateTime
1365             DateTime::Tiny
1366             Time::Moment
1367             Mango::BSON::Time
1368             -------------------------------------------------------------------
1369             undef 0x0a NULL undef
1370             -------------------------------------------------------------------
1371             BSON::Regex 0x0b REGEX BSON::Regex
1372             qr// reference
1373             MongoDB::BSON::Regexp[d]
1374             -------------------------------------------------------------------
1375             n/a 0x0c DBPOINTER[d] BSON::DBRef
1376             -------------------------------------------------------------------
1377             BSON::Code[6] 0x0d CODE BSON::Code
1378             MongoDB::Code[6]
1379             -------------------------------------------------------------------
1380             n/a 0x0e SYMBOL[d] string
1381             -------------------------------------------------------------------
1382             BSON::Code[6] 0x0f CODEWSCOPE BSON::Code
1383             MongoDB::Code[6]
1384             -------------------------------------------------------------------
1385             integer[7][8] 0x10 INT32 integer[2]
1386             BSON::Int32
1387             -------------------------------------------------------------------
1388             BSON::Timestamp 0x11 TIMESTAMP BSON::Timestamp
1389             MongoDB::Timestamp[d]
1390             -------------------------------------------------------------------
1391             integer[7] 0x12 INT64 integer[2][9]
1392             BSON::Int64
1393             Math::BigInt
1394             Math::Int64
1395             -------------------------------------------------------------------
1396             BSON::MaxKey 0x7F MAXKEY BSON::MaxKey
1397             MongoDB::MaxKey[d]
1398             -------------------------------------------------------------------
1399             BSON::MinKey 0xFF MINKEY BSON::MinKey
1400             MongoDB::MinKey[d]
1401              
1402             [d] Deprecated or soon to be deprecated.
1403             [1] Scalar with "NV" internal representation or a string that looks
1404             like a float if the 'prefer_numeric' option is true.
1405             [2] If the 'wrap_numbers' option is true, numeric types will be wrapped
1406             as BSON::Double, BSON::Int32 or BSON::Int64 as appropriate to ensure
1407             round-tripping. If the 'wrap_strings' option is true, strings will
1408             be wrapped as BSON::String, likewise.
1409             [3] Scalar without "NV" or "IV" representation and not identified as a
1410             number by notes [1] or [7].
1411             [4] If 'ordered' option is set, will return a tied hash that preserves
1412             order (deprecated 'ixhash' option still works).
1413             [5] If the document appears to contain a DBRef and a 'dbref_callback'
1414             exists, that callback is executed with the deserialized document.
1415             [6] Code is serialized as CODE or CODEWSCOPE depending on whether a
1416             scope hashref exists in BSON::Code/MongoDB::Code.
1417             [7] Scalar with "IV" internal representation or a string that looks like
1418             an integer if the 'prefer_numeric' option is true.
1419             [8] Only if the integer fits in 32 bits.
1420             [9] On 32-bit platforms, 64-bit integers are deserialized to
1421             Math::BigInt objects (even if subsequently wrapped into
1422             BSON::Int64 if 'wrap_scalars' is true).
1423              
1424             =head1 THREADS
1425              
1426             Threads are never recommended in Perl, but this module is thread safe.
1427              
1428             =head1 ENVIRONMENT
1429              
1430             =over 4
1431              
1432             =item *
1433              
1434             PERL_BSON_BACKEND – if set at compile time, this will be treated as a module name. The module will be loaded and used as the BSON backend implementation. It must implement the same API as C.
1435              
1436             =item *
1437              
1438             BSON_EXTJSON - if set, serializing BSON type wrappers via C will produce Extended JSON v2 output.
1439              
1440             =item *
1441              
1442             BSON_EXTJSON_RELAXED - if producing Extended JSON output, if this is true, values will use the "Relaxed" form of Extended JSON, which sacrifices type round-tripping for improved human readability.
1443              
1444             =back
1445              
1446             =head1 SEMANTIC VERSIONING SCHEME
1447              
1448             Starting with BSON C, this module is using a "tick-tock"
1449             three-part version-tuple numbering scheme: C
1450              
1451             =over 4
1452              
1453             =item *
1454              
1455             In stable releases, C will be incremented for incompatible API changes.
1456              
1457             =item *
1458              
1459             Even-value increments of C indicate stable releases with new functionality. C will be incremented for bug fixes.
1460              
1461             =item *
1462              
1463             Odd-value increments of C indicate unstable ("development") releases that should not be used in production. C increments have no semantic meaning; they indicate only successive development releases. Development releases may have API-breaking changes, usually indicated by C equal to "999".
1464              
1465             =back
1466              
1467             =head1 HISTORY AND ROADMAP
1468              
1469             This module was originally written by Stefan G. In 2014, he graciously
1470             transferred ongoing maintenance to MongoDB, Inc.
1471              
1472             The C helper functions in L were inspired by similar
1473             work in L by Sebastian Riedel.
1474              
1475             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
1476              
1477             =head1 SUPPORT
1478              
1479             =head2 Bugs / Feature Requests
1480              
1481             Please report any bugs or feature requests through the issue tracker
1482             at L.
1483             You will be notified automatically of any progress on your issue.
1484              
1485             =head2 Source Code
1486              
1487             This is open source software. The code repository is available for
1488             public review and contribution under the terms of the license.
1489              
1490             L
1491              
1492             git clone https://github.com/mongodb/mongo-perl-bson.git
1493              
1494             =head1 AUTHORS
1495              
1496             =over 4
1497              
1498             =item *
1499              
1500             David Golden
1501              
1502             =item *
1503              
1504             Stefan G.
1505              
1506             =back
1507              
1508             =head1 CONTRIBUTORS
1509              
1510             =for stopwords Eric Daniels Finn Olivier Duclos Pat Gunn Petr Písař Robert Sedlacek Thomas Bloor Tobias Leich Wallace Reis Yury Zavarin Oleg Kostyuk
1511              
1512             =over 4
1513              
1514             =item *
1515              
1516             Eric Daniels
1517              
1518             =item *
1519              
1520             Finn
1521              
1522             =item *
1523              
1524             Olivier Duclos
1525              
1526             =item *
1527              
1528             Pat Gunn
1529              
1530             =item *
1531              
1532             Petr Písař
1533              
1534             =item *
1535              
1536             Robert Sedlacek
1537              
1538             =item *
1539              
1540             Thomas Bloor
1541              
1542             =item *
1543              
1544             Tobias Leich
1545              
1546             =item *
1547              
1548             Wallace Reis
1549              
1550             =item *
1551              
1552             Yury Zavarin
1553              
1554             =item *
1555              
1556             Oleg Kostyuk
1557              
1558             =back
1559              
1560             =head1 COPYRIGHT AND LICENSE
1561              
1562             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
1563              
1564             This is free software, licensed under:
1565              
1566             The Apache License, Version 2.0, January 2004
1567              
1568             =cut
1569              
1570             __END__