Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Remove Newtonsoft.Json dependency

Open MVKozlov opened this issue 2 years ago • 3 comments

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 ?

MVKozlov avatar Nov 15 '22 10:11 MVKozlov

Sounds great to me, it reduces dependencies

darkoperator avatar Nov 15 '22 12:11 darkoperator

@darkoperator , btw, my MR doesn't remove Newtonsoft depencency itself

  <package id="Newtonsoft.Json" version="13.0.2" targetFramework="net472" />

MVKozlov avatar Feb 06 '23 20:02 MVKozlov

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: @.***>

darkoperator avatar Feb 06 '23 21:02 darkoperator