| 1 | |||||||||||||||
| 2 | package Mango::BSON::Dump; | ||||||||||||||
| 3 | 2 | $Mango::BSON::Dump::VERSION = '0.2'; | |||||||||||||
| 4 | # ABSTRACT: Helpers to dump Mango BSON documents as Extended JSON | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use Mojo::Base -strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use Mango::BSON (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use Mojo::Util (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use re (); # regexp_pattern() | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use JSON::XS (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | ▶ | use Exporter 5.57 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 2 | our @EXPORT_OK = qw(to_extjson); | |||||||||||||
| 16 | |||||||||||||||
| 17 | 0 | ▶ | sub to_extjson { | ||||||||||||
|
Subroutine
to_extjson: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 18 | 0 | my $doc = shift; | |||||||||||||
| 19 | 0 | state $encoder = JSON::XS->new->convert_blessed(1); | |||||||||||||
| 20 | 0 | my %opts = (pretty=>0,@_); | |||||||||||||
| 21 | 0 | $encoder->$_($opts{$_}) for qw(pretty); | |||||||||||||
| 22 | 0 | return $encoder->encode($doc) . "\n"; | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 2 | our %BINTYPE_MAP = ( | |||||||||||||
| 26 | 'generic' => Mango::BSON::BINARY_GENERIC(), | ||||||||||||||
| 27 | 'function' => Mango::BSON::BINARY_FUNCTION(), | ||||||||||||||
| 28 | 'md5' => Mango::BSON::BINARY_MD5(), | ||||||||||||||
| 29 | 'uuid' => Mango::BSON::BINARY_UUID(), | ||||||||||||||
| 30 | 'user_defined' => Mango::BSON::BINARY_USER_DEFINED(), | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | my %TO_EXTJSON = ( | ||||||||||||||
| 34 | |||||||||||||||
| 35 | # bson_bin | ||||||||||||||
| 36 | 1 | ▶ | 'Mango::BSON::Binary' => sub { | ||||||||||||
|
Subroutine
TO_JSON: called CC=2 SCAR=7.1
| |||||||||||||||
| 37 | 1 | my $bindata = Mojo::Util::b64_encode( $_[0]->data, '' ); | |||||||||||||
| 38 | 1 | ▶ | my $type = unpack( "H2", $BINTYPE_MAP{ $_[0]->type // 'generic' } ); | ||||||||||||
|
Condition50%1/2
$_[0]->type // "generic"
MC/DC0%0/1
$_[0]->type // "generic"
$_[0]->type
Truth table50%1/2
A
A= $_[0]->type
| |||||||||||||||
| 39 | 1 | { '$binary' => $bindata, '$type' => $type }; | |||||||||||||
| 40 | }, | ||||||||||||||
| 41 | |||||||||||||||
| 42 | # bson_code | ||||||||||||||
| 43 | 0 | ▶ | 'Mango::BSON::Code' => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=9.2
Subroutine
TO_JSON: called CC=2 SCAR=9.2
| |||||||||||||||
| 44 | 0 | ▶ | $_[0]->scope | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 45 | ? { '$code' => $_[0]->code, '$scope' => $_[0]->scope } | ||||||||||||||
| 46 | : { '$code' => $_[0]->code }; | ||||||||||||||
| 47 | }, | ||||||||||||||
| 48 | |||||||||||||||
| 49 | # bson_double | ||||||||||||||
| 50 | # bson_int32 | ||||||||||||||
| 51 | # bson_int64 | ||||||||||||||
| 52 | 3 | ▶ | 'Mango::BSON::Number' => sub { | ||||||||||||
|
Subroutine
TO_JSON: called CC=2 SCAR=6.9
| |||||||||||||||
| 53 | 3 | ▶ | ( $_[0]->type eq Mango::BSON::INT64() ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | ? { '$numberLong' => $_[0]->to_string } | ||||||||||||||
| 55 | : $_[0]->value + 0 # DOUBLE() or INT32() | ||||||||||||||
| 56 | }, | ||||||||||||||
| 57 | |||||||||||||||
| 58 | # bson_max | ||||||||||||||
| 59 | 1 | ▶ | 'Mango::BSON::_MaxKey' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 60 | 1 | { '$maxKey' => 1 }; | |||||||||||||
| 61 | }, | ||||||||||||||
| 62 | |||||||||||||||
| 63 | # bson_min | ||||||||||||||
| 64 | 1 | ▶ | 'Mango::BSON::_MinKey' => sub { | ||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | 1 | { '$minKey' => 1 }; | |||||||||||||
| 66 | }, | ||||||||||||||
| 67 | |||||||||||||||
| 68 | # bson_oid | ||||||||||||||
| 69 | 2 | ▶ | 'Mango::BSON::ObjectID' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 70 | 2 | { '$oid' => $_[0]->to_string }; | |||||||||||||
| 71 | }, | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # bson_time | ||||||||||||||
| 74 | 1 | ▶ | 'Mango::BSON::Time' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | # {'$date' => {'$numberLong' => $_[0]->to_string . ''}} | ||||||||||||||
| 77 | 1 | { '$date' => $_[0]->to_datetime }; | |||||||||||||
| 78 | }, | ||||||||||||||
| 79 | |||||||||||||||
| 80 | # bson_ts | ||||||||||||||
| 81 | 1 | ▶ | 'Mango::BSON::Timestamp' => sub { | ||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | 1 | { '$timestamp' => { 't' => $_[0]->seconds, 'i' => $_[0]->increment } }; | |||||||||||||
| 83 | }, | ||||||||||||||
| 84 | |||||||||||||||
| 85 | # regex | ||||||||||||||
| 86 | 1 | ▶ | 'Regexp' => sub { | ||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
| |||||||||||||||
| 87 | 1 | my ( $p, $m ) = re::regexp_pattern( $_[0] ); | |||||||||||||
| 88 | 1 | { '$regex' => $p, '$options' => $m }; | |||||||||||||
| 89 | }, | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 2 | ); | |||||||||||||
| 92 | |||||||||||||||
| 93 | # Don't need TO_EXTJSON: | ||||||||||||||
| 94 | # bson_doc | ||||||||||||||
| 95 | # bson_dbref | ||||||||||||||
| 96 | # bson_true | ||||||||||||||
| 97 | # bson_false | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 2 | for my $class ( keys %TO_EXTJSON ) { | |||||||||||||
| 100 | 18 | my $patch = $TO_EXTJSON{$class}; | |||||||||||||
| 101 | 18 | Mojo::Util::monkey_patch( $class, TO_JSON => $patch ); | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 2 | 1; | |||||||||||||
| 105 | |||||||||||||||
| 106 | =pod | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =encoding UTF-8 | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 NAME | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Mango::BSON::Dump - Helpers to dump Mango BSON documents as Extended JSON | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 VERSION | ||||||||||||||
| 115 | |||||||||||||||
| 116 | version 0.2 | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||||||
| 119 | |||||||||||||||
| 120 | use Mango::BSON ':bson'; | ||||||||||||||
| 121 | use Mango::BSON::Dump qw(to_extjson); | ||||||||||||||
| 122 | |||||||||||||||
| 123 | # '{"v":{"$numberLong":"42"},"created":{"$date":"1970-01-01T00:00:00Z"}}' | ||||||||||||||
| 124 | to_extjson(bson_doc(v => bson_int64(42), created => bson_time(0))); | ||||||||||||||
| 125 | |||||||||||||||
| 126 | use Mojo::JSON qw(encode_json); | ||||||||||||||
| 127 | |||||||||||||||
| 128 | # '{"v":{"$numberLong":"42"},"created":{"$date":"1970-01-01T00:00:00Z"}}' | ||||||||||||||
| 129 | encode_json(bson_doc(v => bson_int64(42), created => bson_time(0))); | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 DESCRIPTION | ||||||||||||||
| 132 | |||||||||||||||
| 133 | This module enables dumping Mango BSON documents and objects | ||||||||||||||
| 134 | as Extended JSON (see L<https://docs.mongodb.com/manual/reference/mongodb-extended-json/>), | ||||||||||||||
| 135 | which might be handy for development and debugging. | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head1 FUNCTIONS | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =over 4 | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =item B<to_extjson> | ||||||||||||||
| 142 | |||||||||||||||
| 143 | $json = to_extjson($bson_doc); | ||||||||||||||
| 144 | $json = to_extjson($bson_doc, pretty => 1); | ||||||||||||||
| 145 | |||||||||||||||
| 146 | Encodes C<$bson_doc> into Extended JSON. | ||||||||||||||
| 147 | |||||||||||||||
| 148 | =back | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =head1 CAVEATS | ||||||||||||||
| 151 | |||||||||||||||
| 152 | This module installs C<TO_JSON> methods to a number of packages | ||||||||||||||
| 153 | (eg. Mango::BSON::Number, Mango::BSON::ObjectID, Regexp). | ||||||||||||||
| 154 | This does not play well with other modules defining or installing | ||||||||||||||
| 155 | the same methods to the same classes. | ||||||||||||||
| 156 | As of Mango 1.29, this clashes with defined C<TO_JSON> | ||||||||||||||
| 157 | for Mango::BSON::Binary, Mango::BSON::Number, and Mango::BSON::Time | ||||||||||||||
| 158 | (which don't conform to Extended JSON). | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =head1 SEE ALSO | ||||||||||||||
| 161 | |||||||||||||||
| 162 | https://docs.mongodb.com/manual/reference/mongodb-extended-json/ | ||||||||||||||
| 163 | |||||||||||||||
| 164 | https://docs.mongodb.com/manual/reference/bson-types/ | ||||||||||||||
| 165 | |||||||||||||||
| 166 | http://bsonspec.org/ | ||||||||||||||
| 167 | |||||||||||||||
| 168 | https://github.com/mongodb/specifications/tree/master/source/bson-corpus | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =head1 ACKNOWLEDGMENTS | ||||||||||||||
| 171 | |||||||||||||||
| 172 | The development of this library has been partially sponsored by Connectivity, Inc. | ||||||||||||||
| 173 | |||||||||||||||
| 174 | =head1 AUTHOR | ||||||||||||||
| 175 | |||||||||||||||
| 176 | Adriano Ferreira <ferreira@cpan.org> | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 179 | |||||||||||||||
| 180 | This software is copyright (c) 2016 by Adriano Ferreira. | ||||||||||||||
| 181 | |||||||||||||||
| 182 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 183 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 184 | |||||||||||||||
| 185 | =cut | ||||||||||||||
| 186 | |||||||||||||||
| 187 | __END__ | ||||||||||||||