Skip to content

Commit b56ec8b

Browse files
authored
Moving to System.Text.Json (#314)
1 parent 046df65 commit b56ec8b

File tree

9 files changed

+49
-42
lines changed

9 files changed

+49
-42
lines changed

nanoFirmwareFlasher.Library/CloudsmithPackageInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6-
using Newtonsoft.Json;
76
using System;
7+
using System.Text.Json.Serialization;
88

99
namespace nanoFramework.Tools.FirmwareFlasher
1010
{
1111
[Serializable]
1212
internal class CloudsmithPackageInfo
1313
{
14-
[JsonProperty("version")]
14+
[JsonPropertyName("version")]
1515
public string Version { get; set; }
1616

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

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

23-
[JsonProperty("uploaded_at")]
23+
[JsonPropertyName("uploaded_at")]
2424
public DateTime PackageDate { get; set; }
2525
}
2626
}

nanoFirmwareFlasher.Library/FileDeployment/FileDeploymentManager.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
using System;
55
using System.IO;
6+
using System.Text.Json;
67
using System.Threading.Tasks;
78
using nanoFramework.Tools.Debugger;
89
using nanoFramework.Tools.Debugger.Extensions;
910
using nanoFramework.Tools.FirmwareFlasher.DeploymentHelpers;
10-
using Newtonsoft.Json;
1111

1212
namespace nanoFramework.Tools.FirmwareFlasher.FileDeployment
1313
{
@@ -25,7 +25,12 @@ public class FileDeploymentManager
2525
/// </summary>
2626
public FileDeploymentManager(string configFilePath, string originalPort, VerbosityLevel verbosity)
2727
{
28-
_configuration = JsonConvert.DeserializeObject<FileDeploymentConfiguration>(File.ReadAllText(configFilePath));
28+
var options = new JsonSerializerOptions
29+
{
30+
PropertyNameCaseInsensitive = true,
31+
};
32+
33+
_configuration = JsonSerializer.Deserialize<FileDeploymentConfiguration>(File.ReadAllText(configFilePath), options);
2934
_serialPort = string.IsNullOrEmpty(_configuration.SerialPort) ? originalPort : _configuration.SerialPort;
3035
_verbosity = verbosity;
3136
}

nanoFirmwareFlasher.Library/FirmwareArchiveManager.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Text.Json;
78
using System.Threading.Tasks;
8-
using Newtonsoft.Json;
99

1010
namespace nanoFramework.Tools.FirmwareFlasher
1111
{
@@ -55,7 +55,12 @@ public List<CloudSmithPackageDetail> GetTargetList(
5555
{
5656
foreach (string filePath in Directory.EnumerateFiles(_archivePath, $"*{INFOFILE_EXTENSION}"))
5757
{
58-
PersistedPackageInformation packageInformation = JsonConvert.DeserializeObject<PersistedPackageInformation>(File.ReadAllText(filePath));
58+
var options = new JsonSerializerOptions
59+
{
60+
PropertyNameCaseInsensitive = true,
61+
};
62+
63+
PersistedPackageInformation packageInformation = JsonSerializer.Deserialize<PersistedPackageInformation>(File.ReadAllText(filePath), options);
5964
if (packageInformation.IsPreview == preview &&
6065
(platform is null || platform.Value.ToString().Equals(packageInformation.Platform, StringComparison.OrdinalIgnoreCase)))
6166
{
@@ -83,7 +88,7 @@ public CloudSmithPackageDetail GetLatestVersion(
8388
{
8489
foreach (string filePath in Directory.EnumerateFiles(_archivePath, $"{target}-*{INFOFILE_EXTENSION}"))
8590
{
86-
PersistedPackageInformation packageInformation = JsonConvert.DeserializeObject<PersistedPackageInformation>(File.ReadAllText(filePath));
91+
PersistedPackageInformation packageInformation = JsonSerializer.Deserialize<PersistedPackageInformation>(File.ReadAllText(filePath));
8792
if (packageInformation is not null && packageInformation.IsPreview == preview)
8893
{
8994
if (Version.TryParse(packageInformation.Version, out Version version))
@@ -192,7 +197,7 @@ public async Task<ExitCodes> DownloadFirmwareFromRepository(
192197
};
193198
File.WriteAllText(
194199
$"{(fwFilePath.EndsWith(".zip") ? fwFilePath : Path.GetDirectoryName(fwFilePath))}{INFOFILE_EXTENSION}",
195-
JsonConvert.SerializeObject(packageInformation)
200+
JsonSerializer.Serialize(packageInformation)
196201
);
197202
}
198203
return result;

nanoFirmwareFlasher.Library/FirmwarePackage.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
using System.Linq;
99
using System.Net.Http;
1010
using System.Reflection;
11+
using System.Text.Json;
1112
using System.Text.RegularExpressions;
1213
using System.Threading;
1314
using System.Threading.Tasks;
1415
using Microsoft.ApplicationInsights.DataContracts;
1516
using nanoFramework.Tools.Debugger;
16-
using Newtonsoft.Json;
1717

1818
namespace nanoFramework.Tools.FirmwareFlasher
1919
{
@@ -198,7 +198,12 @@ public static List<CloudSmithPackageDetail> GetTargetList(
198198
return targetPackages;
199199
}
200200

201-
List<CloudSmithPackageDetailJson> deserializedPackages = JsonConvert.DeserializeObject<List<CloudSmithPackageDetailJson>>(responseBody);
201+
var options = new JsonSerializerOptions
202+
{
203+
PropertyNameCaseInsensitive = true,
204+
};
205+
206+
List<CloudSmithPackageDetailJson> deserializedPackages = JsonSerializer.Deserialize<List<CloudSmithPackageDetailJson>>(responseBody, options);
202207
targetPackages.AddRange(from p in deserializedPackages
203208
select new CloudSmithPackageDetail()
204209
{
@@ -660,7 +665,7 @@ private static async Task<DownloadUrlResult> GetDownloadUrlAsync(
660665
else
661666
{
662667
// parse response
663-
packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);
668+
packageInfo = JsonSerializer.Deserialize<List<CloudsmithPackageInfo>>(responseBody);
664669

665670
// sanity check
666671
if (packageInfo.Count != 1)

nanoFirmwareFlasher.Library/NetworkDeployment/NetworkDeploymentManager.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
using System.IO;
66
using System.Net;
77
using System.Text;
8+
using System.Text.Json;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using nanoFramework.Tools.Debugger;
1112
using nanoFramework.Tools.FirmwareFlasher.DeploymentHelpers;
12-
using Newtonsoft.Json;
1313

1414
namespace nanoFramework.Tools.FirmwareFlasher.NetworkDeployment
1515
{
@@ -30,7 +30,12 @@ public class NetworkDeploymentManager
3030
/// <param name="verbosity">The verbosity level.</param>
3131
public NetworkDeploymentManager(string configFilePath, string originalPort, VerbosityLevel verbosity)
3232
{
33-
_configuration = JsonConvert.DeserializeObject<NetworkDeploymentConfiguration>(File.ReadAllText(configFilePath));
33+
var options = new JsonSerializerOptions
34+
{
35+
PropertyNameCaseInsensitive = true,
36+
};
37+
38+
_configuration = JsonSerializer.Deserialize<NetworkDeploymentConfiguration>(File.ReadAllText(configFilePath), options);
3439
_serialPort = string.IsNullOrEmpty(_configuration.SerialPort) ? originalPort : _configuration.SerialPort;
3540
_verbosity = verbosity;
3641
}
@@ -299,10 +304,10 @@ public async Task<ExitCodes> DeployAsync()
299304
// Read the device certificates from the file
300305
deviceCertificatesBytes = File.ReadAllBytes(_configuration.DeviceCertificatesPath);
301306
}
302-
303-
CheckNullPemTermination(deviceCertificatesBytes);
307+
304308
if (deviceCertificatesBytes != null)
305309
{
310+
CheckNullPemTermination(deviceCertificatesBytes);
306311
// deploy the client certificates
307312
OutputWriter.Write($"Updating client certificates...");
308313
var clientCertificates = device.DebugEngine.GetAllX509DeviceCertificates();
@@ -339,10 +344,10 @@ public async Task<ExitCodes> DeployAsync()
339344
// Read the CA certificates from the file
340345
caCertificatesBytes = File.ReadAllBytes(_configuration.CACertificatesPath);
341346
}
342-
343-
CheckNullPemTermination(caCertificatesBytes);
347+
344348
if (caCertificatesBytes != null)
345349
{
350+
CheckNullPemTermination(caCertificatesBytes);
346351
// deploy the client certificates
347352
OutputWriter.Write($"Updating client certificates...");
348353
var caCertificates = device.DebugEngine.GetAllX509Certificates();

nanoFirmwareFlasher.Library/nanoFirmwareFlasher.Library.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
6464
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
6565
<PackageReference Include="nanoFramework.Tools.Debugger.Net" Version="2.5.10" />
66-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
6766
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.146" PrivateAssets="All" />
6867
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
6968
<PackageReference Include="System.IO.Ports" Version="8.0.0" />

nanoFirmwareFlasher.Library/packages.lock.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@
5050
"resolved": "3.6.146",
5151
"contentHash": "W1eQ8hD9Y/cZ2+ilgxECl003xr1hybpN3fMxoTlMqg++BixETBMWzS4y9s08oHJKXgKtudsoxhITNOEf1OR66w=="
5252
},
53-
"Newtonsoft.Json": {
54-
"type": "Direct",
55-
"requested": "[13.0.3, )",
56-
"resolved": "13.0.3",
57-
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
58-
},
5953
"System.ComponentModel.Annotations": {
6054
"type": "Direct",
6155
"requested": "[5.0.0, )",
@@ -304,12 +298,6 @@
304298
"resolved": "3.6.146",
305299
"contentHash": "W1eQ8hD9Y/cZ2+ilgxECl003xr1hybpN3fMxoTlMqg++BixETBMWzS4y9s08oHJKXgKtudsoxhITNOEf1OR66w=="
306300
},
307-
"Newtonsoft.Json": {
308-
"type": "Direct",
309-
"requested": "[13.0.3, )",
310-
"resolved": "13.0.3",
311-
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
312-
},
313301
"System.ComponentModel.Annotations": {
314302
"type": "Direct",
315303
"requested": "[5.0.0, )",

nanoFirmwareFlasher.Tool/Program.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
using System.Net.Http;
1111
using System.Net.Http.Headers;
1212
using System.Reflection;
13+
using System.Text.Json;
14+
using System.Text.Json.Nodes;
1315
using System.Threading.Tasks;
1416
using CommandLine;
1517
using CommandLine.Text;
1618
using Microsoft.Extensions.Configuration;
1719
using nanoFramework.Tools.FirmwareFlasher.Extensions;
1820
using nanoFramework.Tools.FirmwareFlasher.FileDeployment;
1921
using nanoFramework.Tools.FirmwareFlasher.NetworkDeployment;
20-
using Newtonsoft.Json;
2122

2223
namespace nanoFramework.Tools.FirmwareFlasher
2324
{
@@ -130,8 +131,13 @@ private static void CheckVersion()
130131

131132
HttpResponseMessage response = client.GetAsync("https://api.github.com/repos/nanoframework/nanoFirmwareFlasher/releases/latest").Result;
132133

133-
dynamic responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
134-
string tagName = responseContent.tag_name.ToString();
134+
var options = new JsonSerializerOptions
135+
{
136+
PropertyNameCaseInsensitive = true,
137+
};
138+
139+
JsonNode responseContent = JsonSerializer.Deserialize<JsonNode>(response.Content.ReadAsStringAsync().Result, options);
140+
string tagName = responseContent["tag_name"].ToString();
135141

136142
latestVersion = Version.Parse(tagName.Substring(1));
137143
}

nanoFirmwareFlasher.Tool/packages.lock.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@
133133
"resolved": "1.1.3",
134134
"contentHash": "3Wrmi0kJDzClwAC+iBdUBpEKmEle8FQNsCs77fkiOIw/9oYA07bL1EZNX0kQ2OMN3xpwvl0vAtOCYY3ndDNlhQ=="
135135
},
136-
"Newtonsoft.Json": {
137-
"type": "Transitive",
138-
"resolved": "13.0.3",
139-
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
140-
},
141136
"Polly": {
142137
"type": "Transitive",
143138
"resolved": "7.2.4",
@@ -734,7 +729,6 @@
734729
"Microsoft.ApplicationInsights": "[2.22.0, )",
735730
"Microsoft.Extensions.Configuration": "[8.0.0, )",
736731
"Microsoft.Extensions.Configuration.Json": "[8.0.1, )",
737-
"Newtonsoft.Json": "[13.0.3, )",
738732
"System.ComponentModel.Annotations": "[5.0.0, )",
739733
"System.IO.Ports": "[8.0.0, )",
740734
"System.Net.Http": "[4.3.4, )",

0 commit comments

Comments
 (0)