diff --git a/lib/v1/decoder.js b/lib/v1/decoder.js index eed5e73..2aa2f22 100644 --- a/lib/v1/decoder.js +++ b/lib/v1/decoder.js @@ -529,10 +529,7 @@ proto.readList = proto.readArray; proto.readRef = function (withType) { var rid = this.readRefId(); var obj = this.refMap[rid]; - if (!withType && obj && utils.hasOwnProperty(obj, '$')) { - obj = obj.$; - } - return obj; + return handle(obj, withType); }; /** diff --git a/test/custom_handler.test.js b/test/custom_handler.test.js index 0f21cf1..5824af4 100644 --- a/test/custom_handler.test.js +++ b/test/custom_handler.test.js @@ -67,6 +67,32 @@ describe('utils.test.js', function () { assert.deepEqual(output, { value: '100.06' }); }); + it('should decode with custom handler if has ref', function () { + hessian.registerDecodeHandler('java.math.BigDecimal', function (result) { + return { + $class: result.$class, + $: result.$.value, + }; + }); + var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } }; + var map = new Map(); + map.set(1, o); + map.set(2, o); + var buf = hessian.encode({ + $class: 'java.util.HashMap', + $: map + }, '2.0'); + var output = hessian.decode(buf, '2.0'); + /** + * fix problem like ref object reuse + */ + assert(output[1] === '100.06'); + assert(output[2] === '100.06'); + hessian.deregisterDecodeHandler('java.math.BigDecimal'); + output = hessian.decode(buf, '2.0'); + assert.deepEqual(output, {'1': { value: '100.06'}, '2': { value: '100.06' }}); + }); + if (!supportES6Map) { return; }