Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Issue #31 Replace Newtonsoft.Json with System.Text.Json
Browse files Browse the repository at this point in the history
Signed-off-by: Gerard Braad <[email protected]>
  • Loading branch information
gbraad committed Mar 11, 2021
1 parent bba5463 commit c9ae216
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 84 deletions.
12 changes: 10 additions & 2 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
33 changes: 21 additions & 12 deletions DaemonCommander/DaemonCommander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using System.Text;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace tray_windows.Daemon
{
Expand Down Expand Up @@ -96,7 +97,9 @@ private string SendCommand(string command)
daemonSocket.Send(msg);
daemonSocket.Receive(resp);
daemonSocket.Close();
return Encoding.ASCII.GetString(resp);

var result = Encoding.ASCII.GetString(resp);
return result.Replace("\0", string.Empty);
}
catch (SocketException e)
{
Expand All @@ -110,15 +113,18 @@ private string SendCommand(ConfigSetCommand command)
{
var resp = new byte[2048];
daemonSocket.Connect(daemonSocketEp);
var cmd = JsonConvert.SerializeObject(command, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

JsonSerializerOptions options = new JsonSerializerOptions();
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
var cmd = JsonSerializer.Serialize<ConfigSetCommand>(command, options);

byte[] msg = Encoding.ASCII.GetBytes(cmd);
daemonSocket.Send(msg);
daemonSocket.Receive(resp);
daemonSocket.Close();
return Encoding.ASCII.GetString(resp);

var result = Encoding.ASCII.GetString(resp);
return result.Replace("\0", string.Empty);
}
catch (SocketException e)
{
Expand All @@ -132,15 +138,18 @@ private string SendCommand(ConfigUnsetCommand command)
{
var resp = new byte[2048];
daemonSocket.Connect(daemonSocketEp);
var cmd = JsonConvert.SerializeObject(command, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

JsonSerializerOptions options = new JsonSerializerOptions();
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
var cmd = JsonSerializer.Serialize<ConfigUnsetCommand>(command, options);

byte[] msg = Encoding.ASCII.GetBytes(cmd);
daemonSocket.Send(msg);
daemonSocket.Receive(resp);
daemonSocket.Close();
return Encoding.ASCII.GetString(resp);

var result = Encoding.ASCII.GetString(resp);
return result.Replace("\0", string.Empty);
}
catch (SocketException e)
{
Expand Down
69 changes: 29 additions & 40 deletions DaemonCommander/Handlers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text.Json;
using tray_windows.Daemon;

namespace tray_windows
Expand All @@ -9,12 +9,11 @@ class Handlers
{
public static StartResult HandleStart()
{
var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();
try
{
var r = d.Start();
StartResult sr = JsonConvert.DeserializeObject<StartResult>(r);
return sr;
var result = daemon.Start();
return JsonSerializer.Deserialize<StartResult>(result);
}
catch (SocketException ex)
{
Expand All @@ -30,13 +29,12 @@ public static StartResult HandleStart()

public static StopResult HandleStop()
{
var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();

try
{
var r = d.Stop();
StopResult sr = JsonConvert.DeserializeObject<StopResult>(r);
return sr;
var result = daemon.Stop();
return JsonSerializer.Deserialize<StopResult>(result);
}
catch (SocketException ex)
{
Expand All @@ -47,12 +45,11 @@ public static StopResult HandleStop()

public static DeleteResult HandleDelete()
{
var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();
try
{
var r = d.Delete();
DeleteResult dr = JsonConvert.DeserializeObject<DeleteResult>(r);
return dr;
var result = daemon.Delete();
return JsonSerializer.Deserialize<DeleteResult>(result);
}
catch (SocketException ex)
{
Expand All @@ -63,12 +60,11 @@ public static DeleteResult HandleDelete()

public static StatusResult HandleStatus()
{
var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();
try
{
var r = d.GetStatus();
StatusResult sr = JsonConvert.DeserializeObject<StatusResult>(r);
return sr;
var result = daemon.GetStatus();
return JsonSerializer.Deserialize<StatusResult>(result);
}
catch (SocketException ex)
{
Expand All @@ -79,12 +75,11 @@ public static StatusResult HandleStatus()

public static ConsoleResult HandleOpenWebConsole()
{
var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();
try
{
var r = d.GetWebconsoleURL();
ConsoleResult cr = JsonConvert.DeserializeObject<ConsoleResult>(r);
return cr;
var result = daemon.GetWebconsoleURL();
return JsonSerializer.Deserialize<ConsoleResult>(result);
}
catch (SocketException ex)
{
Expand All @@ -95,66 +90,60 @@ public static ConsoleResult HandleOpenWebConsole()

public static ConfigResult GetConfig()
{
ConfigResult cr = new ConfigResult();
var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();
try
{
var r = d.GetAllConfig();
cr = JsonConvert.DeserializeObject<ConfigResult>(r);
return cr;
var result = daemon.GetAllConfig();
return JsonSerializer.Deserialize<ConfigResult>(result);
}
catch (SocketException ex)
{
DisplayMessageBox.Error(ex.Message);
return cr;
return new ConfigResult(); // returning an empty result?
}
}

public static SetUnsetConfig SetConfig(Dictionary<string, dynamic> cfg)
{
var result = new SetUnsetConfig();
var config = new ConfigSetCommand();
var configArgs = new ConfigSetCommandArg();
configArgs.properties = cfg;

config.command = "setconfig";
config.args = configArgs;

var d = new Daemon.DaemonCommander();
var daemon = new DaemonCommander();
try
{
var r = d.SetConfig(config);
result = JsonConvert.DeserializeObject<SetUnsetConfig>(r);
return result;
var result = daemon.SetConfig(config);
return JsonSerializer.Deserialize<SetUnsetConfig>(result);
}
catch (SocketException ex)
{
DisplayMessageBox.Error(ex.Message);
return result;
return new SetUnsetConfig(); // returning an empty result?
}
}

public static SetUnsetConfig UnsetConfig(List<string> cfg)
{
var result = new SetUnsetConfig();
var config = new ConfigUnsetCommand();
var configArgs = new ConfigUnsetCommandArg();
configArgs.properties = cfg;

config.command = "unsetconfig";
config.args = configArgs;

var d = new Daemon.DaemonCommander();
var daemon = new Daemon.DaemonCommander();
try
{
var r = d.UnsetConfig(config);
result = JsonConvert.DeserializeObject<SetUnsetConfig>(r);
return result;
var result = daemon.UnsetConfig(config);
return JsonSerializer.Deserialize<SetUnsetConfig>(result);
}
catch (SocketException ex)
{
DisplayMessageBox.Error(ex.Message);
return result;
return new SetUnsetConfig(); // returning an empty result?
}
}

Expand Down
44 changes: 22 additions & 22 deletions DaemonCommander/ResponseClasses.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace tray_windows
{
Expand Down Expand Up @@ -77,67 +77,67 @@ struct Config
public string nameserver;
public int memory;

[JsonProperty("disable-update-check")]
[JsonPropertyName("disable-update-check")]
public bool DisableUpdateCheck { get; set; }

[JsonProperty("disk-size")]
[JsonPropertyName("disk-size")]
public int DiskSize {get; set;}

[JsonProperty("enable-experimental-features")]
[JsonPropertyName("enable-experimental-features")]
public bool EnableExperimentalFeatures { get; set; }

[JsonProperty("http-proxy")]
[JsonPropertyName("http-proxy")]
public string HttpProxy { get; set; }

[JsonProperty("https-proxy")]
[JsonPropertyName("https-proxy")]
public string HttpsProxy { get; set; }

[JsonProperty("no-proxy")]
[JsonPropertyName("no-proxy")]
public string NoProxy { get; set; }

[JsonProperty("proxy-ca-file")]
[JsonPropertyName("proxy-ca-file")]
public string ProxyCAFile { get; set; }

[JsonProperty("pull-secret-file")]
[JsonPropertyName("pull-secret-file")]
public string PullSecretFile { get; set; }

[JsonProperty("network-mode")]
[JsonPropertyName("network-mode")]
public string NetworkMode { get; set; }

[JsonProperty("skip-check-admin-helper-cached")]
[JsonPropertyName("skip-check-admin-helper-cached")]
public bool SkipCheckAdminHelperCached { get; set; }

[JsonProperty("skip-check-administrator-user")]
[JsonPropertyName("skip-check-administrator-user")]
public bool SkipCheckAdminUser { get; set; }

[JsonProperty("skip-check-bundle-extracted")]
[JsonPropertyName("skip-check-bundle-extracted")]
public bool SkipCheckBundleExtracted { get; set; }

[JsonProperty("skip-check-hyperv-installed")]
[JsonPropertyName("skip-check-hyperv-installed")]
public bool SkipCheckHypervInstalled { get; set; }

[JsonProperty("skip-check-hyperv-service-running")]
[JsonPropertyName("skip-check-hyperv-service-running")]
public bool SkipCheckHypervServiceRunning;

[JsonProperty("skip-check-hyperv-switch")]
[JsonPropertyName("skip-check-hyperv-switch")]
public bool SkipCheckHypervSwitch { get; set; }

[JsonProperty("skip-check-podman-cached")]
[JsonPropertyName("skip-check-podman-cached")]
public bool SkipCheckPodmanCached { get; set; }

[JsonProperty("skip-check-ram")]
[JsonPropertyName("skip-check-ram")]
public bool SkipCheckRam { get; set; }

[JsonProperty("skip-check-user-in-hyperv-group")]
[JsonPropertyName("skip-check-user-in-hyperv-group")]
public bool SkipCheckUserInHypervGroup { get; set; }

[JsonProperty("skip-check-vscok")]
[JsonPropertyName("skip-check-vscok")]
public bool SkipCheckVsock { get; set; }

[JsonProperty("skip-check-windows-edition")]
[JsonPropertyName("skip-check-windows-edition")]
public bool SkipCheckWindowsEdition { get; set; }

[JsonProperty("skip-check-windows-version")]
[JsonPropertyName("skip-check-windows-version")]
public bool SkipCheckWindowsVersion { get; set; }
}

Expand Down
8 changes: 4 additions & 4 deletions Forms/AboutForm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Drawing;
using System.Text.Json;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace tray_windows
{
Expand All @@ -26,11 +26,11 @@ private void AboutForm_Load(object sender, EventArgs e)

private void GetVersion(object sender, EventArgs e)
{
var d = new Daemon.DaemonCommander();
var daemon = new Daemon.DaemonCommander();
try
{
var r = d.GetVersion();
version = JsonConvert.DeserializeObject<VersionResult>(r);
var result = daemon.GetVersion();
version = JsonSerializer.Deserialize<VersionResult>(result);
if (version.Success) {
CrcVersionLabel.Text = String.Format("{0}+{1}", version.CrcVersion, version.CommitSha);
OcpVersion.Text = version.OpenshiftVersion;
Expand Down
1 change: 0 additions & 1 deletion Forms/CrcSettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private void CrcSettingsForm_Load(object sender, EventArgs e)
this.FormClosing += CrcSettingsForm_FormClosing;
this.tabControl1.SelectedIndexChanged += TabControl1_SelectedIndexChanged;


currentConfig = Handlers.GetConfig();
LoadConfigs(currentConfig);
}
Expand Down
Loading

0 comments on commit c9ae216

Please sign in to comment.