Skip to content

Commit

Permalink
Moving to System.Text.Json (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellerbach authored Feb 14, 2025
1 parent 046df65 commit b56ec8b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 42 deletions.
10 changes: 5 additions & 5 deletions nanoFirmwareFlasher.Library/CloudsmithPackageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
// See LICENSE file in the project root for full license information.
//

using Newtonsoft.Json;
using System;
using System.Text.Json.Serialization;

namespace nanoFramework.Tools.FirmwareFlasher
{
[Serializable]
internal class CloudsmithPackageInfo
{
[JsonProperty("version")]
[JsonPropertyName("version")]
public string Version { get; set; }

[JsonProperty("cdn_url")]
[JsonPropertyName("cdn_url")]
public string DownloadUrl { get; set; }

[JsonProperty("name")]
[JsonPropertyName("name")]
public string TargetName { get; set; }

[JsonProperty("uploaded_at")]
[JsonPropertyName("uploaded_at")]
public DateTime PackageDate { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using nanoFramework.Tools.Debugger;
using nanoFramework.Tools.Debugger.Extensions;
using nanoFramework.Tools.FirmwareFlasher.DeploymentHelpers;
using Newtonsoft.Json;

namespace nanoFramework.Tools.FirmwareFlasher.FileDeployment
{
Expand All @@ -25,7 +25,12 @@ public class FileDeploymentManager
/// </summary>
public FileDeploymentManager(string configFilePath, string originalPort, VerbosityLevel verbosity)
{
_configuration = JsonConvert.DeserializeObject<FileDeploymentConfiguration>(File.ReadAllText(configFilePath));
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};

_configuration = JsonSerializer.Deserialize<FileDeploymentConfiguration>(File.ReadAllText(configFilePath), options);
_serialPort = string.IsNullOrEmpty(_configuration.SerialPort) ? originalPort : _configuration.SerialPort;
_verbosity = verbosity;
}
Expand Down
13 changes: 9 additions & 4 deletions nanoFirmwareFlasher.Library/FirmwareArchiveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace nanoFramework.Tools.FirmwareFlasher
{
Expand Down Expand Up @@ -55,7 +55,12 @@ public List<CloudSmithPackageDetail> GetTargetList(
{
foreach (string filePath in Directory.EnumerateFiles(_archivePath, $"*{INFOFILE_EXTENSION}"))
{
PersistedPackageInformation packageInformation = JsonConvert.DeserializeObject<PersistedPackageInformation>(File.ReadAllText(filePath));
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};

PersistedPackageInformation packageInformation = JsonSerializer.Deserialize<PersistedPackageInformation>(File.ReadAllText(filePath), options);
if (packageInformation.IsPreview == preview &&
(platform is null || platform.Value.ToString().Equals(packageInformation.Platform, StringComparison.OrdinalIgnoreCase)))
{
Expand Down Expand Up @@ -83,7 +88,7 @@ public CloudSmithPackageDetail GetLatestVersion(
{
foreach (string filePath in Directory.EnumerateFiles(_archivePath, $"{target}-*{INFOFILE_EXTENSION}"))
{
PersistedPackageInformation packageInformation = JsonConvert.DeserializeObject<PersistedPackageInformation>(File.ReadAllText(filePath));
PersistedPackageInformation packageInformation = JsonSerializer.Deserialize<PersistedPackageInformation>(File.ReadAllText(filePath));
if (packageInformation is not null && packageInformation.IsPreview == preview)
{
if (Version.TryParse(packageInformation.Version, out Version version))
Expand Down Expand Up @@ -192,7 +197,7 @@ public async Task<ExitCodes> DownloadFirmwareFromRepository(
};
File.WriteAllText(
$"{(fwFilePath.EndsWith(".zip") ? fwFilePath : Path.GetDirectoryName(fwFilePath))}{INFOFILE_EXTENSION}",
JsonConvert.SerializeObject(packageInformation)
JsonSerializer.Serialize(packageInformation)
);
}
return result;
Expand Down
11 changes: 8 additions & 3 deletions nanoFirmwareFlasher.Library/FirmwarePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.DataContracts;
using nanoFramework.Tools.Debugger;
using Newtonsoft.Json;

namespace nanoFramework.Tools.FirmwareFlasher
{
Expand Down Expand Up @@ -198,7 +198,12 @@ public static List<CloudSmithPackageDetail> GetTargetList(
return targetPackages;
}

List<CloudSmithPackageDetailJson> deserializedPackages = JsonConvert.DeserializeObject<List<CloudSmithPackageDetailJson>>(responseBody);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};

List<CloudSmithPackageDetailJson> deserializedPackages = JsonSerializer.Deserialize<List<CloudSmithPackageDetailJson>>(responseBody, options);
targetPackages.AddRange(from p in deserializedPackages
select new CloudSmithPackageDetail()
{
Expand Down Expand Up @@ -660,7 +665,7 @@ private static async Task<DownloadUrlResult> GetDownloadUrlAsync(
else
{
// parse response
packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);
packageInfo = JsonSerializer.Deserialize<List<CloudsmithPackageInfo>>(responseBody);

// sanity check
if (packageInfo.Count != 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using nanoFramework.Tools.Debugger;
using nanoFramework.Tools.FirmwareFlasher.DeploymentHelpers;
using Newtonsoft.Json;

namespace nanoFramework.Tools.FirmwareFlasher.NetworkDeployment
{
Expand All @@ -30,7 +30,12 @@ public class NetworkDeploymentManager
/// <param name="verbosity">The verbosity level.</param>
public NetworkDeploymentManager(string configFilePath, string originalPort, VerbosityLevel verbosity)
{
_configuration = JsonConvert.DeserializeObject<NetworkDeploymentConfiguration>(File.ReadAllText(configFilePath));
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};

_configuration = JsonSerializer.Deserialize<NetworkDeploymentConfiguration>(File.ReadAllText(configFilePath), options);
_serialPort = string.IsNullOrEmpty(_configuration.SerialPort) ? originalPort : _configuration.SerialPort;
_verbosity = verbosity;
}
Expand Down Expand Up @@ -299,10 +304,10 @@ public async Task<ExitCodes> DeployAsync()
// Read the device certificates from the file
deviceCertificatesBytes = File.ReadAllBytes(_configuration.DeviceCertificatesPath);
}

CheckNullPemTermination(deviceCertificatesBytes);

if (deviceCertificatesBytes != null)
{
CheckNullPemTermination(deviceCertificatesBytes);
// deploy the client certificates
OutputWriter.Write($"Updating client certificates...");
var clientCertificates = device.DebugEngine.GetAllX509DeviceCertificates();
Expand Down Expand Up @@ -339,10 +344,10 @@ public async Task<ExitCodes> DeployAsync()
// Read the CA certificates from the file
caCertificatesBytes = File.ReadAllBytes(_configuration.CACertificatesPath);
}

CheckNullPemTermination(caCertificatesBytes);

if (caCertificatesBytes != null)
{
CheckNullPemTermination(caCertificatesBytes);
// deploy the client certificates
OutputWriter.Write($"Updating client certificates...");
var caCertificates = device.DebugEngine.GetAllX509Certificates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="nanoFramework.Tools.Debugger.Net" Version="2.5.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.146" PrivateAssets="All" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
Expand Down
12 changes: 0 additions & 12 deletions nanoFirmwareFlasher.Library/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@
"resolved": "3.6.146",
"contentHash": "W1eQ8hD9Y/cZ2+ilgxECl003xr1hybpN3fMxoTlMqg++BixETBMWzS4y9s08oHJKXgKtudsoxhITNOEf1OR66w=="
},
"Newtonsoft.Json": {
"type": "Direct",
"requested": "[13.0.3, )",
"resolved": "13.0.3",
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
},
"System.ComponentModel.Annotations": {
"type": "Direct",
"requested": "[5.0.0, )",
Expand Down Expand Up @@ -304,12 +298,6 @@
"resolved": "3.6.146",
"contentHash": "W1eQ8hD9Y/cZ2+ilgxECl003xr1hybpN3fMxoTlMqg++BixETBMWzS4y9s08oHJKXgKtudsoxhITNOEf1OR66w=="
},
"Newtonsoft.Json": {
"type": "Direct",
"requested": "[13.0.3, )",
"resolved": "13.0.3",
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
},
"System.ComponentModel.Annotations": {
"type": "Direct",
"requested": "[5.0.0, )",
Expand Down
12 changes: 9 additions & 3 deletions nanoFirmwareFlasher.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using CommandLine;
using CommandLine.Text;
using Microsoft.Extensions.Configuration;
using nanoFramework.Tools.FirmwareFlasher.Extensions;
using nanoFramework.Tools.FirmwareFlasher.FileDeployment;
using nanoFramework.Tools.FirmwareFlasher.NetworkDeployment;
using Newtonsoft.Json;

namespace nanoFramework.Tools.FirmwareFlasher
{
Expand Down Expand Up @@ -130,8 +131,13 @@ private static void CheckVersion()

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

dynamic responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
string tagName = responseContent.tag_name.ToString();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};

JsonNode responseContent = JsonSerializer.Deserialize<JsonNode>(response.Content.ReadAsStringAsync().Result, options);
string tagName = responseContent["tag_name"].ToString();

latestVersion = Version.Parse(tagName.Substring(1));
}
Expand Down
6 changes: 0 additions & 6 deletions nanoFirmwareFlasher.Tool/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@
"resolved": "1.1.3",
"contentHash": "3Wrmi0kJDzClwAC+iBdUBpEKmEle8FQNsCs77fkiOIw/9oYA07bL1EZNX0kQ2OMN3xpwvl0vAtOCYY3ndDNlhQ=="
},
"Newtonsoft.Json": {
"type": "Transitive",
"resolved": "13.0.3",
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
},
"Polly": {
"type": "Transitive",
"resolved": "7.2.4",
Expand Down Expand Up @@ -734,7 +729,6 @@
"Microsoft.ApplicationInsights": "[2.22.0, )",
"Microsoft.Extensions.Configuration": "[8.0.0, )",
"Microsoft.Extensions.Configuration.Json": "[8.0.1, )",
"Newtonsoft.Json": "[13.0.3, )",
"System.ComponentModel.Annotations": "[5.0.0, )",
"System.IO.Ports": "[8.0.0, )",
"System.Net.Http": "[4.3.4, )",
Expand Down

0 comments on commit b56ec8b

Please sign in to comment.