From 77f4fe4b62332addb0819180d8dd8e04f318f5dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=8C=96?= Date: Wed, 21 Oct 2015 01:07:00 +0800 Subject: [PATCH 1/5] decode long with type --- lib/v2/decoder.js | 29 ++++++++++++++--------------- test/long.test.js | 33 +++++++++++++++++++++++++++++++++ test/object.test.js | 10 ++++++++-- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index 51a82c5..a72e3e9 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -170,35 +170,34 @@ utils.addByteCodes(BYTE_CODES, [ * @return {Number} * @api public */ -proto.readLong = function () { +proto.readLong = function (withType) { var code = this.byteBuffer.get(); + var val; // Compact long if (code >= 0xd8 && code <= 0xef) { // Longs between -8 and 15 are represented by a single octet in the range xd8 to xef. // value = (code - 0xe0) - return code - 0xe0; - } - if (code >= 0xf0 && code <= 0xff) { + val = code - 0xe0; + } else if (code >= 0xf0 && code <= 0xff) { // Longs between -2048 and 2047 are encoded in two octets with the leading byte in the range xf0 to xff. // value = ((code - 0xf8) << 8) + b0 - return ((code - 0xf8) << 8) + this.byteBuffer.get(); - } - if (code >= 0x38 && code <= 0x3f) { + val = ((code - 0xf8) << 8) + this.byteBuffer.get(); + } else if (code >= 0x38 && code <= 0x3f) { // Longs between -262144 and 262143 are encoded in three octets with the leading byte in the range x38 to x3f. // value = ((code - 0x3c) << 16) + (b1 << 8) + b0 var b1 = this.byteBuffer.get(); var b0 = this.byteBuffer.get(); - return ((code - 0x3c) << 16) + (b1 << 8) + b0; - } - if (code === 0x59) { + val = ((code - 0x3c) << 16) + (b1 << 8) + b0; + } else if (code === 0x59) { // 32-bit integer cast to long - return this.byteBuffer.getInt32(); - } - if (code === 0x4c) { - return utils.handleLong(this.byteBuffer.getLong()); + val = this.byteBuffer.getInt32(); + } else if (code === 0x4c) { + val = utils.handleLong(this.byteBuffer.getLong()); + } else { + this.throwError('readLong', code); } - this.throwError('readLong', code); + return this.handleType('long', val, withType) }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/long.test.js b/test/long.test.js index b792bff..b7ee01f 100644 --- a/test/long.test.js +++ b/test/long.test.js @@ -32,6 +32,7 @@ describe('long.test.js', function () { it('should read long 300', function () { hessian.decode(longBuffer).should.equal(300); + hessian.decode(longBuffer, true).should.eql({$class: 'long', $: 300}); }); it('should write long 300', function () { @@ -53,14 +54,19 @@ describe('long.test.js', function () { it('should write and read equal java impl', function () { hessian.encode(java.long(0), '1.0').should.eql(utils.bytes('v1/long/0')); hessian.decode(utils.bytes('v1/long/0')).should.equal(0); + hessian.decode(utils.bytes('v1/long/0'), true).should.eql({$class: 'long', $: 0}); hessian.encode(java.long(-8), '1.0').should.eql(utils.bytes('v1/long/-8')); hessian.decode(utils.bytes('v1/long/-8')).should.equal(-8); + hessian.decode(utils.bytes('v1/long/-8'), true).should.eql({$class: 'long', $: -8}); hessian.encode(java.long(-7), '1.0').should.eql(utils.bytes('v1/long/-7')); hessian.decode(utils.bytes('v1/long/-7')).should.equal(-7); + hessian.decode(utils.bytes('v1/long/-7'), true).should.eql({$class: 'long', $: -7}); hessian.encode(java.long(15), '1.0').should.eql(utils.bytes('v1/long/15')); hessian.decode(utils.bytes('v1/long/15')).should.equal(15); + hessian.decode(utils.bytes('v1/long/15'), true).should.eql({$class: 'long', $: 15}); hessian.encode(java.long(14), '1.0').should.eql(utils.bytes('v1/long/14')); hessian.decode(utils.bytes('v1/long/14')).should.equal(14); + hessian.decode(utils.bytes('v1/long/14'), true).should.eql({$class: 'long', $: 14}); hessian.encode(java.long(-9), '1.0').should.eql(utils.bytes('v1/long/-9')); hessian.decode(utils.bytes('v1/long/-9')).should.equal(-9); hessian.encode(java.long(16), '1.0').should.eql(utils.bytes('v1/long/16')); @@ -81,6 +87,7 @@ describe('long.test.js', function () { hessian.decode(utils.bytes('v1/long/-2049')).should.equal(-2049); hessian.encode(java.long(-2147483648), '1.0').should.eql(utils.bytes('v1/long/-2147483648')); hessian.decode(utils.bytes('v1/long/-2147483648')).should.equal(-2147483648); + hessian.decode(utils.bytes('v1/long/-2147483648'), true).should.eql({$class: 'long', $: -2147483648}); hessian.encode(java.long(-2147483647), '1.0').should.eql(utils.bytes('v1/long/-2147483647')); hessian.decode(utils.bytes('v1/long/-2147483647')).should.equal(-2147483647); hessian.encode(java.long(2147483647), '1.0').should.eql(utils.bytes('v1/long/2147483647')); @@ -89,6 +96,7 @@ describe('long.test.js', function () { hessian.decode(utils.bytes('v1/long/2147483646')).should.equal(2147483646); hessian.encode(java.long(2147483648), '1.0').should.eql(utils.bytes('v1/long/2147483648')); hessian.decode(utils.bytes('v1/long/2147483648')).should.equal(2147483648); + hessian.decode(utils.bytes('v1/long/2147483648'), true).should.eql({$class: 'long', $: 2147483648}); }); describe('v2.0', function () { @@ -96,29 +104,35 @@ describe('long.test.js', function () { hessian.decode(new Buffer([0xe0]), '2.0').should.equal(0); hessian.decode(new Buffer([0xd8]), '2.0').should.equal(-8); hessian.decode(new Buffer([0xef]), '2.0').should.equal(15); + hessian.decode(new Buffer([0xef]), '2.0', true).should.eql({$class: 'long', $: 15}); hessian.decode(new Buffer([0xf8, 0x00]), '2.0').should.equal(0); hessian.decode(new Buffer([0xf0, 0x00]), '2.0').should.equal(-2048); + hessian.decode(new Buffer([0xf0, 0x00]), '2.0', true).should.eql({$class: 'long', $: -2048}); hessian.decode(new Buffer([0xf7, 0x00]), '2.0').should.equal(-256); hessian.decode(new Buffer([0xff, 0xff]), '2.0').should.equal(2047); hessian.decode(new Buffer([0x3c, 0x00, 0x00]), '2.0').should.equal(0); hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0').should.equal(-262144); hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0').should.equal(262143); + hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0', true).should.eql({$class: 'long', $: 262143}); // four octet longs hessian.decode(new Buffer([0x59, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0); hessian.decode(new Buffer([0x59, 0x00, 0x00, 0x01, 0x2c]), '2.0').should.equal(300); hessian.decode(new Buffer([0x59, 0x7f, 0xff, 0xff, 0xff]), '2.0').should.equal(2147483647); hessian.decode(new Buffer([0x59, 0x80, 0x00, 0x00, 0x00]), '2.0').should.equal(-2147483648); + hessian.decode(new Buffer([0x59, 0x80, 0x00, 0x00, 0x00]), '2.0', true).should.eql({$class: 'long', $: -2147483648}); hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0); hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2c]), '2.0').should.equal(300); hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff]), '2.0').should.equal(2147483647); + hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff]), '2.0', true).should.eql({$class: 'long', $: 2147483647}); }); it('should read normal long', function () { hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0').should.equal(2147483648); + hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0', true).should.eql({$class: 'long', $: 2147483648}); }); it('should write compact long', function () { @@ -353,45 +367,64 @@ describe('long.test.js', function () { it('should write and read equal java impl', function () { hessian.encode(java.long(0), '2.0').should.eql(utils.bytes('v2/long/0')); hessian.decode(utils.bytes('v2/long/0'), '2.0').should.equal(0); + hessian.decode(utils.bytes('v2/long/0'), '2.0', true).should.eql({ $: 0, $class: 'long' }); hessian.encode(java.long(-8), '2.0').should.eql(utils.bytes('v2/long/-8')); hessian.decode(utils.bytes('v2/long/-8'), '2.0').should.equal(-8); + hessian.decode(utils.bytes('v2/long/-8'), '2.0', true).should.eql({ $: -8, $class: 'long' }); hessian.encode(java.long(-7), '2.0').should.eql(utils.bytes('v2/long/-7')); hessian.decode(utils.bytes('v2/long/-7'), '2.0').should.equal(-7); + hessian.decode(utils.bytes('v2/long/-7'), '2.0', true).should.eql({ $: -7, $class: 'long' }); hessian.encode(java.long(15), '2.0').should.eql(utils.bytes('v2/long/15')); hessian.decode(utils.bytes('v2/long/15'), '2.0').should.equal(15); + hessian.decode(utils.bytes('v2/long/15'), '2.0', true).should.eql({ $: 15, $class: 'long' }); hessian.encode(java.long(14), '2.0').should.eql(utils.bytes('v2/long/14')); hessian.decode(utils.bytes('v2/long/14'), '2.0').should.equal(14); + hessian.decode(utils.bytes('v2/long/14'), '2.0', true).should.eql({ $: 14, $class: 'long' }); hessian.encode(java.long(-9), '2.0').should.eql(utils.bytes('v2/long/-9')); hessian.decode(utils.bytes('v2/long/-9'), '2.0').should.equal(-9); + hessian.decode(utils.bytes('v2/long/-9'), '2.0', true).should.eql({ $: -9, $class: 'long' }); hessian.encode(java.long(16), '2.0').should.eql(utils.bytes('v2/long/16')); hessian.decode(utils.bytes('v2/long/16'), '2.0').should.equal(16); + hessian.decode(utils.bytes('v2/long/16'), '2.0', true).should.eql({ $: 16, $class: 'long' }); hessian.encode(java.long(255), '2.0').should.eql(utils.bytes('v2/long/255')); hessian.encode(java.long(Long.fromNumber(255)), '2.0').should.eql(utils.bytes('v2/long/255')); hessian.encode(Long.fromNumber(255), '2.0').should.eql(utils.bytes('v2/long/255')); hessian.decode(utils.bytes('v2/long/255'), '2.0').should.equal(255); + hessian.decode(utils.bytes('v2/long/255'), '2.0', true).should.eql({ $: 255, $class: 'long' }); hessian.encode(java.long(-2048), '2.0').should.eql(utils.bytes('v2/long/-2048')); hessian.decode(utils.bytes('v2/long/-2048'), '2.0').should.equal(-2048); + hessian.decode(utils.bytes('v2/long/-2048'), '2.0', true).should.eql({ $: -2048, $class: 'long' }); hessian.encode(java.long(2047), '2.0').should.eql(utils.bytes('v2/long/2047')); hessian.decode(utils.bytes('v2/long/2047'), '2.0').should.equal(2047); + hessian.decode(utils.bytes('v2/long/2047'), '2.0', true).should.eql({ $: 2047, $class: 'long' }); hessian.encode(java.long(262143), '2.0').should.eql(utils.bytes('v2/long/262143')); hessian.decode(utils.bytes('v2/long/262143'), '2.0').should.equal(262143); + hessian.decode(utils.bytes('v2/long/262143'), '2.0', true).should.eql({ $: 262143, $class: 'long' }); hessian.encode(java.long(-262144), '2.0').should.eql(utils.bytes('v2/long/-262144')); hessian.decode(utils.bytes('v2/long/-262144'), '2.0').should.equal(-262144); + hessian.decode(utils.bytes('v2/long/-262144'), '2.0', true).should.eql({ $: -262144, $class: 'long' }); hessian.encode(java.long(2048), '2.0').should.eql(utils.bytes('v2/long/2048')); hessian.decode(utils.bytes('v2/long/2048'), '2.0').should.equal(2048); + hessian.decode(utils.bytes('v2/long/2048'), '2.0', true).should.eql({ $: 2048, $class: 'long' }); hessian.encode(java.long(-2049), '2.0').should.eql(utils.bytes('v2/long/-2049')); hessian.decode(utils.bytes('v2/long/-2049'), '2.0').should.equal(-2049); + hessian.decode(utils.bytes('v2/long/-2049'), '2.0', true).should.eql({ $: -2049, $class: 'long' }); hessian.encode(java.long(-2147483648), '2.0').should.eql(utils.bytes('v2/long/-2147483648')); hessian.decode(utils.bytes('v2/long/-2147483648'), '2.0').should.equal(-2147483648); + hessian.decode(utils.bytes('v2/long/-2147483648'), '2.0', true).should.eql({ $: -2147483648, $class: 'long' }); hessian.encode(java.long(-2147483647), '2.0').should.eql(utils.bytes('v2/long/-2147483647')); hessian.decode(utils.bytes('v2/long/-2147483647'), '2.0').should.equal(-2147483647); + hessian.decode(utils.bytes('v2/long/-2147483647'), '2.0', true).should.eql({ $: -2147483647, $class: 'long' }); hessian.encode(java.long(2147483647), '2.0').should.eql(utils.bytes('v2/long/2147483647')); hessian.decode(utils.bytes('v2/long/2147483647'), '2.0').should.equal(2147483647); + hessian.decode(utils.bytes('v2/long/2147483647'), '2.0', true).should.eql({ $: 2147483647, $class: 'long' }); hessian.encode(java.long(2147483646), '2.0').should.eql(utils.bytes('v2/long/2147483646')); hessian.decode(utils.bytes('v2/long/2147483646'), '2.0').should.equal(2147483646); + hessian.decode(utils.bytes('v2/long/2147483646'), '2.0', true).should.eql({ $: 2147483646, $class: 'long' }); hessian.encode(java.long(2147483648), '2.0').should.eql(utils.bytes('v2/long/2147483648')); hessian.decode(utils.bytes('v2/long/2147483648'), '2.0').should.equal(2147483648); + hessian.decode(utils.bytes('v2/long/2147483648'), '2.0', true).should.eql({ $: 2147483648, $class: 'long' }); }); it('should read 1.0 bin as well', function () { diff --git a/test/object.test.js b/test/object.test.js index ef8dd55..97b4bc0 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -813,7 +813,10 @@ describe('object.test.js', function () { a0.should.eql({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { - value: 0 + value: { + $class: 'long', + $: 0 + } } }); hessian.encode({ @@ -833,7 +836,10 @@ describe('object.test.js', function () { a1.should.eql({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { - value: 1 + value: { + $class: 'long', + $: 1 + } } }); hessian.encode({ From c403d276b4f7e2ef2734ad3f5da2131b7510ae9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=8C=96?= Date: Wed, 21 Oct 2015 01:12:19 +0800 Subject: [PATCH 2/5] decode int with type --- lib/v2/decoder.js | 26 ++++++++++----------- test/list.test.js | 20 +++++++++++++++-- test/object.test.js | 55 ++++++++++++++++++++++++++++++++++++++------- 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index a72e3e9..64527d7 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -93,31 +93,31 @@ utils.addByteCodes(BYTE_CODES, [ * @return {Number} * @api public */ -proto.readInt = function () { +proto.readInt = function (withType) { var code = this.byteBuffer.get(); + var val; // Compact int if (code >= 0x80 && code <= 0xbf) { // Integers between -16 and 47 can be encoded by a single octet in the range x80 to xbf. // value = code - 0x90 - return code - 0x90; - } - if (code >= 0xc0 && code <= 0xcf) { + val = code - 0x90; + } else if (code >= 0xc0 && code <= 0xcf) { // Integers between -2048 and 2047 can be encoded in two octets with the leading byte in the range xc0 to xcf. // value = ((code - 0xc8) << 8) + b0; - return ((code - 0xc8) << 8) + this.byteBuffer.get(); - } - if (code >= 0xd0 && code <= 0xd7) { + val = ((code - 0xc8) << 8) + this.byteBuffer.get(); + } else if (code >= 0xd0 && code <= 0xd7) { // Integers between -262144 and 262143 can be encoded in three bytes with the leading byte in the range xd0 to xd7. // value = ((code - 0xd4) << 16) + (b1 << 8) + b0; var b1 = this.byteBuffer.get(); var b0 = this.byteBuffer.get(); - return ((code - 0xd4) << 16) + (b1 << 8) + b0; - } - if (code === 0x49) { - return this.byteBuffer.getInt(); + val = ((code - 0xd4) << 16) + (b1 << 8) + b0; + } else if (code === 0x49) { + val = this.byteBuffer.getInt(); + } else { + this.throwError('readInt', code); } - - this.throwError('readInt', code); + + return this.handleType('int', val, withType); }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/list.test.js b/test/list.test.js index e05d7fd..16e1738 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -172,7 +172,7 @@ describe('list.test.js', function () { hessian.encode([], '2.0').should.eql(utils.bytes('v2/list/untyped_[]')); hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0').should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([1, 2, 'foo']); + hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([{$: 1, $class: 'int'}, {$: 2, $class: 'int'}, 'foo']); hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0').should.eql(['1', '2', '3', '4', '5', '6', '7', '8']); hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0', true).should.eql(['1', '2', '3', '4', '5', '6', '7', '8']); hessian.decode(utils.bytes('v2/list/untyped_[]'), '2.0').should.eql([]); @@ -238,7 +238,23 @@ describe('list.test.js', function () { hessian.decode(utils.bytes('v2/list/[int'), '2.0').should.eql([1, 2, 3]); // encode again should use type cache - hessian.decode(utils.bytes('v2/list/[int'), '2.0', true).should.eql(list); + hessian.decode(utils.bytes('v2/list/[int'), '2.0', true).should.eql({ + $class: '[int', + $: [ + { + $class: 'int', + $: 1 + }, + { + $class: 'int', + $: 2 + }, + { + $class: 'int', + $: 3 + } + ] + }); var strs = { $class: '[string', diff --git a/test/object.test.js b/test/object.test.js index 97b4bc0..2a4a34d 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -402,7 +402,10 @@ describe('object.test.js', function () { ctx: { $class: 'hessian.ConnectionRequest$RequestContext', $: { - id: 101, + id: { + $class: 'int', + $: 101 + }, // 'this$0': null } } @@ -551,7 +554,16 @@ describe('object.test.js', function () { var buf = hessian.encode(obj, '2.0'); buf[0].should.equal(0x43); hessian.decode(buf, '2.0').should.eql(obj.$); - hessian.decode(buf, '2.0', true).should.eql(obj); + hessian.decode(buf, '2.0', true).should.eql({ + $class: 'hessian.test.demo.Car', + $: { + a: { + $class: 'int', + $: 1, + }, + b: 'map', + }, + }); }); it('should read one car list', function () { @@ -574,7 +586,11 @@ describe('object.test.js', function () { b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } + mileage: { + $class: 'int', + $: 65536, + }, + } } ]); @@ -607,7 +623,11 @@ describe('object.test.js', function () { b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } + mileage: { + $class: 'int', + $: 65536, + }, + } }, { $class: 'hessian.demo.Car', @@ -617,7 +637,11 @@ describe('object.test.js', function () { b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 } + mileage: { + $class: 'int', + $: 65536, + }, + } } ]); @@ -661,7 +685,12 @@ describe('object.test.js', function () { b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } }, + mileage: { + $class: 'int', + $: 65536, + }, + } + }, { '$class': 'hessian.demo.Car', '$': { a: 'a', @@ -669,7 +698,12 @@ describe('object.test.js', function () { b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 } }, + mileage: { + $class: 'int', + $: 65536, + }, + } + }, { '$class': 'hessian.demo.Car', '$': { a: 'a', @@ -677,7 +711,12 @@ describe('object.test.js', function () { b: 'b', model: 'model 3', color: 'aquamarine', - mileage: 65536 } } + mileage: { + $class: 'int', + $: 65536, + }, + } + } ]); hessian.encode(cars, '2.0').should.eql(utils.bytes('v2/map/car_list')); From c64e8432dfe9ece5bf30066a000df7fe26fdd1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=8C=96?= Date: Wed, 21 Oct 2015 11:00:11 +0800 Subject: [PATCH 3/5] decode double with type --- lib/v2/decoder.js | 38 ++++++++++++++++++-------------------- test/double.test.js | 10 ++++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index 64527d7..91188f0 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -116,7 +116,7 @@ proto.readInt = function (withType) { } else { this.throwError('readInt', code); } - + return this.handleType('int', val, withType); }; @@ -246,29 +246,27 @@ utils.addByteCodes(BYTE_CODES, [ * @return {Number} * @api public */ -proto.readDouble = function () { +proto.readDouble = function (withType) { var code = this.byteBuffer.get(); + var val; if (code === 0x44) { - return this.byteBuffer.getDouble(); + val = this.byteBuffer.getDouble(); + } else if (code === 0x5b) { + // Compact double + val = 0.0; + } else if (code === 0x5c) { + val = 1.0; + } else if (code === 0x5d) { + val = this.byteBuffer.getInt8(); + } else if (code === 0x5e) { + val = this.byteBuffer.getInt16(); + } else if (code === 0x5f) { + val = this.byteBuffer.getInt32() * 0.001; + } else { + this.throwError('readDouble', code); } - // Compact double - if (code === 0x5b) { - return 0.0; - } - if (code === 0x5c) { - return 1.0; - } - if (code === 0x5d) { - return this.byteBuffer.getInt8(); - } - if (code === 0x5e) { - return this.byteBuffer.getInt16(); - } - if (code === 0x5f) { - return this.byteBuffer.getInt32() * 0.001; - } - this.throwError('readDouble', code); + return this.handleType('double', val, withType); }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/double.test.js b/test/double.test.js index 42f6004..a79d977 100644 --- a/test/double.test.js +++ b/test/double.test.js @@ -105,7 +105,9 @@ describe('double.test.js', function () { describe('v2.0', function () { it('should read 0.0 and 1.0', function () { hessian.decode(new Buffer([0x5b]), '2.0').should.equal(0.0); + hessian.decode(new Buffer([0x5b]), '2.0', true).should.eql({$class: 'double', $: 0.0}); hessian.decode(new Buffer([0x5c]), '2.0').should.equal(1.0); + hessian.decode(new Buffer([0x5c]), '2.0', true).should.eql({$class: 'double', $: 1.0}); }); it('should read 8 bits double', function () { @@ -113,6 +115,7 @@ describe('double.test.js', function () { hessian.decode(new Buffer([0x5d, 0x01]), '2.0').should.equal(1.0); hessian.decode(new Buffer([0x5d, 0x80]), '2.0').should.equal(-128.0); hessian.decode(new Buffer([0x5d, 0x7f]), '2.0').should.equal(127.0); + hessian.decode(new Buffer([0x5d, 0x7f]), '2.0', true).should.eql({$class: 'double', $: 127.0}); }); it('should read 16 bits double', function () { @@ -121,16 +124,20 @@ describe('double.test.js', function () { hessian.decode(new Buffer([0x5e, 0x00, 0x80]), '2.0').should.equal(128.0); hessian.decode(new Buffer([0x5e, 0x00, 0x7f]), '2.0').should.equal(127.0); hessian.decode(new Buffer([0x5e, 0x80, 0x00]), '2.0').should.equal(-32768.0); + hessian.decode(new Buffer([0x5e, 0x80, 0x00]), '2.0', true).should.eql({$class: 'double', $: -32768.0}); hessian.decode(new Buffer([0x5e, 0x7f, 0xff]), '2.0').should.equal(32767.0); }); it('should read 32 bits float double', function () { hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0.0); + hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x00, 0x00]), '2.0', true).should.eql({$class: 'double', $: 0.0}); hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x2f, 0xda]), '2.0').should.equal(12.25); + hessian.decode(new Buffer([0x5f, 0x00, 0x00, 0x2f, 0xda]), '2.0', true).should.eql({$class: 'double', $: 12.25}); }); it('should read normal double', function () { hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0').should.equal(10.0); + hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0', true).should.eql({$class: 'double', $: 10.0}); }); it('should write 0.0 and 1.0', function () { @@ -183,6 +190,7 @@ describe('double.test.js', function () { hessian.decode(utils.bytes('v2/double/10.1'), '2.0').should.equal(10.1); hessian.decode(utils.bytes('v2/double/-128'), '2.0').should.equal(-128); hessian.decode(utils.bytes('v2/double/-127.9999'), '2.0').should.equal(-127.9999); + hessian.decode(utils.bytes('v2/double/-127.9999'), '2.0', true).should.eql({$class: 'double', $: -127.9999}); hessian.decode(utils.bytes('v2/double/127'), '2.0').should.equal(127); hessian.decode(utils.bytes('v2/double/126.9989'), '2.0').should.equal(126.9989); hessian.decode(utils.bytes('v2/double/-32768'), '2.0').should.equal(-32768); @@ -203,6 +211,7 @@ describe('double.test.js', function () { hessian.decode(utils.bytes('v2/double/2147483647'), '2.0').should.equal(2147483647); hessian.decode(utils.bytes('v2/double/2147483646'), '2.0').should.equal(2147483646); hessian.decode(utils.bytes('v2/double/2147483646.456'), '2.0').should.equal(2147483646.456); + hessian.decode(utils.bytes('v2/double/2147483646.456'), '2.0', true).should.eql({$class: 'double', $: 2147483646.456}); }); it('should read java hessian 1.0 bin format', function () { @@ -229,6 +238,7 @@ describe('double.test.js', function () { hessian.decode(utils.bytes('v1/double/2147483647'), '2.0').should.equal(2147483647); hessian.decode(utils.bytes('v1/double/2147483646'), '2.0').should.equal(2147483646); hessian.decode(utils.bytes('v1/double/2147483646.456'), '2.0').should.equal(2147483646.456); + hessian.decode(utils.bytes('v1/double/2147483646.456'), '2.0', true).should.eql({$class: 'double', $: 2147483646.456}); }); }); }); From f057d906dd493024f133a63a6508dec72f06614c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=8C=96?= Date: Wed, 21 Oct 2015 11:15:48 +0800 Subject: [PATCH 4/5] decode date with type --- lib/v2/decoder.js | 14 ++++++++------ test/date.test.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index 91188f0..c106cbf 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -292,16 +292,18 @@ utils.addByteCodes(BYTE_CODES, [ * @return {Date} * @api public */ -proto.readDate = function () { +proto.readDate = function (withType) { var code = this.byteBuffer.get(); + var val; if (code === 0x4a) { - return new Date(utils.handleLong(this.byteBuffer.getLong())); - } - if (code === 0x4b) { - return new Date(this.byteBuffer.getUInt32() * 60000); + val = new Date(utils.handleLong(this.byteBuffer.getLong())); + } else if (code === 0x4b) { + val = new Date(this.byteBuffer.getUInt32() * 60000); + } else { + this.throwError('readDate', code); } - this.throwError('readDate', code); + return this.handleType('date', val, withType); }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/date.test.js b/test/date.test.js index 5e49365..c3fafb8 100644 --- a/test/date.test.js +++ b/test/date.test.js @@ -28,14 +28,20 @@ describe('date.test.js', function () { d.getTime().should.equal(894621091000); d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT'); d.toISOString().should.equal('1998-05-08T09:51:31.000Z'); + hessian.decode(dateBuffer, true).should.eql({ + $class: 'java.util.Date', + $: new Date(894621091000) + }); }); it('should write date 2:51:31 May 8, 1998', function () { hessian.encode(new Date(894621091000)).should.eql(dateBuffer); + hessian.encode({$class: 'java.util.Date', $:new Date(894621091000)}).should.eql(dateBuffer); }); it('should write date 0 and read', function () { hessian.encode(new Date(0)).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0])); + hessian.encode({$class: 'java.util.Date', $: new Date(0)}).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0])); }); it('should read date 09:51:31 May 8, 1998 UTC', function () { @@ -45,6 +51,8 @@ describe('date.test.js', function () { d.getTime().should.equal(894621091000); d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT'); d.toISOString().should.equal('1998-05-08T09:51:31.000Z'); + + hessian.decode(utils.bytes('v1/date/894621091000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)}); }); it('should read date 09:51:00 May 8, 1998 UTC', function () { @@ -54,13 +62,17 @@ describe('date.test.js', function () { d.getTime().should.equal(894621060000); d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT'); d.toISOString().should.equal('1998-05-08T09:51:00.000Z'); + + hessian.decode(utils.bytes('v1/date/894621060000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)}); }); it('should write date', function () { var now = new Date(1398280514000); hessian.encode(now, '1.0').should.eql(utils.bytes('v1/date/now')); + hessian.encode({$class: 'java.util.Date', $: now}, '1.0').should.eql(utils.bytes('v1/date/now')); // read it hessian.decode(utils.bytes('v1/date/now'), '1.0').should.eql(now); + hessian.decode(utils.bytes('v1/date/now'), '1.0', true).should.eql({$class: 'java.util.Date', $: now}); }); describe('hessian 2.0', function () { @@ -71,6 +83,8 @@ describe('date.test.js', function () { d.getTime().should.equal(894621091000); d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT'); d.toISOString().should.equal('1998-05-08T09:51:31.000Z'); + + hessian.decode(utils.bytes('v2/date/894621091000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)}); }); it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function () { @@ -80,13 +94,17 @@ describe('date.test.js', function () { d.getTime().should.equal(894621060000); d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT'); d.toISOString().should.equal('1998-05-08T09:51:00.000Z'); + + hessian.decode(utils.bytes('v2/date/894621060000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)}); }); it('should write and read date', function () { var now = new Date(1398280514000); hessian.encode(now, '2.0').should.eql(utils.bytes('v2/date/now')); + hessian.encode({$class: 'java.util.Date', $: now}, '2.0').should.eql(utils.bytes('v2/date/now')); // read it hessian.decode(utils.bytes('v2/date/now'), '2.0').should.eql(now); + hessian.decode(utils.bytes('v2/date/now'), '2.0', true).should.eql({$class: 'java.util.Date', $: now}); }); // it('should read 1.0 format', function () { From 8215621520859125cc0499acd6b4c563afe228c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=8C=96?= Date: Wed, 21 Oct 2015 12:17:48 +0800 Subject: [PATCH 5/5] decode string with type --- lib/v2/decoder.js | 5 +-- test/list.test.js | 43 +++++++++++++++++++---- test/object.test.js | 84 ++++++++++++++++++++++++++------------------- test/string.test.js | 72 +++++++++++++++++++++++++++++++++++++- 4 files changed, 159 insertions(+), 45 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index c106cbf..ee4c111 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -406,7 +406,7 @@ utils.addByteCodes(BYTE_CODES, [ * @return {String} * @api public */ -proto.readString = function () { +proto.readString = function (withType) { var str = ''; var code = this.byteBuffer.get(); debug('readString() code: %s', code); @@ -456,7 +456,8 @@ proto.readString = function () { this.throwError('readString', code); break; } - return str; + + return this.handleType('string', str, withType); }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/list.test.js b/test/list.test.js index 16e1738..a9ec31d 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -172,11 +172,23 @@ describe('list.test.js', function () { hessian.encode([], '2.0').should.eql(utils.bytes('v2/list/untyped_[]')); hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0').should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([{$: 1, $class: 'int'}, {$: 2, $class: 'int'}, 'foo']); + hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([{$: 1, $class: 'int'}, {$: 2, $class: 'int'}, {$class: 'java.lang.String', $: 'foo'}]); hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0').should.eql(['1', '2', '3', '4', '5', '6', '7', '8']); - hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0', true).should.eql(['1', '2', '3', '4', '5', '6', '7', '8']); + hessian.decode(utils.bytes('v2/list/untyped_list_8'), '2.0', true).should.eql([ + {$class: 'java.lang.String', $: '1'}, + {$class: 'java.lang.String', $: '2'}, + {$class: 'java.lang.String', $: '3'}, + {$class: 'java.lang.String', $: '4'}, + {$class: 'java.lang.String', $: '5'}, + {$class: 'java.lang.String', $: '6'}, + {$class: 'java.lang.String', $: '7'}, + {$class: 'java.lang.String', $: '8'}, + ]); hessian.decode(utils.bytes('v2/list/untyped_[]'), '2.0').should.eql([]); - hessian.decode(utils.bytes('v2/list/untyped_[foo,bar]'), '2.0', true).should.eql(['foo', 'bar']); + hessian.decode(utils.bytes('v2/list/untyped_[foo,bar]'), '2.0', true).should.eql([ + {$class: 'java.lang.String', $: 'foo'}, + {$class: 'java.lang.String', $: 'bar'}, + ]); // java.util.ArrayList as simple hessian.encode({ @@ -218,12 +230,24 @@ describe('list.test.js', function () { hessian.decode(utils.bytes('v2/list/typed_list'), '2.0', true) .should.eql({ '$class': 'hessian.demo.SomeArrayList', - '$': [ 'ok', 'some list' ] + '$': [ + {$class: 'java.lang.String', $: 'ok'}, + {$class: 'java.lang.String', $: 'some list'} + ] }); hessian.decode(utils.bytes('v2/list/typed_list_8'), '2.0', true) .should.eql({ '$class': 'hessian.demo.SomeArrayList', - '$': ['1', '2', '3', '4', '5', '6', '7', '8'] + '$': [ + {$class: 'java.lang.String', $: '1'}, + {$class: 'java.lang.String', $: '2'}, + {$class: 'java.lang.String', $: '3'}, + {$class: 'java.lang.String', $: '4'}, + {$class: 'java.lang.String', $: '5'}, + {$class: 'java.lang.String', $: '6'}, + {$class: 'java.lang.String', $: '7'}, + {$class: 'java.lang.String', $: '8'}, + ] }); hessian.decode(utils.bytes('v2/list/typed_list_8'), '2.0') @@ -261,7 +285,14 @@ describe('list.test.js', function () { $: ['1', '@', '3'] }; hessian.encode(strs, '2.0').should.eql(utils.bytes('v2/list/[string')); - hessian.decode(utils.bytes('v2/list/[string'), '2.0', true).should.eql(strs); + hessian.decode(utils.bytes('v2/list/[string'), '2.0', true).should.eql({ + $class: '[string', + $: [ + {$class: 'java.lang.String', $: '1'}, + {$class: 'java.lang.String', $: '@'}, + {$class: 'java.lang.String', $: '3'}, + ] + }); }); // it('should read hessian 1.0 untyped list', function () { diff --git a/test/object.test.js b/test/object.test.js index 2a4a34d..b072b08 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -516,7 +516,10 @@ describe('object.test.js', function () { hessian.decode(utils.bytes('v2/enum/green'), '2.0', true).should.eql({ $class: 'hessian.Main$Color', $: { - name: 'GREEN' + name: { + $class: 'java.lang.String', + $: 'GREEN', + }, } }); @@ -531,7 +534,10 @@ describe('object.test.js', function () { hessian.decode(utils.bytes('v2/enum/red'), '2.0', true).should.eql({ $class: 'hessian.Main$Color', $: { - name: 'RED' + name: { + $class: 'java.lang.String', + $: 'RED', + }, } }); @@ -540,9 +546,9 @@ describe('object.test.js', function () { ); hessian.decode(utils.bytes('v2/enum/lists'), '2.0', true).should.eql([ - { '$class': 'hessian.Main$Color', '$': { name: 'BLUE' } }, - { '$class': 'hessian.Main$Color', '$': { name: 'RED' } }, - { '$class': 'hessian.Main$Color', '$': { name: 'GREEN' } } + { '$class': 'hessian.Main$Color', '$': {name: {$class: 'java.lang.String', $: 'BLUE'}} }, + { '$class': 'hessian.Main$Color', '$': {name: {$class: 'java.lang.String', $: 'RED'}} }, + { '$class': 'hessian.Main$Color', '$': {name: {$class: 'java.lang.String', $: 'GREEN'}} } ]); }); @@ -561,7 +567,10 @@ describe('object.test.js', function () { $class: 'int', $: 1, }, - b: 'map', + b: { + $class: 'java.lang.String', + $: 'map', + } }, }); }); @@ -581,11 +590,11 @@ describe('object.test.js', function () { { $class: 'hessian.demo.Car', $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', + a: {$class: 'java.lang.String', $: 'a'}, + c: {$class: 'java.lang.String', $: 'c'}, + b: {$class: 'java.lang.String', $: 'b'}, + model: {$class: 'java.lang.String', $: 'model 1'}, + color: {$class: 'java.lang.String', $: 'aquamarine'}, mileage: { $class: 'int', $: 65536, @@ -618,11 +627,11 @@ describe('object.test.js', function () { { $class: 'hessian.demo.Car', $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', + a: {$class: 'java.lang.String', $: 'a'}, + c: {$class: 'java.lang.String', $: 'c'}, + b: {$class: 'java.lang.String', $: 'b'}, + model: {$class: 'java.lang.String', $: 'model 1'}, + color: {$class: 'java.lang.String', $: 'aquamarine'}, mileage: { $class: 'int', $: 65536, @@ -632,11 +641,11 @@ describe('object.test.js', function () { { $class: 'hessian.demo.Car', $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', + a: {$class: 'java.lang.String', $: 'a'}, + c: {$class: 'java.lang.String', $: 'c'}, + b: {$class: 'java.lang.String', $: 'b'}, + model: {$class: 'java.lang.String', $: 'model 2'}, + color: {$class: 'java.lang.String', $: 'aquamarine'}, mileage: { $class: 'int', $: 65536, @@ -680,11 +689,12 @@ describe('object.test.js', function () { cars.should.eql([ { '$class': 'hessian.demo.Car', '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', + { + a: {$class: 'java.lang.String', $: 'a'}, + c: {$class: 'java.lang.String', $: 'c'}, + b: {$class: 'java.lang.String', $: 'b'}, + model: {$class: 'java.lang.String', $: 'model 1'}, + color: {$class: 'java.lang.String', $: 'aquamarine'}, mileage: { $class: 'int', $: 65536, @@ -693,11 +703,12 @@ describe('object.test.js', function () { }, { '$class': 'hessian.demo.Car', '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', + { + a: {$class: 'java.lang.String', $: 'a'}, + c: {$class: 'java.lang.String', $: 'c'}, + b: {$class: 'java.lang.String', $: 'b'}, + model: {$class: 'java.lang.String', $: 'model 2'}, + color: {$class: 'java.lang.String', $: 'aquamarine'}, mileage: { $class: 'int', $: 65536, @@ -706,11 +717,12 @@ describe('object.test.js', function () { }, { '$class': 'hessian.demo.Car', '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 3', - color: 'aquamarine', + { + a: {$class: 'java.lang.String', $: 'a'}, + c: {$class: 'java.lang.String', $: 'c'}, + b: {$class: 'java.lang.String', $: 'b'}, + model: {$class: 'java.lang.String', $: 'model 3'}, + color: {$class: 'java.lang.String', $: 'aquamarine'}, mileage: { $class: 'int', $: 65536, diff --git a/test/string.test.js b/test/string.test.js index 0cc990d..832bf37 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -24,6 +24,7 @@ describe('string.test.js', function () { it('should read string', function () { hessian.decode(helloBuffer).should.equal('hello'); + hessian.decode(helloBuffer, true).should.eql({$class: 'java.lang.String', $: 'hello'}); hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), new Buffer('hello, '), new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), new Buffer('world')])).should.equal('hello, world'); @@ -34,11 +35,18 @@ describe('string.test.js', function () { s.should.be.a.Buffer; s.should.length(8); s.should.eql(helloBuffer); + + var s2 = hessian.encode({$class: 'java.lang.String', $: 'hello'}); + s2.should.be.a.Buffer; + s2.should.length(8); + s2.should.eql(helloBuffer); }); it('should read write empty string', function () { hessian.decode(new Buffer(['S'.charCodeAt(0), 0, 0])).should.equal(''); + hessian.decode(new Buffer(['S'.charCodeAt(0), 0, 0]), true).should.eql({$class: 'java.lang.String', $: ''}); hessian.encode('').should.eql(new Buffer(['S'.charCodeAt(0), 0, 0])); + hessian.encode({$class: 'java.lang.String', $: ''}).should.eql(new Buffer(['S'.charCodeAt(0), 0, 0])); }); it('should read and write utf8 string as java', function () { @@ -47,93 +55,125 @@ describe('string.test.js', function () { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_32767')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_32767')); hessian.decode(utils.bytes('v1/string/utf8_32767')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_32767'), true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 32768; i++) { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_32768')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_32768')); hessian.decode(utils.bytes('v1/string/utf8_32768')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_32768'), true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 32769; i++) { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_32769')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_32769')); hessian.decode(utils.bytes('v1/string/utf8_32769')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_32769'), true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 65534; i++) { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65534')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_65534')); hessian.decode(utils.bytes('v1/string/utf8_65534')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_65534'), true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 65535; i++) { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65535')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_65535')); hessian.decode(utils.bytes('v1/string/utf8_65535')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_65535'), true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 65536; i++) { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65536')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_65536')); hessian.decode(utils.bytes('v1/string/utf8_65536')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_65536'), true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 65537; i++) { str += '锋'; } hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65537')); + hessian.encode({$class: 'java.lang.String', $: str}, '1.0').should.eql(utils.bytes('v1/string/utf8_65537')); hessian.decode(utils.bytes('v1/string/utf8_65537')).should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_65537'), true).should.eql({$class: 'java.lang.String', $: str}); }); it('should write string same as java write', function () { hessian.encode('', '1.0').should.eql(utils.bytes('v1/string/empty')); hessian.encode('foo').should.eql(utils.bytes('v1/string/foo')); hessian.encode('中文 Chinese', '1.0').should.eql(utils.bytes('v1/string/chinese')); + hessian.encode({$class: 'java.lang.String', $: '中文 Chinese'}, '1.0').should.eql(utils.bytes('v1/string/chinese')); + var text4k = utils.string('4k'); hessian.encode(text4k, '1.0').should.eql(utils.bytes('v1/string/text4k')); + hessian.encode({$class: 'java.lang.String', $: text4k}, '1.0').should.eql(utils.bytes('v1/string/text4k')); hessian.decode(utils.bytes('v1/string/text4k')).should.equal(text4k); + hessian.decode(utils.bytes('v1/string/text4k'), true).should.eql({$class: 'java.lang.String', $: text4k}); var largeBuf = new Buffer(32767); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_32767')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_32767')); hessian.decode(utils.bytes('v1/string/large_string_32767')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_32767'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(32768); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_32768')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_32768')); hessian.decode(utils.bytes('v1/string/large_string_32768')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_32768'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(32769); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_32769')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_32769')); hessian.decode(utils.bytes('v1/string/large_string_32769')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_32769'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(65534); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65534')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_65534')); hessian.decode(utils.bytes('v1/string/large_string_65534')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_65534'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(65535); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65535')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_65535')); hessian.decode(utils.bytes('v1/string/large_string_65535')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_65535'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(65536); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65536')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_65536')); hessian.decode(utils.bytes('v1/string/large_string_65536')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_65536'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(65537); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65537')); + hessian.encode({$class: 'java.lang.String', $: largeBuf.toString()}, '1.0').should.eql(utils.bytes('v1/string/large_string_65537')); hessian.decode(utils.bytes('v1/string/large_string_65537')).should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v1/string/large_string_65537'), true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); }); @@ -156,39 +196,59 @@ describe('string.test.js', function () { describe('v2.0', function () { it('should read short strings', function () { hessian.decode(new Buffer([0x00]), '2.0').should.equal(''); - hessian.decode(new Buffer([0x00]), '2.0', true).should.equal(''); + hessian.decode(new Buffer([0x00]), '2.0', true).should.eql({$class: 'java.lang.String', $: ''}); hessian.decode(Buffer.concat([new Buffer([0x05]), new Buffer('hello')]), '2.0').should.equal('hello'); hessian.decode(new Buffer([0x01, 0xc3, 0x83]), '2.0').should.equal('\u00c3'); hessian.decode(Buffer.concat([new Buffer([0x09]), new Buffer('hello, 中文')]), '2.0').should.equal('hello, 中文'); + hessian.decode(Buffer.concat([new Buffer([0x09]), + new Buffer('hello, 中文')]), '2.0', true).should.eql({$class: 'java.lang.String', $: 'hello, 中文'}); }); it('should read "hello" in long form', function () { hessian.decode(Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), new Buffer('hello')]), '2.0').should.equal('hello'); + hessian.decode(Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), + new Buffer('hello')]), '2.0', true).should.eql({$class: 'java.lang.String', $: 'hello'}); }); it('should read split into two chunks: s and short strings', function () { hessian.decode(Buffer.concat([new Buffer([0x52, 0x00, 0x07]), new Buffer('hello, '), new Buffer([0x05]), new Buffer('world')]), '2.0') .should.equal('hello, world'); + hessian.decode(Buffer.concat([new Buffer([0x52, 0x00, 0x07]), + new Buffer('hello, '), new Buffer([0x05]), new Buffer('world')]), '2.0', true) + .should.eql({$class: 'java.lang.String', $: 'hello, world'}); }); it('should write short strings', function () { hessian.encode('', '2.0').should.eql(new Buffer([0x00])); + hessian.encode({$class: 'java.lang.String', $: ''}, '2.0').should.eql(new Buffer([0x00])); hessian.encode('foo', '2.0').should.eql( Buffer.concat([ new Buffer([0x03]), new Buffer('foo') ]) ); + hessian.encode({$class: 'java.lang.String', $: 'foo'}, '2.0').should.eql( + Buffer.concat([ + new Buffer([0x03]), + new Buffer('foo') + ]) + ); hessian.encode('0123456789012345678901234567890', '2.0').should.eql( Buffer.concat([ new Buffer([0x1f]), new Buffer('0123456789012345678901234567890') ]) ); + hessian.encode({$class: 'java.lang.String', $: '0123456789012345678901234567890'}, '2.0').should.eql( + Buffer.concat([ + new Buffer([0x1f]), + new Buffer('0123456789012345678901234567890') + ]) + ); // var len32Buf = new Buffer(2); // len32Buf.writeInt16BE(32, 0); @@ -199,6 +259,13 @@ describe('string.test.js', function () { new Buffer('01234567890123456789012345678901') ]) ); + hessian.encode({$class: 'java.lang.String', $: '01234567890123456789012345678901'}, '2.0').should.eql( + Buffer.concat([ + new Buffer([0x30, 0x20]), + // len32Buf, + new Buffer('01234567890123456789012345678901') + ]) + ); var largeBuf = new Buffer(65535); largeBuf.fill(0x41); @@ -214,6 +281,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v2/string/foo'), '2.0').should.equal('foo'); hessian.decode(utils.bytes('v2/string/chinese'), '2.0').should.equal('中文 Chinese'); hessian.decode(utils.bytes('v2/string/text4k'), '2.0').should.equal(utils.string('4k')); + hessian.decode(utils.bytes('v2/string/text4k'), '2.0', true).should.eql({$class: 'java.lang.String', $: utils.string('4k')}); var largeBuf = new Buffer(32767); largeBuf.fill(0x41); @@ -238,6 +306,7 @@ describe('string.test.js', function () { var largeBuf = new Buffer(65536); largeBuf.fill(0x41); hessian.decode(utils.bytes('v2/string/large_string_65536'), '2.0').should.equal(largeBuf.toString()); + hessian.decode(utils.bytes('v2/string/large_string_65536'), '2.0', true).should.eql({$class: 'java.lang.String', $: largeBuf.toString()}); var largeBuf = new Buffer(65537); largeBuf.fill(0x41); @@ -299,6 +368,7 @@ describe('string.test.js', function () { hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_32767')); hessian.decode(utils.bytes('v2/string/utf8_32767'), '2.0').should.equal(str); hessian.decode(utils.bytes('v1/string/utf8_32767'), '2.0').should.equal(str); + hessian.decode(utils.bytes('v1/string/utf8_32767', true), '2.0', true).should.eql({$class: 'java.lang.String', $: str}); var str = ''; for (var i = 0; i < 32768; i++) {