Skip to content

Commit b49b38d

Browse files
authored
Merge pull request #24 from Combeenation/convert-to-system-text-json
Use System.Text.Json instead of Newtonsoft.Json
2 parents d842282 + 8cc575d commit b49b38d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+454
-419
lines changed

CloudConvert.API/CloudConvert.API.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
21-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2221
<PackageReference Include="System.Net.Http" Version="4.3.4" />
2322
</ItemGroup>
2423

CloudConvert.API/CloudConvertAPI.cs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using System.Security.Cryptography;
77
using System.Text;
88
using System.Threading.Tasks;
9-
using Newtonsoft.Json;
10-
using Newtonsoft.Json.Linq;
9+
using System.Text.Json;
1110
using CloudConvert.API.Models.JobModels;
1211
using CloudConvert.API.Models.TaskModels;
1312
using 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;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text.Json.Serialization;
2+
using System.Text.Json;
3+
4+
namespace CloudConvert.API
5+
{
6+
public class DefaultJsonSerializerOptions
7+
{
8+
public static readonly JsonSerializerOptions SerializerOptions = new()
9+
{
10+
Converters = { new JsonStringEnumConverter() },
11+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
12+
};
13+
}
14+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json.Serialization;
22

33
namespace CloudConvert.API.Models
44
{
55
public partial class ErrorResponse
66
{
7-
[JsonProperty("message")]
7+
[JsonPropertyName("message")]
88
public string Message { get; set; }
99

10-
[JsonProperty("code")]
10+
[JsonPropertyName("code")]
1111
public string Code { get; set; }
1212

13-
[JsonProperty("errors")]
13+
[JsonPropertyName("errors")]
1414
public object Errors { get; set; }
1515
}
1616
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json.Serialization;
22

33
namespace CloudConvert.API.Models.ExportOperations
44
{
55
public class ExportAzureBlobCreateRequest
66
{
7-
[JsonProperty("operation")]
8-
public static string Operation = "export/azure/blob";
7+
[JsonPropertyName("operation")]
8+
public string Operation { get; } = "export/azure/blob";
99

1010
/// <summary>
1111
/// The input task name(s) for this task.
1212
/// input: string | string[];
1313
/// </summary>
14-
[JsonProperty("input")]
15-
public dynamic Input { get; set; }
14+
[JsonPropertyName("input")]
15+
public object Input { get; set; }
1616

17-
[JsonProperty("storage_account")]
17+
[JsonPropertyName("storage_account")]
1818
public string Storage_Account { get; set; }
1919

20-
[JsonProperty("storage_access_key", NullValueHandling = NullValueHandling.Ignore)]
20+
[JsonPropertyName("storage_access_key")]
2121
public string Storage_Access_Key { get; set; }
2222

23-
[JsonProperty("sas_token", NullValueHandling = NullValueHandling.Ignore)]
23+
[JsonPropertyName("sas_token")]
2424
public string Sas_Token { get; set; }
2525

26-
[JsonProperty("container")]
26+
[JsonPropertyName("container")]
2727
public string Container { get; set; }
2828

29-
[JsonProperty("blob", NullValueHandling = NullValueHandling.Ignore)]
29+
[JsonPropertyName("blob")]
3030
public string Blob { get; set; }
3131

32-
[JsonProperty("blob_prefix", NullValueHandling = NullValueHandling.Ignore)]
32+
[JsonPropertyName("blob_prefix")]
3333
public string Blob_Prefix { get; set; }
3434
}
3535
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json.Serialization;
22

33
namespace CloudConvert.API.Models.ExportOperations
44
{
55
public class ExportGoogleCloudStorageCreateRequest
66
{
7-
[JsonProperty("operation")]
8-
public static string Operation = "export/google-cloud-storage";
7+
[JsonPropertyName("operation")]
8+
public string Operation { get; } = "export/google-cloud-storage";
99

1010
/// <summary>
1111
/// The input task name(s) for this task.
1212
/// input: string | string[];
1313
/// </summary>
14-
[JsonProperty("input")]
15-
public dynamic Input { get; set; }
14+
[JsonPropertyName("input")]
15+
public object Input { get; set; }
1616

17-
[JsonProperty("project_id")]
17+
[JsonPropertyName("project_id")]
1818
public string ProjectId { get; set; }
1919

20-
[JsonProperty("bucket")]
20+
[JsonPropertyName("bucket")]
2121
public string Bucket { get; set; }
2222

23-
[JsonProperty("client_email")]
23+
[JsonPropertyName("client_email")]
2424
public string Client_Email { get; set; }
2525

26-
[JsonProperty("private_key")]
26+
[JsonPropertyName("private_key")]
2727
public string Private_Key { get; set; }
2828

29-
[JsonProperty("file", NullValueHandling = NullValueHandling.Ignore)]
29+
[JsonPropertyName("file")]
3030
public string File { get; set; }
3131

32-
[JsonProperty("file_prefix", NullValueHandling = NullValueHandling.Ignore)]
32+
[JsonPropertyName("file_prefix")]
3333
public string File_Prefix { get; set; }
3434
}
3535
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json.Serialization;
22

33
namespace CloudConvert.API.Models.ExportOperations
44
{
55
public class ExportOpenStackCreateRequest
66
{
7-
[JsonProperty("operation")]
8-
public static string Operation = "export/openstack";
7+
[JsonPropertyName("operation")]
8+
public string Operation { get; } = "export/openstack";
99

1010
/// <summary>
1111
/// The input task name(s) for this task.
1212
/// input: string | string[];
1313
/// </summary>
14-
[JsonProperty("input")]
15-
public dynamic Input { get; set; }
14+
[JsonPropertyName("input")]
15+
public object Input { get; set; }
1616

17-
[JsonProperty("auth_url")]
17+
[JsonPropertyName("auth_url")]
1818
public string Auth_Url { get; set; }
1919

20-
[JsonProperty("username")]
20+
[JsonPropertyName("username")]
2121
public string Username { get; set; }
2222

23-
[JsonProperty("password")]
23+
[JsonPropertyName("password")]
2424
public string Password { get; set; }
2525

26-
[JsonProperty("region")]
26+
[JsonPropertyName("region")]
2727
public string Region { get; set; }
2828

29-
[JsonProperty("container")]
29+
[JsonPropertyName("container")]
3030
public string Container { get; set; }
3131

32-
[JsonProperty("file", NullValueHandling = NullValueHandling.Ignore)]
32+
[JsonPropertyName("file")]
3333
public string File { get; set; }
3434

35-
[JsonProperty("file_prefix", NullValueHandling = NullValueHandling.Ignore)]
35+
[JsonPropertyName("file_prefix")]
3636
public string FilePrefix { get; set; }
3737
}
3838
}

0 commit comments

Comments
 (0)