@@ -297,7 +297,10 @@ export class JsonSerializer {
297
297
}
298
298
299
299
const type = Reflection . getType ( instance , propertyKey ) ;
300
- const isArrayProperty = type ?. name ?. toLowerCase ( ) === 'array' ;
300
+ const typeName = type ?. name ?. toLowerCase ( ) ;
301
+ const isArrayProperty = typeName === 'array' ;
302
+ const isSetProperty = typeName === 'set' ;
303
+ const isMapProperty = typeName === 'map' ;
301
304
let propertyType = metadata . type || type ;
302
305
303
306
if ( metadata . beforeDeserialize ) {
@@ -307,10 +310,18 @@ export class JsonSerializer {
307
310
let property : any ;
308
311
const predicate = metadata . predicate ;
309
312
310
- if ( metadata . isDictionary ) {
313
+ if ( metadata . isDictionary || isMapProperty ) {
311
314
property = this . deserializeDictionary ( dataSource , propertyType , predicate ) ;
312
- } else if ( isArrayProperty ) {
315
+
316
+ if ( isMapProperty ) {
317
+ property = new Map ( Object . entries ( property ) ) ;
318
+ }
319
+ } else if ( isArrayProperty || isSetProperty ) {
313
320
property = this . deserializeArray ( dataSource , propertyType , predicate ) ;
321
+
322
+ if ( isSetProperty ) {
323
+ property = new Set ( property ) ;
324
+ }
314
325
} else if (
315
326
( ! isJsonObject ( propertyType ) && ! predicate ) ||
316
327
( predicate && ! predicate ( dataSource , obj ) )
@@ -550,18 +561,40 @@ export class JsonSerializer {
550
561
private serializeProperty ( instance : object , key : string , metadata : JsonPropertyMetadata ) : any {
551
562
const property = instance [ key ] ;
552
563
const type = Reflection . getType ( instance , key ) ;
553
- const isArrayProperty = type ?. name ?. toLocaleLowerCase ( ) === 'array' ;
564
+ const typeName = type ?. name ?. toLowerCase ( ) ;
565
+ const isArrayProperty = typeName === 'array' ;
566
+ const isSetProperty = typeName === 'set' ;
567
+ const isMapProperty = typeName === 'map' ;
554
568
const predicate = metadata . predicate ;
555
569
const propertyType = metadata . type || type ;
556
570
const isJsonObjectProperty = isJsonObject ( propertyType ) ;
557
571
558
572
if ( property && ( isJsonObjectProperty || predicate ) ) {
559
- if ( isArrayProperty ) {
560
- return this . serializeObjectArray ( property ) ;
573
+ if ( isArrayProperty || isSetProperty ) {
574
+ const array = isSetProperty ? Array . from ( property ) : property ;
575
+ return this . serializeObjectArray ( array ) ;
561
576
}
562
577
563
- if ( metadata . isDictionary ) {
564
- return this . serializeDictionary ( property ) ;
578
+ if ( metadata . isDictionary || isMapProperty ) {
579
+ if ( ! isMapProperty ) {
580
+ return this . serializeDictionary ( property ) ;
581
+ }
582
+
583
+ const obj = { } ;
584
+ property . forEach ( ( value , mapKey ) => {
585
+ if ( isString ( mapKey ) ) {
586
+ obj [ mapKey ] = value ;
587
+ } else {
588
+ this . error (
589
+ `Fail to serialize: type of '${ typeof mapKey } ' is not assignable to type 'string'.\nReceived: ${ JSON . stringify (
590
+ mapKey
591
+ ) } .`
592
+ ) ;
593
+ }
594
+ } ) ;
595
+
596
+ console . log ( obj ) ;
597
+ return this . serializeDictionary ( obj ) ;
565
598
}
566
599
567
600
return this . serializeObject ( property ) ;
0 commit comments