Skip to content

Commit

Permalink
Merge pull request #498 from MVKozlov/DataContractJsonSerializer
Browse files Browse the repository at this point in the history
use DataContractJsonSerializer instead of Newtonsoft.Json
  • Loading branch information
darkoperator authored Feb 4, 2023
2 parents 154d93f + 02b171d commit f00564d
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@ pip-log.txt
# Mac crap
.DS_Store
Posh-SSH.zip

Source/**/bin/**
Source/**/obj/**
1 change: 0 additions & 1 deletion Source/PoshSSH/PoshSSH.Core/PoshSSH.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SshNet.Security.Cryptography">
<Version>1.3.0</Version>
</PackageReference>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"runtimeTarget": {
"name": ".NETStandard,Version=v2.0/",
"signature": "1c29894f4e98a18a46b4ef3d88fe6ab552e3c6d5"
},
"compilationOptions": {},
"targets": {
".NETStandard,Version=v2.0": {},
".NETStandard,Version=v2.0/": {
"PoshSSH/1.0.0": {
"dependencies": {
"NETStandard.Library": "2.0.3",
"PowerShellStandard.Library": "5.1.1",
"SSH.NET": "2020.0.2",
"SshNet.Security.Cryptography": "1.3.0"
},
"runtime": {
"PoshSSH.dll": {}
}
},
"Microsoft.NETCore.Platforms/1.1.0": {},
"NETStandard.Library/2.0.3": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0"
}
},
"PowerShellStandard.Library/5.1.1": {
"runtime": {
"lib/netstandard2.0/System.Management.Automation.dll": {
"assemblyVersion": "3.0.0.0",
"fileVersion": "5.1.1.0"
}
}
},
"SSH.NET/2020.0.2": {
"dependencies": {
"SshNet.Security.Cryptography": "1.3.0"
},
"runtime": {
"lib/netstandard2.0/Renci.SshNet.dll": {
"assemblyVersion": "2020.0.2.0",
"fileVersion": "2020.0.2.0"
}
}
},
"SshNet.Security.Cryptography/1.3.0": {
"runtime": {
"lib/netstandard2.0/SshNet.Security.Cryptography.dll": {
"assemblyVersion": "1.3.0.0",
"fileVersion": "1.3.0.0"
}
}
}
}
},
"libraries": {
"PoshSSH/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Microsoft.NETCore.Platforms/1.1.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==",
"path": "microsoft.netcore.platforms/1.1.0",
"hashPath": "microsoft.netcore.platforms.1.1.0.nupkg.sha512"
},
"NETStandard.Library/2.0.3": {
"type": "package",
"serviceable": true,
"sha512": "sha512-st47PosZSHrjECdjeIzZQbzivYBJFv6P2nv4cj2ypdI204DO+vZ7l5raGMiX4eXMJ53RfOIg+/s4DHVZ54Nu2A==",
"path": "netstandard.library/2.0.3",
"hashPath": "netstandard.library.2.0.3.nupkg.sha512"
},
"PowerShellStandard.Library/5.1.1": {
"type": "package",
"serviceable": true,
"sha512": "sha512-e31xJjG+Kjbv6YF3Yq6D4Dl3or8v7LrNF41k3CXrWozW6hR1zcOe5KYuZJaGSiAgLnwP8wcW+I3+IWEzMPZKXQ==",
"path": "powershellstandard.library/5.1.1",
"hashPath": "powershellstandard.library.5.1.1.nupkg.sha512"
},
"SSH.NET/2020.0.2": {
"type": "package",
"serviceable": true,
"sha512": "sha512-G0dNlTBAM00KZXv1wWVwgg26d9/METcM6qWBpNQwllzQmmbu+Zu+FS1L1X4fFgGdPu3e8k9mmTBu6SwtQ0614g==",
"path": "ssh.net/2020.0.2",
"hashPath": "ssh.net.2020.0.2.nupkg.sha512"
},
"SshNet.Security.Cryptography/1.3.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-5pBIXRjcSO/amY8WztpmNOhaaCNHY/B6CcYDI7FSTgqSyo/ZUojlLiKcsl+YGbxQuLX439qIkMfP0PHqxqJi/Q==",
"path": "sshnet.security.cryptography/1.3.0",
"hashPath": "sshnet.security.cryptography.1.3.0.nupkg.sha512"
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 25 additions & 11 deletions Source/PoshSSH/PoshSSH/Stores/JsonStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using System.Runtime.Serialization.Json;

namespace SSH.Stores
{
Expand All @@ -14,34 +14,48 @@ public class ConfigFileStruct
public class JsonStore : MemoryStore
{
private readonly string FileName;
private readonly DataContractJsonSerializerSettings serializationSettings;

public JsonStore(string fileName)
{
FileName = fileName;
serializationSettings = new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
}

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);
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();
}
}
}
}

Expand Down

0 comments on commit f00564d

Please sign in to comment.