Skip to content

Commit ae6e58b

Browse files
author
宗羽
committed
feat(generic) support generic map
- support encode es6 Map to java generic map issue #44
1 parent 53cbc75 commit ae6e58b

5 files changed

Lines changed: 68 additions & 12 deletions

File tree

lib/v1/encoder.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var utils = require('../utils');
1616
var javaObject = require('../object');
1717
var is = require('is-type-of');
1818

19+
var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';
20+
1921
function Encoder(options) {
2022
options = options || {};
2123
//array of buffer
@@ -263,12 +265,19 @@ proto._writeHashMap = function (obj) {
263265
// hashmap's type is null
264266
this.writeType('');
265267

266-
// hash map must sort keys
267-
var keys = Object.keys(obj).sort();
268-
for (var i = 0; i < keys.length; i++) {
269-
var k = keys[i];
270-
this.writeString(k);
271-
this.write(obj[k]);
268+
if (SUPPORT_ES6_MAP && obj instanceof Map) {
269+
obj.forEach(function (value, key) {
270+
this.write(key);
271+
this.write(value);
272+
}, this);
273+
} else {
274+
// hash map must sort keys
275+
var keys = Object.keys(obj).sort();
276+
for (var i = 0; i < keys.length; i++) {
277+
var k = keys[i];
278+
this.writeString(k);
279+
this.write(obj[k]);
280+
}
272281
}
273282
this.byteBuffer.put(0x7a);
274283
return this;

lib/v2/encoder.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var util = require('util');
2020
var EncoderV1 = require('../v1/encoder');
2121
var javaObject = require('../object');
2222

23+
var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';
24+
2325
function Encoder(options) {
2426
EncoderV1.call(this, options);
2527

@@ -574,12 +576,19 @@ proto._writeHashMap = function (obj) {
574576

575577
this.byteBuffer.put(0x48); // H
576578

577-
// hash map must sort keys
578-
var keys = Object.keys(obj).sort();
579-
for (var i = 0; i < keys.length; i++) {
580-
var k = keys[i];
581-
this.writeString(k);
582-
this.write(obj[k]);
579+
if (SUPPORT_ES6_MAP && obj instanceof Map) {
580+
obj.forEach(function (value, key) {
581+
this.write(key);
582+
this.write(value);
583+
}, this);
584+
} else {
585+
// hash map must sort keys
586+
var keys = Object.keys(obj).sort();
587+
for (var i = 0; i < keys.length; i++) {
588+
var k = keys[i];
589+
this.writeString(k);
590+
this.write(obj[k]);
591+
}
583592
}
584593
this.byteBuffer.putChar('Z');
585594
return this;

test/fixtures/v1/map/generic.bin

33 Bytes
Binary file not shown.

test/fixtures/v2/map/generic.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
H�{��@=�@�{Z

test/map.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ describe('map.test.js', function () {
161161
});
162162
});
163163

164+
it('should write es6 Map to java.util.Map', function() {
165+
var map = new Map();
166+
map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456);
167+
map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123);
168+
var buf = hessian.encode(map);
169+
buf.should.eql(utils.bytes('v1/map/generic'));
170+
171+
buf = hessian.encode({ '$class': 'java.util.HashMap', '$': map });
172+
buf.should.eql(utils.bytes('v1/map/generic'));
173+
174+
// decode auto transfer key to string
175+
hessian.decode(utils.bytes('v1/map/generic'), '1.0').should.eql({
176+
'123': 123456,
177+
'123456': 123
178+
});
179+
});
180+
164181
describe('v2.0', function () {
165182
// map = new HashMap();
166183
// map.put(new Integer(1), "fee");
@@ -330,4 +347,24 @@ describe('map.test.js', function () {
330347
var rv = hessian.decode(data);
331348
rv.should.eql({null: 'null'});
332349
});
350+
351+
it('should write es6 Map to java.util.Map', function() {
352+
var map = new Map();
353+
map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456);
354+
map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123);
355+
var encoder = new hessian.EncoderV2();
356+
var buf = encoder.write(map).get();
357+
buf.should.eql(utils.bytes('v2/map/generic'));
358+
359+
encoder.reset();
360+
361+
buf = encoder.write({ '$class': 'java.util.HashMap', '$': map }).get();
362+
buf.should.eql(utils.bytes('v2/map/generic'));
363+
364+
// decode auto transfer key to string
365+
hessian.decode(utils.bytes('v2/map/generic'), '2.0').should.eql({
366+
'123': 123456,
367+
'123456': 123
368+
});
369+
});
333370
});

0 commit comments

Comments
 (0)