Skip to content

Commit

Permalink
Merge pull request #45 from node-modules/support-generic-map
Browse files Browse the repository at this point in the history
feat(generic) support generic map
  • Loading branch information
fengmk2 committed Aug 27, 2015
2 parents 53cbc75 + 9633dd3 commit 57eaa72
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ var testObject = {
encoder.write(testObject);
```

### Java Generic Map

```js
// java code:
// Map<Long, Integer> map = new HashMap<Long, Integer>();
// map.put(123L, 123456);
// map.put(123456L, 123);

var hessian = require('hessian.js');
var encoder = new hessian.Encoder();

// using es6 Map
var map = new Map();
map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456);
map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123);

encoder.write(map); // or encoder.write({ '$class': 'java.util.HashMap', '$': map })
```

## Decoder

```js
Expand Down
21 changes: 15 additions & 6 deletions lib/v1/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var utils = require('../utils');
var javaObject = require('../object');
var is = require('is-type-of');

var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';

function Encoder(options) {
options = options || {};
//array of buffer
Expand Down Expand Up @@ -263,12 +265,19 @@ proto._writeHashMap = function (obj) {
// hashmap's type is null
this.writeType('');

// hash map must sort keys
var keys = Object.keys(obj).sort();
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
this.writeString(k);
this.write(obj[k]);
if (SUPPORT_ES6_MAP && obj instanceof Map) {
obj.forEach(function (value, key) {
this.write(key);
this.write(value);
}, this);
} else {
// hash map must sort keys
var keys = Object.keys(obj).sort();
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
this.writeString(k);
this.write(obj[k]);
}
}
this.byteBuffer.put(0x7a);
return this;
Expand Down
21 changes: 15 additions & 6 deletions lib/v2/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var util = require('util');
var EncoderV1 = require('../v1/encoder');
var javaObject = require('../object');

var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function';

function Encoder(options) {
EncoderV1.call(this, options);

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

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

// hash map must sort keys
var keys = Object.keys(obj).sort();
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
this.writeString(k);
this.write(obj[k]);
if (SUPPORT_ES6_MAP && obj instanceof Map) {
obj.forEach(function (value, key) {
this.write(key);
this.write(value);
}, this);
} else {
// hash map must sort keys
var keys = Object.keys(obj).sort();
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
this.writeString(k);
this.write(obj[k]);
}
}
this.byteBuffer.putChar('Z');
return this;
Expand Down
Binary file added test/fixtures/v1/map/generic.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/v2/map/generic.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
H�{��@=�@�{Z
47 changes: 47 additions & 0 deletions test/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ describe('map.test.js', function () {
});
});

it('should write es6 Map to java.util.Map', function() {
if (typeof Map !== 'function') {
// pass if not support es6 Map
return;
}

var map = new Map();
map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456);
map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123);
var buf = hessian.encode(map);
buf.should.eql(utils.bytes('v1/map/generic'));

buf = hessian.encode({ '$class': 'java.util.HashMap', '$': map });
buf.should.eql(utils.bytes('v1/map/generic'));

// decode auto transfer key to string
hessian.decode(utils.bytes('v1/map/generic'), '1.0').should.eql({
'123': 123456,
'123456': 123
});
});

describe('v2.0', function () {
// map = new HashMap();
// map.put(new Integer(1), "fee");
Expand Down Expand Up @@ -330,4 +352,29 @@ describe('map.test.js', function () {
var rv = hessian.decode(data);
rv.should.eql({null: 'null'});
});

it('should write es6 Map to java.util.Map', function() {
if (typeof Map !== 'function') {
// pass if not support es6 Map
return;
}

var map = new Map();
map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456);
map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123);
var encoder = new hessian.EncoderV2();
var buf = encoder.write(map).get();
buf.should.eql(utils.bytes('v2/map/generic'));

encoder.reset();

buf = encoder.write({ '$class': 'java.util.HashMap', '$': map }).get();
buf.should.eql(utils.bytes('v2/map/generic'));

// decode auto transfer key to string
hessian.decode(utils.bytes('v2/map/generic'), '2.0').should.eql({
'123': 123456,
'123456': 123
});
});
});

0 comments on commit 57eaa72

Please sign in to comment.