node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



Read/write simple output from Ruby's Marshal library.

How simple? I serialize everything with Marshal.dump and want to use it to interop with other langs for some reason

This is not for you. First of all, don't use Marshal for that. And second of all, no smoking.

F----- OFFFF

I want to exchange data with Ruby/Rails apps that store simple values in Marshal format

Now we're talking.

I've done basics like true, false and nil, Fixnums, Floats, strings that just contain ASCII/latin1 chars, arrays and hashes. Coverage of what can be serialized is far from exhaustive.

OK, That Suits My Needs


var marsha = require('marsha');

Marshaled data contains all sorts of nonprintable characters; you can provide it as a buffer:

var buffer = fs.readFileSync('./data/marshaled');

Or you can provide a string and an encoding, a buffer will be created internally.

marsha.load('BAhbCGkGaQdpCA==', 'base64'); // returns [ 1, 2, 3 ] 

The JS representation of the marshaled data will be returned, or an exception will be thrown if it's not usable. Marshal can serialize a wide variety of Ruby objects including instances of builtin and user defined classes, which may not have a JS equivalent. :symbols are treated as strings in JS land.

Writing is similar to reading, returns a buffer if called with one argument:

marsha.dump([ 1, 2, 3 ]); // returns <Buffer 04 08 5b 08 69 06 69 07 69 08> 

or a string if called with two:

marsha.dump([ 1, 2, 3 ], 'base64'); // returns 'BAhbCGkGaQdpCA==' 


npm test

Most coverage is generated from test/examples.json, which contains various hex-encoded outputs from asking Marshal to encode things in irb, and their JS equivalents.