20
20
using System . Management . Automation ;
21
21
using System . Text ;
22
22
using Newtonsoft . Json ;
23
+ using Microsoft . WindowsAzure . Commands . Utilities . Common ;
24
+ using System . Linq ;
25
+ using System . Collections . Generic ;
23
26
24
27
namespace Microsoft . Azure . Commands . Automation . Common
25
28
{
@@ -31,20 +34,41 @@ public static string Serialize(object inputObject)
31
34
{
32
35
return null ;
33
36
}
34
- while ( inputObject is PSObject && ( ( PSObject ) inputObject ) . BaseObject != null )
37
+ if ( inputObject is string @str )
35
38
{
36
- inputObject = ( ( PSObject ) inputObject ) . BaseObject ;
39
+ return str . Trim ( ) ;
37
40
}
38
- if ( inputObject is string )
41
+ else if ( inputObject is object [ ] @objectArray )
39
42
{
40
- inputObject = ( ( string ) inputObject ) . Trim ( ) ;
43
+ return SerializeArray ( objectArray ) ;
44
+ }
45
+ else if ( inputObject is PSObject @psObject )
46
+ {
47
+ return SerializePsObject ( psObject ) ;
41
48
}
42
49
return JsonConvert . SerializeObject ( inputObject ) ;
43
50
}
44
51
52
+ private static string SerializePsObject ( PSObject @psObject )
53
+ {
54
+ Dictionary < string , string > hashTable = new Dictionary < string , string > ( ) ;
55
+ foreach ( var item in @psObject . Properties )
56
+ {
57
+ hashTable . Add ( item . Name , Serialize ( item . Value ) ) ;
58
+ }
59
+
60
+ return JsonConvert . SerializeObject ( hashTable ) ;
61
+ }
62
+
63
+ private static string SerializeArray ( object [ ] objectArray )
64
+ {
65
+ List < object > objectList = objectArray . ToList ( ) ;
66
+ return string . Format ( "[{0}]" , string . Join ( "," , objectList . Select ( Serialize ) . ToList ( ) ) ) ;
67
+ }
68
+
45
69
public static PSObject Deserialize ( string json )
46
70
{
47
- if ( String . IsNullOrEmpty ( json ) )
71
+ if ( string . IsNullOrEmpty ( json ) )
48
72
{
49
73
return null ;
50
74
}
0 commit comments