Skip to content

Commit c8b3ddb

Browse files
author
余化
committed
decode int with type
1 parent e1d02b9 commit c8b3ddb

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/v2/decoder.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,31 @@ utils.addByteCodes(BYTE_CODES, [
9393
* @return {Number}
9494
* @api public
9595
*/
96-
proto.readInt = function () {
96+
proto.readInt = function (withType) {
9797
var code = this.byteBuffer.get();
98+
var val;
9899
// Compact int
99100
if (code >= 0x80 && code <= 0xbf) {
100101
// Integers between -16 and 47 can be encoded by a single octet in the range x80 to xbf.
101102
// value = code - 0x90
102-
return code - 0x90;
103-
}
104-
if (code >= 0xc0 && code <= 0xcf) {
103+
val = code - 0x90;
104+
} else if (code >= 0xc0 && code <= 0xcf) {
105105
// Integers between -2048 and 2047 can be encoded in two octets with the leading byte in the range xc0 to xcf.
106106
// value = ((code - 0xc8) << 8) + b0;
107-
return ((code - 0xc8) << 8) + this.byteBuffer.get();
108-
}
109-
if (code >= 0xd0 && code <= 0xd7) {
107+
val = ((code - 0xc8) << 8) + this.byteBuffer.get();
108+
} else if (code >= 0xd0 && code <= 0xd7) {
110109
// Integers between -262144 and 262143 can be encoded in three bytes with the leading byte in the range xd0 to xd7.
111110
// value = ((code - 0xd4) << 16) + (b1 << 8) + b0;
112111
var b1 = this.byteBuffer.get();
113112
var b0 = this.byteBuffer.get();
114-
return ((code - 0xd4) << 16) + (b1 << 8) + b0;
115-
}
116-
if (code === 0x49) {
117-
return this.byteBuffer.getInt();
113+
val = ((code - 0xd4) << 16) + (b1 << 8) + b0;
114+
} else if (code === 0x49) {
115+
val = this.byteBuffer.getInt();
116+
} else {
117+
this.throwError('readInt', code);
118118
}
119-
120-
this.throwError('readInt', code);
119+
120+
return this.handleType('int', val, withType);
121121
};
122122

123123
utils.addByteCodes(BYTE_CODES, [

test/list.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('list.test.js', function () {
171171
hessian.encode([1, 2, 'foo'], '2.0').should.eql(utils.bytes('v2/list/untyped_list'));
172172
hessian.encode([], '2.0').should.eql(utils.bytes('v2/list/untyped_[]'));
173173

174-
hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0').should.eql([1, 2, 'foo']);
174+
hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0').should.eql([ { $: 1, $class: 'int' }, { $: 2, $class: 'int' }, 'foo' ]);
175175
hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([1, 2, 'foo']);
176176
hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0').should.eql(['1', '2', '3', '4', '5', '6', '7', '8']);
177177
hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0', true).should.eql(['1', '2', '3', '4', '5', '6', '7', '8']);

0 commit comments

Comments
 (0)