Skip to content

Commit a46fbc9

Browse files
gxcsoccerfengmk2
authored andcommitted
feat: support java.util.HashMap to es6 Map (#70)
1 parent 2b53d13 commit a46fbc9

25 files changed

Lines changed: 187 additions & 87 deletions

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ language: node_js
33
node_js:
44
- '5'
55
- '4'
6-
- '3'
7-
- '2'
8-
- '1'
96
- '0.12'
10-
- '0.10'
117
script:
128
- "npm run ci"
139
after_script:

lib/object.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**!
1+
/**
22
* hessian.js - lib/object.js
3-
* Copyright(c) 2014
3+
* Copyright(c)
44
* MIT Licensed
55
*
66
* Authors:

lib/utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**!
1+
/**
22
* hessian.js - lib/utils.js
3-
* Copyright(c) 2014
3+
* Copyright(c)
44
* MIT Licensed
55
*
66
* Authors:
@@ -19,6 +19,9 @@ var MIN_SAFE_INT = Long.fromNumber(1 - Math.pow(2, 53));
1919
var MAX_BYTE_TRUNK_SIZE = exports.MAX_BYTE_TRUNK_SIZE = 0x8000;
2020
var MAX_CHAR_TRUNK_SIZE = exports.MAX_CHAR_TRUNK_SIZE = 0x8000;
2121

22+
// Map feature detect
23+
exports.supportES6Map = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';
24+
2225
exports.getSerializer = function (type) {
2326
// get from SERIALIZER_MAP
2427
if (object.SERIALIZER_MAP[type]) {

lib/v1/decoder.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**!
1+
/**
22
* hessian.js - lib/v1/decoder.js
3-
* Copyright(c) 2014
3+
* Copyright(c)
44
* MIT Licensed
55
*
66
* Authors:
@@ -16,6 +16,7 @@ var is = require('is-type-of');
1616
var utils = require('../utils');
1717
var object = require('../object');
1818
var JavaExceptionError = object.JavaExceptionError;
19+
var supportES6Map = require('../utils').supportES6Map;
1920

2021
var BYTE_CODES = {};
2122

@@ -402,12 +403,22 @@ proto.readObject = function (withType) {
402403
$class: type,
403404
$: {}
404405
};
406+
var isMap = (type.indexOf(object.DEFAULT_CLASSNAME.map) === 0 ||
407+
type.indexOf(object.DEFAULT_CLASSNAME.iMap) === 0) && supportES6Map;
408+
409+
if (isMap) {
410+
Object.defineProperty(result.$, '$map', {
411+
value: new Map(),
412+
enumerable: false,
413+
});
414+
}
405415

406416
this._addRef(result);
407417

408418
// get
409419
var label = this.byteBuffer.getChar();
410420
var key;
421+
var t;
411422

412423
while (label !== 'z') {
413424
this.byteBuffer.position(this.byteBuffer.position() - 1);
@@ -416,9 +427,13 @@ proto.readObject = function (withType) {
416427
label = this.byteBuffer.getChar();
417428
// property name will auto transfer to a String type.
418429
debug('read object prop: %j with type: %s', key, withType);
419-
if (!/^this\$\d+$/.test(key)) {
430+
t = typeof key;
431+
if ((t === 'string' || t === 'number') && !/^this\$\d+$/.test(key)) {
420432
result.$[key] = value;
421433
}
434+
if (isMap) {
435+
result.$.$map.set(key, value);
436+
}
422437
}
423438
debug('read object finish');
424439

lib/v1/encoder.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**!
1+
/**
22
* hessian.js - lib/encoder.js
3-
* Copyright(c) 2014
3+
* Copyright(c)
44
* MIT Licensed
55
*
66
* Authors:
@@ -15,8 +15,7 @@ var debug = require('debug')('hessian:v1:encoder');
1515
var utils = require('../utils');
1616
var javaObject = require('../object');
1717
var is = require('is-type-of');
18-
19-
var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';
18+
var supportES6Map = require('../utils').supportES6Map;
2019

2120
function Encoder(options) {
2221
options = options || {};
@@ -265,7 +264,7 @@ proto._writeHashMap = function (obj) {
265264
// hashmap's type is null
266265
this.writeType('');
267266

268-
if (SUPPORT_ES6_MAP && obj instanceof Map) {
267+
if (supportES6Map && obj instanceof Map) {
269268
obj.forEach(function (value, key) {
270269
this.write(key);
271270
this.write(value);

lib/v2/decoder.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/**!
1+
/**
22
* hessian.js - lib/v2/decoder.js
33
*
4-
* Copyright(c) 2014
4+
* Copyright(c)
55
* MIT Licensed
66
*
77
* Authors:
@@ -19,6 +19,7 @@ var debug = require('debug')('hessian:v2:decoder');
1919
var DecoderV1 = require('../v1/decoder');
2020
var utils = require('../utils');
2121
var JavaExceptionError = require('../object').JavaExceptionError;
22+
var supportES6Map = require('../utils').supportES6Map;
2223

2324
var BYTE_CODES = {};
2425

@@ -714,13 +715,30 @@ utils.addByteCodes(BYTE_CODES, [
714715
proto._readMap = function (map, withType) {
715716
var code = this.byteBuffer.get(this.byteBuffer.position());
716717
map = map || {};
718+
719+
if (supportES6Map) {
720+
Object.defineProperty(map, '$map', {
721+
value: new Map(),
722+
enumerable: false,
723+
});
724+
}
725+
717726
var k;
718727
var v;
728+
var t;
719729
// Z(0x5a) list/map terminator
720730
while (code !== 0x5a) {
721731
k = this.read(withType);
722732
v = this.read(withType);
723-
map[k] = v;
733+
t = typeof k;
734+
735+
if (t === 'string' || t === 'number') {
736+
map[k] = v;
737+
}
738+
if (supportES6Map) {
739+
map.$map.set(k, v);
740+
}
741+
724742
code = this.byteBuffer.get(this.byteBuffer.position());
725743
}
726744

lib/v2/encoder.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/**!
1+
/**
22
* hessian.js - lib/v2/encoder.js
33
*
4-
* Copyright(c) 2014
4+
* Copyright(c)
55
* MIT Licensed
66
*
77
* Authors:
@@ -20,8 +20,7 @@ var util = require('util');
2020
var EncoderV1 = require('../v1/encoder');
2121
var javaObject = require('../object');
2222
var utility = require('utility');
23-
24-
var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';
23+
var supportES6Map = require('../utils').supportES6Map;
2524

2625
function Encoder(options) {
2726
EncoderV1.call(this, options);
@@ -584,7 +583,7 @@ proto._writeHashMap = function (obj) {
584583

585584
this.byteBuffer.put(0x48); // H
586585

587-
if (SUPPORT_ES6_MAP && obj instanceof Map) {
586+
if (supportES6Map && obj instanceof Map) {
588587
obj.forEach(function (value, key) {
589588
this.write(key);
590589
this.write(value);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
"js-to-java": "2",
4949
"jshint": "*",
5050
"mocha": "*",
51-
"should": "5"
51+
"should": "10"
5252
},
5353
"engines": {
54-
"node": ">= 0.10.0"
54+
"node": ">= 0.12.0"
5555
}
5656
}

test/array.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**!
1+
/**
22
* Copyright(c) node-modules and other contributors.
33
* MIT Licensed
44
*

test/binary.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*!
1+
/*
22
* hessian.js - test/binary.test.js
33
*
4-
* Copyright(c) 2014
4+
* Copyright(c)
55
* MIT Licensed
66
*
77
* Authors:

0 commit comments

Comments
 (0)