This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
BSON.pm
144 lines (97 loc) · 3.83 KB
/
BSON.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#
# Copyright 2009-2013 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package MongoDB::BSON;
# ABSTRACT: Tools for serializing and deserializing data in BSON form
use version;
our $VERSION = 'v0.999.998.2'; # TRIAL
use XSLoader;
XSLoader::load("MongoDB", $VERSION);
use Moose;
use namespace::clean -except => 'meta';
=head1 NAME
MongoDB::BSON - Encoding and decoding utilities (more to come)
=head1 ATTRIBUTES
=head2 C<looks_like_number>
$MongoDB::BSON::looks_like_number = 1;
$collection->insert({age => "4"}); # stores 4 as an int
If this is set, the driver will be more aggressive about converting strings into
numbers. Anything that L<Scalar::Util>'s looks_like_number would approve as a
number will be sent to MongoDB as its numeric value.
Defaults to 0 (for backwards compatibility).
If you do not set this, you may be using strings more often than you intend to.
See the L<MongoDB::DataTypes> section for more info on the behavior of strings
vs. numbers.
=cut
$MongoDB::BSON::looks_like_number = 0;
=head2 char
$MongoDB::BSON::char = ":";
$collection->query({"x" => {":gt" => 4}});
Can be used to set a character other than "$" to use for special operators.
=cut
$MongoDB::BSON::char = '$';
=head2 Turn on/off UTF8 flag when return strings
# turn off utf8 flag on strings
$MongoDB::BSON::utf8_flag_on = 0;
Default is turn on, that compatible with version before 0.34.
If set to 0, will turn of utf8 flag on string attribute and return on bytes mode, meant same as :
utf8::encode($str)
Currently MongoDB return string with utf8 flag, on character mode , some people
wish to turn off utf8 flag and return string on byte mode, it maybe help to display "pretty" strings.
NOTE:
If you turn off utf8 flag, the string length will compute as bytes, and is_utf8 will return false.
=cut
$MongoDB::BSON::utf8_flag_on = 1;
=head2 Return boolean values as booleans instead of integers
$MongoDB::BSON::use_boolean = 1
By default, booleans are deserialized as integers. If you would like them to be
deserialized as L<boolean/true> and L<boolean/false>, set
C<$MongoDB::BSON::use_boolean> to 1.
=cut
$MongoDB::BSON::use_boolean = 0;
=head2 Return binary data as instances of L<MongoDB::BSON::Binary> instead of
string refs.
$MongoDB::BSON::use_binary = 1
For backwards compatibility, binary data is deserialized as a string ref. If
you would like to have it deserialized as instances of L<MongoDB::BSON::Binary>
(to, say, preserve the subtype), set C<$MongoDB::BSON::use_binary> to 1.
=cut
$MongoDB::BSON::use_binary = 0;
sub decode_bson {
my ($msg,$client) = @_;
my @decode_args;
if ( $client ) {
@decode_args = map { $client->$_ } qw/dt_type inflate_dbrefs inflate_regexps/;
push @decode_args, $client;
}
else {
@decode_args = (undef, 0, 0, undef);
}
my $struct = eval { MongoDB::BSON::_decode_bson($msg, @decode_args) };
Carp::confess($@) if $@;
return $struct;
}
sub encode_bson {
my ($struct, $clean_keys, $max_size) = @_;
$clean_keys = 0 unless defined $clean_keys;
my $bson = eval { MongoDB::BSON::_encode_bson($struct, $clean_keys) };
Carp::confess($@) if $@;
if ( $max_size && length($bson) > $max_size ) {
Carp::confess("Document exceeds maximum size $max_size");
}
return $bson;
}
__PACKAGE__->meta->make_immutable;
1;