Posh-SSH
Posh-SSH copied to clipboard
Remove Newtonsoft.Json dependency
Since the dependency on Newtonsoft.Json
can cause problems, there is an option to replace it with DataContractJsonSerializer
It is about 3-4 times slower, but our configs are quite simple, it is unlikely that this will spoil everything much.
the code looks like
public void LoadFromDisk()
{
if (File.Exists(FileName))
{
/*
var jsonString = File.ReadAllText(FileName);
var keys = JsonConvert.DeserializeObject<ConfigFileStruct>(jsonString).Keys;
HostKeys = new ConcurrentDictionary<string, KnownHostValue>(keys);
*/
using (var stream = File.OpenRead(FileName))
{
var serializer = new DataContractJsonSerializer(typeof(ConfigFileStruct), serializationSettings);
var keys = (ConfigFileStruct)serializer.ReadObject(stream);
if (Equals(keys, null)) throw new Exception();
HostKeys = new ConcurrentDictionary<string, KnownHostValue>(keys.Keys);
}
}
}
private void WriteToDisk()
{
/*
var jsonString = JsonConvert.SerializeObject(new ConfigFileStruct()
{
Keys = HostKeys.ToDictionary(x => x.Key, x => x.Value)
},
Formatting.Indented
);
var d = Directory.CreateDirectory(Path.GetDirectoryName(FileName));
if (d.Exists)
{
File.WriteAllText(FileName, jsonString);
}
*/
var d = Directory.CreateDirectory(Path.GetDirectoryName(FileName));
if (d.Exists)
{
using (var stream = File.Open(FileName, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
stream, System.Text.Encoding.UTF8, true, true, " "))
{
var serializer = new DataContractJsonSerializer(typeof(ConfigFileStruct), serializationSettings);
serializer.WriteObject(writer,
new ConfigFileStruct()
{
Keys = HostKeys.ToDictionary(x => x.Key, x => x.Value)
}
);
writer.Flush();
}
}
}
}
@darkoperator what you think ?
Sounds great to me, it reduces dependencies
@darkoperator , btw, my MR doesn't remove Newtonsoft depencency itself
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net472" />
Thanks for the heads up, will remove it when I test this up comming weekendSent from my iPhoneOn Feb 6, 2023, at 4:42 PM, Max Kozlov @.***> wrote: @darkoperator , btw, my commit doesn't remove Newtonsoft depencency itself <package id="Newtonsoft.Json" version="13.0.2" targetFramework="net472" />
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>