View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
MongoDB Inc > BSON > BSON



Annotate this POD


View/Report Bugs
Module Version: v1.6.1   Source  


BSON - BSON serialization and deserialization


version v1.6.1


    use BSON;
    use BSON::Types ':all';
    use boolean;

    my $codec = BSON->new;

    my $document = {
        _id             => bson_oid(),
        creation_time   => bson_time(),
        zip_code        => bson_string("08544"),
        hidden          => false,

    my $bson = $codec->encode_one( $document );
    my $doc  = $codec->decode_one( $bson     );


This class implements a BSON encoder/decoder ("codec"). It consumes "documents" (typically hash references) and emits BSON strings and vice versa in accordance with the BSON Specification.

BSON is the primary data representation for MongoDB. While this module has several features that support MongoDB-specific needs and conventions, it can be used as a standalone serialization format.

The codec may be customized through attributes on the codec option as well as encode/decode specific options on methods:

    my $codec = BSON->new( \%global_attributes );

    my $bson = $codec->encode_one( $document, \%encode_options );
    my $doc  = $codec->decode_one( $bson    , \%decode_options );

Because BSON is strongly-typed and Perl is not, this module supports a number of "type wrappers" – classes that wrap Perl data to indicate how they should serialize. The BSON::Types module describes these and provides associated helper functions. See "PERL-BSON TYPE MAPPING" for more details.

When decoding, type wrappers are used for any data that has no native Perl representation. Optionally, all data may be wrapped for precise control of round-trip encoding.

Please read the configuration attributes carefully to understand more about how to control encoding and decoding.

At compile time, this module will select an implementation backend. It will prefer BSON::XS (released separately) if available, or will fall back to BSON::PP (bundled with this module). See "ENVIRONMENT" for a way to control the selection of the backend.



This attribute specifies a function reference that will be called with three positional arguments:

Note: for decoding errors, the byte-string is passed as a reference to avoid copying possibly large strings.

If not provided, errors messages will be thrown with Carp::croak.


A string containing ASCII characters that must not appear in keys. The default is the empty string, meaning there are no invalid characters.


This attribute defines the maximum document size. The default is 0, which disables any maximum.

If set to a positive number, it applies to both encoding and decoding (the latter is necessary for prevention of resource consumption attacks).


This is a single character to use for special MongoDB-specific query operators. If a key starts with op_char, the op_char character will be replaced with "$".

The default is "$", meaning that no replacement is necessary.


If set to a true value, then decoding will return a reference to a tied hash that preserves key order. Otherwise, a regular (unordered) hash reference will be returned.


The default is false.


When false, scalar values will be encoded as a number if they were originally a number or were ever used in a numeric context. However, a string that looks like a number but was never used in a numeric context (e.g. "42") will be encoded as a string.

If prefer_numeric is set to true, the encoder will attempt to coerce strings that look like a number into a numeric value. If the string doesn't look like a double or integer, it will be encoded as a string.

IMPORTANT CAVEAT: the heuristics for determining whether something is a string or number are less accurate on older Perls. See BSON::Types for wrapper classes that specify exact serialization types.

The default is false.


If set to true, during decoding, documents with the fields '$id' and '$ref' (literal dollar signs, not variables) will be wrapped as BSON::DBRef objects. If false, they are decoded into ordinary hash references (or ordered hashes, if ordered is true).

The default is true.


If set to true, during decoding, numeric values will be wrapped into BSON type-wrappers: BSON::Double, BSON::Int64 or BSON::Int32. While very slow, this can help ensure fields can round-trip if unmodified.

The default is false.


If set to true, during decoding, string values will be wrapped into a BSON type-wrappers, BSON::String. While very slow, this can help ensure fields can round-trip if unmodified.

The default is false.

dt_type (Discouraged)

Sets the type of object which is returned for BSON DateTime fields. The default is undef, which returns objects of type BSON::Time. This is overloaded to be the integer epoch value when used as a number or string, so is somewhat backwards compatible with dt_type in the MongoDB driver.

Other acceptable values are BSON::Time (explicitly), DateTime, Time::Moment, DateTime::Tiny, Mango::BSON::Time.

Because BSON::Time objects have methods to convert to DateTime, Time::Moment or DateTime::Tiny, use of this field is discouraged. Users should use these methods on demand. This option is provided for backwards compatibility only.



    $byte_string = $codec->encode_one( $doc );
    $byte_string = $codec->encode_one( $doc, \%options );

Takes a "document", typically a hash reference, an array reference, or a Tie::IxHash object and returns a byte string with the BSON representation of the document.

An optional hash reference of options may be provided. Valid options include:


    $doc = $codec->decode_one( $byte_string );
    $doc = $codec->decode_one( $byte_string, \%options );

Takes a byte string with a BSON-encoded document and returns a hash reference representing the decoded document.

An optional hash reference of options may be provided. Valid options include:


    $copy = $codec->clone( ordered => 1 );

Constructs a copy of the original codec, but allows changing attributes in the copy.


    $oid = BSON->create_oid;

This class method returns a new BSON::OID. This abstracts OID generation away from any specific Object ID class and makes it an interface on a BSON codec. Alternative BSON codecs should define a similar class method that returns an Object ID of whatever type is appropriate.


    use JSON::MaybeXS;
    $data = decode_json( $json_string );
    $bson->inflate_extjson( $data );

Given a hash reference, this method walks the hash, replacing any MongoDB extended JSON items with BSON type-wrapper equivalents. Additionally, any JSON boolean objects (e.g. JSON::PP::Boolean) will be replaced with true or false values.



    my $bson = encode({ bar => 'foo' }, \%options);

This is the legacy, functional interface and is only exported on demand. It takes a hashref and returns a BSON string. It uses an internal codec singleton with default attributes.


    my $hash = decode( $bson, \%options );

This is the legacy, functional interface and is only exported on demand. It takes a BSON string and returns a hashref. It uses an internal codec singleton with default attributes.


BSON has numerous data types and Perl does not.

When decoding, each BSON type should result in a single, predictable Perl type. Where no native Perl type is appropriate, BSON decodes to an object of a particular class (a "type wrapper").

When encoding, for historical reasons, there may be many Perl representations that should encode to a particular BSON type. For example, all the popular "boolean" type modules on CPAN should encode to the BSON boolean type. Likewise, as this module is intended to supersede the type wrappers that have shipped with the MongoDB module, those type wrapper are supported by this codec.

The table below describes the BSON/Perl mapping for both encoding and decoding.

On the left are all the Perl types or classes this BSON codec knows how to serialize to BSON. The middle column is the BSON type for each class. The right-most column is the Perl type or class that the BSON type deserializes to. Footnotes indicate variations or special behaviors.

    Perl type/class ->          BSON type        -> Perl type/class
    float[1]                    0x01 DOUBLE         float[2]
    string[3]                   0x02 UTF8           string[2]
    hashref                     0x03 DOCUMENT       hashref[4][5]
    arrayref                    0x04 ARRAY          arrayref
    BSON::Bytes                 0x05 BINARY         BSON::Bytes
    n/a                         0x06 UNDEFINED[d]   undef
    BSON::OID                   0x07 OID            BSON::OID
    boolean                     0x08 BOOL           boolean
    BSON::Time                  0x09 DATE_TIME      BSON::Time
    undef                       0x0a NULL           undef
    BSON::Regex                 0x0b REGEX          BSON::Regex
    qr// reference
    n/a                         0x0c DBPOINTER[d]   BSON::DBRef
    BSON::Code[6]               0x0d CODE           BSON::Code
    n/a                         0x0e SYMBOL[d]      string
    BSON::Code[6]               0x0f CODEWSCOPE     BSON::Code
    integer[7][8]               0x10 INT32          integer[2]
    BSON::Timestamp             0x11 TIMESTAMP      BSON::Timestamp
    integer[7]                  0x12 INT64          integer[2][9]
    BSON::MaxKey                0x7F MAXKEY         BSON::MaxKey
    BSON::MinKey                0xFF MINKEY         BSON::MinKey

    [d] Deprecated or soon to be deprecated.
    [1] Scalar with "NV" internal representation no "PV"
        representation, or a string that looks like a float if the
        'prefer_numeric' option is true.
    [2] If the 'wrap_numbers' option is true, numeric types will be wrapped
        as BSON::Double, BSON::Int32 or BSON::Int64 as appropriate to ensure
        round-tripping. If the 'wrap_strings' option is true, strings will
        be wrapped as BSON::String, likewise.
    [3] Scalar with "PV" representation and not identified as a number
        by notes [1] or [7].
    [4] If 'ordered' option is set, will return a tied hash that preserves
        order (deprecated 'ixhash' option still works).
    [5] If the document appears to contain a DBRef and a 'dbref_callback'
        exists, that callback is executed with the deserialized document.
    [6] Code is serialized as CODE or CODEWSCOPE depending on whether a
        scope hashref exists in BSON::Code/MongoDB::Code.
    [7] Scalar with "IV" internal representation and no "PV"
        representation, or a string that looks like an integer if the
        'prefer_numeric' option is true.
    [8] Only if the integer fits in 32 bits.
    [9] On 32-bit platforms, 64-bit integers are deserialized to
        Math::BigInt objects (even if subsequently wrapped into
        BSON::Int64 if 'wrap_scalars' is true).


Threads are never recommended in Perl, but this module is thread safe for Perl 5.8.5 or later. Threads are not supported on older Perls.



Starting with BSON v0.999.0, this module is using a "tick-tock" three-part version-tuple numbering scheme: vX.Y.Z


This module was originally written by Stefan G. In 2014, he graciously transferred ongoing maintenance to MongoDB, Inc.

The bson_xxxx helper functions in BSON::Types were inspired by similar work in Mango::BSON by Sebastian Riedel.


Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license.

  git clone




This software is Copyright (c) 2018 by Stefan G. and MongoDB, Inc.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004
syntax highlighting: