Skip to content

Commit c403d27

Browse files
author
余化
committed
decode int with type
1 parent 77f4fe4 commit c403d27

File tree

3 files changed

+78
-23
lines changed

3 files changed

+78
-23
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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ describe('list.test.js', function () {
172172
hessian.encode([], '2.0').should.eql(utils.bytes('v2/list/untyped_[]'));
173173

174174
hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0').should.eql([1, 2, 'foo']);
175-
hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([1, 2, 'foo']);
175+
hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([{$: 1, $class: 'int'}, {$: 2, $class: 'int'}, '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']);
178178
hessian.decode(utils.bytes('v2/list/untyped_[]'), '2.0').should.eql([]);
@@ -238,7 +238,23 @@ describe('list.test.js', function () {
238238

239239
hessian.decode(utils.bytes('v2/list/[int'), '2.0').should.eql([1, 2, 3]);
240240
// encode again should use type cache
241-
hessian.decode(utils.bytes('v2/list/[int'), '2.0', true).should.eql(list);
241+
hessian.decode(utils.bytes('v2/list/[int'), '2.0', true).should.eql({
242+
$class: '[int',
243+
$: [
244+
{
245+
$class: 'int',
246+
$: 1
247+
},
248+
{
249+
$class: 'int',
250+
$: 2
251+
},
252+
{
253+
$class: 'int',
254+
$: 3
255+
}
256+
]
257+
});
242258

243259
var strs = {
244260
$class: '[string',

test/object.test.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,10 @@ describe('object.test.js', function () {
402402
ctx: {
403403
$class: 'hessian.ConnectionRequest$RequestContext',
404404
$: {
405-
id: 101,
405+
id: {
406+
$class: 'int',
407+
$: 101
408+
},
406409
// 'this$0': null
407410
}
408411
}
@@ -551,7 +554,16 @@ describe('object.test.js', function () {
551554
var buf = hessian.encode(obj, '2.0');
552555
buf[0].should.equal(0x43);
553556
hessian.decode(buf, '2.0').should.eql(obj.$);
554-
hessian.decode(buf, '2.0', true).should.eql(obj);
557+
hessian.decode(buf, '2.0', true).should.eql({
558+
$class: 'hessian.test.demo.Car',
559+
$: {
560+
a: {
561+
$class: 'int',
562+
$: 1,
563+
},
564+
b: 'map',
565+
},
566+
});
555567
});
556568

557569
it('should read one car list', function () {
@@ -574,7 +586,11 @@ describe('object.test.js', function () {
574586
b: 'b',
575587
model: 'model 1',
576588
color: 'aquamarine',
577-
mileage: 65536 }
589+
mileage: {
590+
$class: 'int',
591+
$: 65536,
592+
},
593+
}
578594
}
579595
]);
580596

@@ -607,7 +623,11 @@ describe('object.test.js', function () {
607623
b: 'b',
608624
model: 'model 1',
609625
color: 'aquamarine',
610-
mileage: 65536 }
626+
mileage: {
627+
$class: 'int',
628+
$: 65536,
629+
},
630+
}
611631
},
612632
{
613633
$class: 'hessian.demo.Car',
@@ -617,7 +637,11 @@ describe('object.test.js', function () {
617637
b: 'b',
618638
model: 'model 2',
619639
color: 'aquamarine',
620-
mileage: 65536 }
640+
mileage: {
641+
$class: 'int',
642+
$: 65536,
643+
},
644+
}
621645
}
622646
]);
623647

@@ -661,23 +685,38 @@ describe('object.test.js', function () {
661685
b: 'b',
662686
model: 'model 1',
663687
color: 'aquamarine',
664-
mileage: 65536 } },
688+
mileage: {
689+
$class: 'int',
690+
$: 65536,
691+
},
692+
}
693+
},
665694
{ '$class': 'hessian.demo.Car',
666695
'$':
667696
{ a: 'a',
668697
c: 'c',
669698
b: 'b',
670699
model: 'model 2',
671700
color: 'aquamarine',
672-
mileage: 65536 } },
701+
mileage: {
702+
$class: 'int',
703+
$: 65536,
704+
},
705+
}
706+
},
673707
{ '$class': 'hessian.demo.Car',
674708
'$':
675709
{ a: 'a',
676710
c: 'c',
677711
b: 'b',
678712
model: 'model 3',
679713
color: 'aquamarine',
680-
mileage: 65536 } }
714+
mileage: {
715+
$class: 'int',
716+
$: 65536,
717+
},
718+
}
719+
}
681720
]);
682721

683722
hessian.encode(cars, '2.0').should.eql(utils.bytes('v2/map/car_list'));

0 commit comments

Comments
 (0)