http://www.perlmonks.org?node_id=1146049


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

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

Replies are listed 'Best First'.
Re^5: Serialise to binary?
by xdg (Monsignor) on Nov 04, 2015 at 14:51 UTC

    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.