-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom decode handler (#119)
- Loading branch information
Showing
7 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ module.exports = { | |
'istanbul', | ||
], | ||
semver: [ | ||
'byte@1', | ||
'debug@3', | ||
'mocha@3', | ||
], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
|
||
var handlers = {}; | ||
|
||
function deregisterDecodeHandler(className) { | ||
delete handlers[className]; | ||
} | ||
|
||
function registerDecodeHandler(className, handler) { | ||
assert(typeof handler === 'function', 'handler should be a function'); | ||
handlers[className] = handler; | ||
} | ||
|
||
function handle(result, withType) { | ||
var className = result.$class; | ||
var handler = handlers[className]; | ||
|
||
if (handler) { | ||
result = handler(result); | ||
} | ||
return withType ? result : result.$; | ||
} | ||
|
||
exports.handle = handle; | ||
exports.registerDecodeHandler = registerDecodeHandler; | ||
exports.deregisterDecodeHandler = deregisterDecodeHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var hessian = require('../'); | ||
var supportES6Map = require('../lib/utils').supportES6Map; | ||
|
||
describe('utils.test.js', function () { | ||
describe('v1.0', function () { | ||
it('should decode with custom handler', function () { | ||
hessian.registerDecodeHandler('java.math.BigDecimal', function (result) { | ||
return { | ||
$class: result.$class, | ||
$: result.$.value, | ||
}; | ||
}); | ||
var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } }; | ||
var buf = hessian.encode(o, '1.0'); | ||
var output = hessian.decode(buf, '1.0'); | ||
assert(output === '100.06'); | ||
hessian.deregisterDecodeHandler('java.math.BigDecimal'); | ||
output = hessian.decode(buf, '1.0'); | ||
assert.deepEqual(output, { value: '100.06' }); | ||
}); | ||
|
||
if (!supportES6Map) { | ||
return; | ||
} | ||
|
||
it('should decode map with custom handler', function () { | ||
hessian.registerDecodeHandler('java.util.HashMap', function (result) { | ||
return { | ||
$class: result.$class, | ||
$: result.$.$map, | ||
}; | ||
}); | ||
var map = new Map(); | ||
map.set(1, 'fee'); | ||
map.set(2, 'fie'); | ||
map.set(3, 'foe'); | ||
var buf = hessian.encode({ | ||
$class: 'java.util.HashMap', | ||
$: map | ||
}, '1.0'); | ||
var output = hessian.decode(buf, '1.0'); | ||
assert(output instanceof Map); | ||
assert(output.get(1) === 'fee'); | ||
assert(output.get(2) === 'fie'); | ||
assert(output.get(3) === 'foe'); | ||
hessian.deregisterDecodeHandler('java.util.HashMap'); | ||
}); | ||
}); | ||
|
||
describe('v2.0', function () { | ||
it('should decode with custom handler', function () { | ||
hessian.registerDecodeHandler('java.math.BigDecimal', function (result) { | ||
return { | ||
$class: result.$class, | ||
$: result.$.value, | ||
}; | ||
}); | ||
var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } }; | ||
var buf = hessian.encode(o, '2.0'); | ||
var output = hessian.decode(buf, '2.0'); | ||
assert(output === '100.06'); | ||
hessian.deregisterDecodeHandler('java.math.BigDecimal'); | ||
output = hessian.decode(buf, '2.0'); | ||
assert.deepEqual(output, { value: '100.06' }); | ||
}); | ||
|
||
if (!supportES6Map) { | ||
return; | ||
} | ||
|
||
it('should decode map with custom handler', function () { | ||
hessian.registerDecodeHandler('java.util.HashMap', function (result) { | ||
return { | ||
$class: result.$class, | ||
$: result.$.$map, | ||
}; | ||
}); | ||
var map = new Map(); | ||
map.set(1, 'fee'); | ||
map.set(2, 'fie'); | ||
map.set(3, 'foe'); | ||
var buf = hessian.encode({ | ||
$class: 'java.util.HashMap', | ||
$: map | ||
}, '2.0'); | ||
var output = hessian.decode(buf, '2.0'); | ||
assert(output instanceof Map); | ||
assert(output.get(1) === 'fee'); | ||
assert(output.get(2) === 'fie'); | ||
assert(output.get(3) === 'foe'); | ||
hessian.deregisterDecodeHandler('java.util.HashMap'); | ||
}); | ||
}); | ||
}); |