8
8
using System . IO ;
9
9
using System . Linq ;
10
10
using System . Management . Automation ;
11
- using System . Reflection ;
12
11
13
12
using Google . Protobuf ;
14
13
using Microsoft . Azure . WebJobs . Script . Grpc . Messages ;
15
14
using Microsoft . Azure . Functions . PowerShellWorker . PowerShell ;
15
+ using Microsoft . PowerShell . Commands ;
16
16
using Newtonsoft . Json . Linq ;
17
17
18
18
namespace Microsoft . Azure . Functions . PowerShellWorker . Utility
@@ -94,30 +94,9 @@ internal static object ToObject(this TypedData data)
94
94
}
95
95
}
96
96
97
- // PowerShell NuGet packages only have 'System.Management.Automation.dll' as the ref assembly, and thus types from other powershell assemblies
98
- // cannot be used directly in an application that reference the PowerShell NuGet packages. This is tracked by PowerShell#8121.
99
- // Here we need to use 'Microsoft.PowerShell.Commands.JsonObject' from 'Microsoft.PowerShell.Commands.Utility'. Due the above issue, we have to
100
- // use reflection to call 'JsonObject.ConvertFromJson(...)'.
101
- private static MethodInfo s_ConvertFromJson = null ;
102
97
private static object ConvertFromJson ( string json )
103
98
{
104
- const string UtilityAssemblyFullName = "Microsoft.PowerShell.Commands.Utility, Version=6.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" ;
105
-
106
- if ( s_ConvertFromJson == null )
107
- {
108
- Assembly utilityAssembly = AppDomain . CurrentDomain . GetAssemblies ( ) . First ( asm => asm . FullName == UtilityAssemblyFullName ) ;
109
- Type jsonObjectType = utilityAssembly . GetType ( "Microsoft.PowerShell.Commands.JsonObject" ) ;
110
- s_ConvertFromJson = jsonObjectType . GetMethod (
111
- name : "ConvertFromJson" ,
112
- types : new Type [ ] { typeof ( string ) , typeof ( bool ) , typeof ( ErrorRecord ) . MakeByRefType ( ) } ,
113
- modifiers : null ) ;
114
- }
115
-
116
- object retObj = s_ConvertFromJson . Invoke ( null , new object [ ] { json , true , null } ) ;
117
- if ( retObj is PSObject psObj )
118
- {
119
- retObj = psObj . BaseObject ;
120
- }
99
+ object retObj = JsonObject . ConvertFromJson ( json , returnHashtable : true , out _ ) ;
121
100
122
101
if ( retObj is Hashtable hashtable )
123
102
{
@@ -136,6 +115,16 @@ private static object ConvertFromJson(string json)
136
115
return retObj ;
137
116
}
138
117
118
+ private static string ConvertToJson ( object fromObj )
119
+ {
120
+ var context = new JsonObject . ConvertToJsonContext (
121
+ maxDepth : 3 ,
122
+ enumsAsStrings : false ,
123
+ compressOutput : true ) ;
124
+
125
+ return JsonObject . ConvertToJson ( fromObj , in context ) ;
126
+ }
127
+
139
128
internal static RpcException ToRpcException ( this Exception exception )
140
129
{
141
130
return new RpcException
@@ -146,7 +135,7 @@ internal static RpcException ToRpcException(this Exception exception)
146
135
} ;
147
136
}
148
137
149
- private static RpcHttp ToRpcHttp ( this HttpResponseContext httpResponseContext , PowerShellManager psHelper )
138
+ private static RpcHttp ToRpcHttp ( this HttpResponseContext httpResponseContext )
150
139
{
151
140
var rpcHttp = new RpcHttp
152
141
{
@@ -155,7 +144,7 @@ private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext, P
155
144
156
145
if ( httpResponseContext . Body != null )
157
146
{
158
- rpcHttp . Body = httpResponseContext . Body . ToTypedData ( psHelper ) ;
147
+ rpcHttp . Body = httpResponseContext . Body . ToTypedData ( ) ;
159
148
}
160
149
161
150
rpcHttp . EnableContentNegotiation = httpResponseContext . EnableContentNegotiation ;
@@ -178,7 +167,7 @@ private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext, P
178
167
return rpcHttp ;
179
168
}
180
169
181
- internal static TypedData ToTypedData ( this object value , PowerShellManager psHelper )
170
+ internal static TypedData ToTypedData ( this object value )
182
171
{
183
172
if ( value is TypedData self )
184
173
{
@@ -218,14 +207,13 @@ internal static TypedData ToTypedData(this object value, PowerShellManager psHel
218
207
typedData . Stream = ByteString . FromStream ( s ) ;
219
208
break ;
220
209
case HttpResponseContext http :
221
- typedData . Http = http . ToRpcHttp ( psHelper ) ;
210
+ typedData . Http = http . ToRpcHttp ( ) ;
222
211
break ;
223
212
case string str :
224
213
if ( IsValidJson ( str ) ) { typedData . Json = str ; } else { typedData . String = str ; }
225
214
break ;
226
215
default :
227
- if ( psHelper == null ) { throw new ArgumentNullException ( nameof ( psHelper ) ) ; }
228
- typedData . Json = psHelper . ConvertToJson ( originalValue ) ;
216
+ typedData . Json = ConvertToJson ( originalValue ) ;
229
217
break ;
230
218
}
231
219
return typedData ;
0 commit comments