Skip to content

Commit d4b0880

Browse files
author
余化
committed
decode date with type
1 parent 1e81c39 commit d4b0880

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/v2/decoder.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,18 @@ utils.addByteCodes(BYTE_CODES, [
292292
* @return {Date}
293293
* @api public
294294
*/
295-
proto.readDate = function () {
295+
proto.readDate = function (withType) {
296296
var code = this.byteBuffer.get();
297+
var val;
297298
if (code === 0x4a) {
298-
return new Date(utils.handleLong(this.byteBuffer.getLong()));
299-
}
300-
if (code === 0x4b) {
301-
return new Date(this.byteBuffer.getUInt32() * 60000);
299+
val = new Date(utils.handleLong(this.byteBuffer.getLong()));
300+
} else if (code === 0x4b) {
301+
val = new Date(this.byteBuffer.getUInt32() * 60000);
302+
} else {
303+
this.throwError('readDate', code);
302304
}
303305

304-
this.throwError('readDate', code);
306+
return this.handleType('date', val, withType);
305307
};
306308

307309
utils.addByteCodes(BYTE_CODES, [

test/date.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ describe('date.test.js', function () {
2828
d.getTime().should.equal(894621091000);
2929
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
3030
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
31+
hessian.decode(dateBuffer, true).should.eql({
32+
$class: 'java.util.Date',
33+
$: new Date(894621091000)
34+
});
3135
});
3236

3337
it('should write date 2:51:31 May 8, 1998', function () {
3438
hessian.encode(new Date(894621091000)).should.eql(dateBuffer);
39+
hessian.encode({$class: 'java.util.Date', $:new Date(894621091000)}).should.eql(dateBuffer);
3540
});
3641

3742
it('should write date 0 and read', function () {
3843
hessian.encode(new Date(0)).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
44+
hessian.encode({$class: 'java.util.Date', $: new Date(0)}).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
3945
});
4046

4147
it('should read date 09:51:31 May 8, 1998 UTC', function () {
@@ -45,6 +51,8 @@ describe('date.test.js', function () {
4551
d.getTime().should.equal(894621091000);
4652
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
4753
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
54+
55+
hessian.decode(utils.bytes('v1/date/894621091000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)});
4856
});
4957

5058
it('should read date 09:51:00 May 8, 1998 UTC', function () {
@@ -54,13 +62,17 @@ describe('date.test.js', function () {
5462
d.getTime().should.equal(894621060000);
5563
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
5664
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');
65+
66+
hessian.decode(utils.bytes('v1/date/894621060000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)});
5767
});
5868

5969
it('should write date', function () {
6070
var now = new Date(1398280514000);
6171
hessian.encode(now, '1.0').should.eql(utils.bytes('v1/date/now'));
72+
hessian.encode({$class: 'java.util.Date', $: now}, '1.0').should.eql(utils.bytes('v1/date/now'));
6273
// read it
6374
hessian.decode(utils.bytes('v1/date/now'), '1.0').should.eql(now);
75+
hessian.decode(utils.bytes('v1/date/now'), '1.0', true).should.eql({$class: 'java.util.Date', $: now});
6476
});
6577

6678
describe('hessian 2.0', function () {
@@ -71,6 +83,8 @@ describe('date.test.js', function () {
7183
d.getTime().should.equal(894621091000);
7284
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
7385
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
86+
87+
hessian.decode(utils.bytes('v2/date/894621091000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)});
7488
});
7589

7690
it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function () {
@@ -80,13 +94,17 @@ describe('date.test.js', function () {
8094
d.getTime().should.equal(894621060000);
8195
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
8296
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');
97+
98+
hessian.decode(utils.bytes('v2/date/894621060000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)});
8399
});
84100

85101
it('should write and read date', function () {
86102
var now = new Date(1398280514000);
87103
hessian.encode(now, '2.0').should.eql(utils.bytes('v2/date/now'));
104+
hessian.encode({$class: 'java.util.Date', $: now}, '2.0').should.eql(utils.bytes('v2/date/now'));
88105
// read it
89106
hessian.decode(utils.bytes('v2/date/now'), '2.0').should.eql(now);
107+
hessian.decode(utils.bytes('v2/date/now'), '2.0', true).should.eql({$class: 'java.util.Date', $: now});
90108
});
91109

92110
// it('should read 1.0 format', function () {

0 commit comments

Comments
 (0)