66using System . Security . Cryptography ;
77using System . Text ;
88using System . Threading . Tasks ;
9- using Newtonsoft . Json ;
10- using Newtonsoft . Json . Linq ;
9+ using System . Text . Json ;
1110using CloudConvert . API . Models . JobModels ;
1211using CloudConvert . API . Models . TaskModels ;
1312using CloudConvert . API . Models ;
@@ -74,16 +73,17 @@ public CloudConvertAPI(string url, string api_key)
7473 private HttpRequestMessage GetRequest ( string endpoint , HttpMethod method , object model = null )
7574 {
7675 var request = new HttpRequestMessage { RequestUri = new Uri ( endpoint ) , Method = method } ;
77-
76+
7877 if ( model != null )
7978 {
80- var content = new StringContent ( JsonConvert . SerializeObject ( model ) , Encoding . UTF8 , "application/json" ) ;
79+ var content = new StringContent ( JsonSerializer . Serialize ( model , DefaultJsonSerializerOptions . SerializerOptions ) , Encoding . UTF8 , "application/json" ) ;
8180 request . Content = content ;
8281 }
83-
82+
83+
8484 request . Headers . Add ( "Authorization" , _api_key ) ;
85- request . Headers . Add ( "User-Agent" , "cloudconvert-dotnet/v" + System . Reflection . Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) + " (https://github.com/cloudconvert/cloudconvert-dotnet)" ) ;
86-
85+ request . Headers . Add ( "User-Agent" , "cloudconvert-dotnet/v" + System . Reflection . Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) + " (https://github.com/cloudconvert/cloudconvert-dotnet)" ) ;
86+
8787 return request ;
8888 }
8989
@@ -100,7 +100,7 @@ private HttpRequestMessage GetMultipartFormDataRequest(string endpoint, HttpMeth
100100 }
101101 }
102102
103- fileContent . Headers . Add ( "Content-Disposition" , $ "form-data; name=\" file\" ; filename=\" { new string ( Encoding . UTF8 . GetBytes ( fileName ) . Select ( b => ( char ) b ) . ToArray ( ) ) } \" ") ;
103+ fileContent . Headers . Add ( "Content-Disposition" , $ "form-data; name=\" file\" ; filename=\" { new string ( Encoding . UTF8 . GetBytes ( fileName ) . Select ( b => ( char ) b ) . ToArray ( ) ) } \" ") ;
104104 content . Add ( fileContent ) ;
105105
106106 request . Content = content ;
@@ -225,10 +225,10 @@ private HttpRequestMessage GetMultipartFormDataRequest(string endpoint, HttpMeth
225225 public string CreateSignedUrl ( string baseUrl , string signingSecret , JobCreateRequest job , string cacheKey = null )
226226 {
227227 string url = baseUrl ;
228- string jobJson = JsonConvert . SerializeObject ( job ) ;
228+ string jobJson = JsonSerializer . Serialize ( job , DefaultJsonSerializerOptions . SerializerOptions ) ;
229229 string base64Job = System . Convert . ToBase64String ( Encoding . ASCII . GetBytes ( jobJson ) ) . TrimEnd ( base64Padding ) . Replace ( '+' , '-' ) . Replace ( '/' , '_' ) ;
230-
231- url += "?job=" + base64Job ;
230+
231+ url += "?job=" + base64Job ;
232232
233233 if ( cacheKey != null ) {
234234 url += "&cache_key=" + cacheKey ;
@@ -254,14 +254,47 @@ private string HashHMAC(string key, string message)
254254 return BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLower ( ) ;
255255 }
256256
257+ // FIXME
257258 private Dictionary < string , string > GetParameters ( object parameters )
258259 {
259- var attributes = ( ( JToken ) parameters ) . ToList ( ) ;
260- Dictionary < string , string > dictionaryParameters = new Dictionary < string , string > ( ) ;
261- foreach ( JToken attribute in attributes )
260+ var dictionaryParameters = new Dictionary < string , string > ( ) ;
261+
262+ if ( parameters != null )
262263 {
263- JProperty jProperty = attribute . ToObject < JProperty > ( ) ;
264- dictionaryParameters . Add ( jProperty . Name , jProperty . Value . ToString ( ) ) ;
264+ // Serialize the input object to JSON.
265+ string json = JsonSerializer . Serialize ( parameters , DefaultJsonSerializerOptions . SerializerOptions ) ;
266+
267+ // Parse the JSON using JsonDocument.
268+ using ( var doc = JsonDocument . Parse ( json ) )
269+ {
270+ var root = doc . RootElement ;
271+ if ( root . ValueKind == JsonValueKind . Object )
272+ {
273+ foreach ( JsonProperty prop in root . EnumerateObject ( ) )
274+ {
275+ string valueStr ;
276+ // Replicate JToken.ToString() behavior:
277+ // For string values, return the unquoted string.
278+ // For objects and arrays, serialize them to JSON.
279+ // Otherwise, use the default ToString().
280+ switch ( prop . Value . ValueKind )
281+ {
282+ case JsonValueKind . String :
283+ valueStr = prop . Value . GetString ( ) ;
284+ break ;
285+ case JsonValueKind . Object :
286+ case JsonValueKind . Array :
287+ valueStr = JsonSerializer . Serialize ( prop . Value , DefaultJsonSerializerOptions . SerializerOptions ) ;
288+ break ;
289+ default :
290+ valueStr = prop . Value . ToString ( ) ;
291+ break ;
292+ }
293+
294+ dictionaryParameters [ prop . Name ] = valueStr ;
295+ }
296+ }
297+ }
265298 }
266299
267300 return dictionaryParameters ;
0 commit comments