Parse a Ruby Marshal data to a JavaScript value.
data
{string | Uint8Array | ArrayBuffer} The Marshal data.loadOptions
{Object} Parse options.
Force decode or not decode string values.
data = Marshal.dump(["foo", "foo".force_encoding("binary")])
load(data); // => ["foo", { __type: "bytes", data: [102, 111, 111] }]
load(data, { stringMode: "utf8" }); // => ["foo", "foo"]
load(data, { stringMode: "binary" }); // => [{ __type: "bytes", data: [102, 111, 111] }, { __type: "bytes", data: [102, 111, 111] }]
If set, changes "@" instance variables prefix to passed.
If you've loaded files with this option, also use it in dump.
load(data); // => [{ "__symbol__@instanceVar": 5 }]
load(data, { instanceVarPrefix: "" }); // => [{ "__symbol__instanceVar": 5 }]
load(data, { instanceVarPrefix: "_" }); // => [{ "__symbol___instanceVar": 5 }]
Decode Ruby objects as same-class JavaScript objects.
class A end
data = Marshal.dump(A.new)
class A {}
load(data); // => RubyObject { class: Symbol(A) }
load(data, { decodeKnown: { A } }); // => A {}
Encode a JavaScript value into Ruby marshal data. Returns a Uint8Array
.
Note that the Uint8Array
may not always be the same length as its underlying buffer.
You should always check the byteOffset
and byteLength
when accessing the buffer.
value
{unknown} The JavaScript value.dumpOptions
{Object} Encode options.
Set it to the same value as in load().
Encode JavaScript objects into same-name Ruby objects.
class A {}
dump(new A()); // Error: Cannot dump type object of object [object Object]
dump(new A(), { encodeKnown: { A } }); // => ruby: #<A>
This is an alter to the error case of dumpOptions.encodeKnown
.
It should return a string indicating the Ruby class name to encode into.
If you return null
or empty string, it fallbacks to throw the error.
dump(new A(), { encodeUnknown: (a) => a.constructor?.name }); // => ruby: #<A>
If set, force maximum size of the output buffer to passed. Passed number should be bytes (!) number. Default value is 16000000 (16 MB).
dump(hugeStructure); // => RangeError: ArrayBuffer.prototype.resize: Invalid length parameter
dump(hugeStructure, { maxByteLength: 32000000 }); // => All good