Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Serialise to binary?

by xdg (Monsignor)
on Oct 26, 2015 at 15:55 UTC ( #1145982=note: print w/replies, xml ) Need Help??


in reply to Re^2: Serialise to binary?
in thread Serialise to binary?

they don't see to really care about having it being able to encode arbitrary data structures

I think that's an unkind assumption about intent. (N.B. I am the current maintainer.)

But like JSON, BSON is document-oriented, so is not designed to store raw arrays or scalars the way Storable or Sereal will. So in that sense, it might not be the right choice for your needs.

Beyond that however, the goal of BSON is to handle whatever you can throw at it as best as possible given the ambiguities mapping data between a dynamic, largely typeless language like Perl and a typed data format like BSON. Knowing that some Perl scalar is binary data and not an arbitrary string is impossible without some hints from the programmer.

The MongoDB::BSON implementation is in XS and has been part of the MongoDB driver distribution. We hope to eventually split it out so that it can be used independently where warranted.

The BSON.pm implementation is pure Perl and was originally developed outside MongoDB (but has since been adopted by the company). There are still some areas where it is not yet as good as MongoDB::BSON.

Even if BSON is not right for this particular problem, if anyone experiences bugs using either implementation, I encourage you to report them or at least email us about them so we can fix them.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^4: Serialise to binary?
by davido (Cardinal) on Oct 27, 2015 at 02:56 UTC

    Thanks for jumping into the discussion! :) It's always great to hear from a maintainer.

    I'm curious why BSON may not be the right choice. If passing a raw scalar or array is problematic, wouldn't this be a reasonable solution?

    encode({data => $some_scalar});

    Whether $some_scalar contains an array ref, a hash ref, or a flat scalar, encode would still see it as a hash based document.

    But I don't have a lot of experience with BSON. I remember seeing it presented a few years ago at a PerlMongers meeting in So. Cal, and it stuck in my head as a more portable solution for data serialization when the data might contain binary elements.

    I was a little surprised to see that there is no (documented) "TO_BSON" means of providing serialization procedures for objects. Is there any plan for that sort of hook in the future?

    (Sorry to hijack the thread!)


    Dave

      Wrapping an array in a hash adds some mild overhead, but otherwise should work. Unfortunately, for historical reasons, the spec for arrays in BSON is horribly inefficient (index-value pairs!) so I would not recommend it for storing large arrays.

      I have been considering supporting a TO_BSON method (or even just using TO_JSON, but don't want to do that until I have a broader think about the general case of mappings to and from BSON. (If you think about how JSON::XS is using Types::Serialiser, you can see the general case problem.)

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1145982]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2019-05-20 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (123 votes). Check out past polls.

    Notices?