@@ -161,6 +161,28 @@ describe('map.test.js', function () {
161161 } ) ;
162162 } ) ;
163163
164+ it ( 'should write es6 Map to java.util.Map' , function ( ) {
165+ if ( typeof Map !== 'function' ) {
166+ // pass if not support es6 Map
167+ return ;
168+ }
169+
170+ var map = new Map ( ) ;
171+ map . set ( { '$class' : 'java.lang.Long' , '$' : 123 } , 123456 ) ;
172+ map . set ( { '$class' : 'java.lang.Long' , '$' : 123456 } , 123 ) ;
173+ var buf = hessian . encode ( map ) ;
174+ buf . should . eql ( utils . bytes ( 'v1/map/generic' ) ) ;
175+
176+ buf = hessian . encode ( { '$class' : 'java.util.HashMap' , '$' : map } ) ;
177+ buf . should . eql ( utils . bytes ( 'v1/map/generic' ) ) ;
178+
179+ // decode auto transfer key to string
180+ hessian . decode ( utils . bytes ( 'v1/map/generic' ) , '1.0' ) . should . eql ( {
181+ '123' : 123456 ,
182+ '123456' : 123
183+ } ) ;
184+ } ) ;
185+
164186 describe ( 'v2.0' , function ( ) {
165187 // map = new HashMap();
166188 // map.put(new Integer(1), "fee");
@@ -330,4 +352,29 @@ describe('map.test.js', function () {
330352 var rv = hessian . decode ( data ) ;
331353 rv . should . eql ( { null : 'null' } ) ;
332354 } ) ;
355+
356+ it ( 'should write es6 Map to java.util.Map' , function ( ) {
357+ if ( typeof Map !== 'function' ) {
358+ // pass if not support es6 Map
359+ return ;
360+ }
361+
362+ var map = new Map ( ) ;
363+ map . set ( { '$class' : 'java.lang.Long' , '$' : 123 } , 123456 ) ;
364+ map . set ( { '$class' : 'java.lang.Long' , '$' : 123456 } , 123 ) ;
365+ var encoder = new hessian . EncoderV2 ( ) ;
366+ var buf = encoder . write ( map ) . get ( ) ;
367+ buf . should . eql ( utils . bytes ( 'v2/map/generic' ) ) ;
368+
369+ encoder . reset ( ) ;
370+
371+ buf = encoder . write ( { '$class' : 'java.util.HashMap' , '$' : map } ) . get ( ) ;
372+ buf . should . eql ( utils . bytes ( 'v2/map/generic' ) ) ;
373+
374+ // decode auto transfer key to string
375+ hessian . decode ( utils . bytes ( 'v2/map/generic' ) , '2.0' ) . should . eql ( {
376+ '123' : 123456 ,
377+ '123456' : 123
378+ } ) ;
379+ } ) ;
333380} ) ;
0 commit comments