Skip to content

Commit 44f322b

Browse files
committed
[#19, #21] Implement toJSON compatibility
1 parent 1b31f58 commit 44f322b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/msgpack.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ try {
1212
sys = require('sys');
1313
}
1414

15-
var pack = mpBindings.pack;
15+
var bpack = mpBindings.pack;
1616
var unpack = mpBindings.unpack;
1717

1818
exports.pack = pack;
1919
exports.unpack = unpack;
2020

21+
function pack() {
22+
var args = arguments, that, i;
23+
for (i = 0; i < arguments.length; i++) {
24+
that = args[i];
25+
if (that && typeof that === 'object' &&
26+
typeof that.toJSON === 'function') {
27+
args[i] = that.toJSON();
28+
}
29+
}
30+
return bpack.apply(null, args);
31+
}
32+
2133
var Stream = function(s) {
2234
var self = this;
2335

@@ -27,7 +39,7 @@ var Stream = function(s) {
2739
self.buf = null;
2840

2941
// Send a message down the stream
30-
//
42+
//
3143
// Allows the caller to pass additional arguments, which are passed
3244
// faithfully down to the write() method of the underlying stream.
3345
self.send = function(m) {

test/lib/msgpack.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,17 @@ exports.msgpack = {
192192
test.expect(4);
193193
// Object to test with
194194
var o = [1, 2, 3];
195-
195+
196196
// Create two buffers full of packed data, 'b' and 'bb', with the latter
197197
// containing 3 extra bytes
198198
var b = msgpack.pack(o);
199199
var bb = new Buffer(b.length + 3);
200200
b.copy(bb, 0, 0, b.length);
201-
201+
202202
// Expect no remaining bytes when unpacking 'b'
203203
test.deepEqual(msgpack.unpack(b), o);
204204
test.deepEqual(msgpack.unpack.bytes_remaining, 0);
205-
205+
206206
// Expect 3 remaining bytes when unpacking 'bb'
207207
test.deepEqual(msgpack.unpack(bb), o);
208208
test.equal(msgpack.unpack.bytes_remaining, 3);
@@ -221,5 +221,27 @@ exports.msgpack = {
221221
test.ok(false, e.message);
222222
}
223223
test.done();
224+
},
225+
'test toJSON compatibility' : function (test) {
226+
var expect = { msg: 'hello world' };
227+
var subject = { toJSON: function() { return expect; }};
228+
test.expect(1);
229+
test.deepEqual(expect, msgpack.unpack(msgpack.pack(subject)));
230+
test.done();
231+
},
232+
'test toJSON compatibility for multiple args' : function (test) {
233+
var expect = { msg: 'hello world' };
234+
var subject = { toJSON: function() { return expect; }};
235+
var subject1 = { toJSON: function() { msg: 'goodbye world' }};
236+
test.expect(1);
237+
test.deepEqual(expect, msgpack.unpack(msgpack.pack(subject, subject1)));
238+
test.done();
239+
},
240+
'test toJSON compatibility with prototype' : function (test) {
241+
var expect = { msg: 'hello world' };
242+
var subject = { __proto__: { toJSON: function() { return expect; }}};
243+
test.expect(1);
244+
test.deepEqual(expect, msgpack.unpack(msgpack.pack(subject)));
245+
test.done();
224246
}
225247
};

0 commit comments

Comments
 (0)