Skip to content

Commit

Permalink
Get rid of Json.NET in favor of System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
manison committed Jan 10, 2020
1 parent 5d99a29 commit 5744a4e
Show file tree
Hide file tree
Showing 28 changed files with 355 additions and 410 deletions.
43 changes: 43 additions & 0 deletions Loxone.Client.Tests/DeserializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ----------------------------------------------------------------------
// <copyright file="DeserializationTests.cs">
// Copyright (c) The Loxone.NET Authors. All rights reserved.
// </copyright>
// <license>
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE.txt file.
// </license>
// ----------------------------------------------------------------------

namespace Loxone.Client.Tests
{
using System;
using Loxone.Client.Transport;
using Loxone.Client.Transport.Serialization.Responses;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class DeserializationTests
{
[TestMethod]
public void DeserializeResponseNumberCodeLowercase()
{
string s = @"{""LL"":{""control"":""jdev/sys/getkey2/user"",""code"":200,""value"":{""key"":""0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"",""salt"":""0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345""}}}";
var r = LXResponse<GetKey2>.Deserialize(s);
Assert.AreEqual(200, r.Code);
Assert.AreEqual("jdev/sys/getkey2/user", r.Control);
Assert.IsNotNull(r.Value);
}

[TestMethod]
public void DeserializeResponseStringCodeUppercase()
{
string s = @"{""LL"": { ""control"": ""dev/cfg/api"", ""value"": ""{'snr': 'AA:BB:CC:DD:EE:FF', 'version':'10.3.4.10'}"", ""Code"": ""200""}}";
var r = LXResponse<Api>.Deserialize(s);
Assert.AreEqual(200, r.Code);
Assert.AreEqual("dev/cfg/api", r.Control);
Assert.IsNotNull(r.Value);
Assert.AreEqual(SerialNumber.Parse("AA:BB:CC:DD:EE:FF"), r.Value.SerialNumber);
Assert.AreEqual(new Version(10, 3, 4, 10), r.Value.Version);
}
}
}
14 changes: 14 additions & 0 deletions Loxone.Client.Tests/Loxone.Client.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Loxone.Client\Loxone.Client.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Loxone.Client/Loxone.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Authors>manison</Authors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
<PackageReference Include="DerConverter" Version="3.0.0.82" />
</ItemGroup>
<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions Loxone.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ----------------------------------------------------------------------
// <copyright file="AsemblyInfo.cs">
// Copyright (c) The Loxone.NET Authors. All rights reserved.
// </copyright>
// <license>
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE.txt file.
// </license>
// ----------------------------------------------------------------------

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Loxone.Client.Tests")]
18 changes: 0 additions & 18 deletions Loxone.Client/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions Loxone.Client/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@
<data name="HexConverter_BadFormat" xml:space="preserve">
<value>Hex string format is invalid.</value>
</data>
<data name="LXDateTimeConverter_InvalidFormat" xml:space="preserve">
<value>Invalid date format.</value>
</data>
<data name="MiniserverCommandException_MessageFmt" xml:space="preserve">
<value>Miniserver command failed with status code {0}.</value>
</data>
Expand All @@ -135,9 +132,6 @@
<data name="MiniserverTransportException_Message" xml:space="preserve">
<value>Message received from Miniserver has invalid format.</value>
</data>
<data name="UuidConverter_UnexpectedValue" xml:space="preserve">
<value>Unexpected value when converting UUID.</value>
</data>
<data name="Uuid_ArgMustBeUuid" xml:space="preserve">
<value>Argument must be of type Uuid.</value>
</data>
Expand Down
29 changes: 5 additions & 24 deletions Loxone.Client/StructureFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,9 @@ public static async Task<StructureFile> LoadAsync(string fileName, CancellationT

public static async Task<StructureFile> LoadAsync(Stream stream, CancellationToken cancellationToken)
{
using (var reader = new StreamReader(stream))
{
return await LoadAsync(reader, cancellationToken).ConfigureAwait(false);
}
}

public static async Task<StructureFile> LoadAsync(TextReader reader, CancellationToken cancellationToken)
{
string s = await reader.ReadToEndAsync().ConfigureAwait(false);
return Parse(s);
var transportFile = await Transport.Serialization.SerializationHelper.DeserializeAsync<Transport.StructureFile>(
stream, cancellationToken).ConfigureAwait(false);
return new StructureFile(transportFile);
}

public async Task SaveAsync(string fileName, CancellationToken cancellationToken)
Expand All @@ -116,19 +109,7 @@ public async Task SaveAsync(string fileName, CancellationToken cancellationToken
}
}

public async Task SaveAsync(Stream stream, CancellationToken cancellationToken)
{
using (var writer = new StreamWriter(stream))
{
await SaveAsync(writer, cancellationToken).ConfigureAwait(false);
}
}

public Task SaveAsync(TextWriter writer, CancellationToken cancellationToken)
{
var serializer = Transport.Serialization.SerializationHelper.CreateSerializer();
serializer.Serialize(writer, _innerFile);
return Task.FromResult(0);
}
public Task SaveAsync(Stream stream, CancellationToken cancellationToken)
=> Transport.Serialization.SerializationHelper.SerializeAsync(stream, _innerFile, cancellationToken);
}
}
31 changes: 9 additions & 22 deletions Loxone.Client/Transport/Category.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// <copyright file="Category.cs">
// Copyright (c) The Loxone.NET Authors. All rights reserved.
// </copyright>
Expand All @@ -12,35 +12,22 @@ namespace Loxone.Client.Transport
{
using System.Drawing;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

internal sealed class Category
{
// Suppress 'Field is never assigned to, and will always have its
// default value' warning.
// Justification: Fields are set during deserialization.
#pragma warning disable CS0649

[JsonProperty("uuid")]
public Uuid Uuid;
public Uuid Uuid { get; set; }

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

[JsonProperty("isFavorite")]
public bool IsFavorite;
public bool IsFavorite { get; set; }

[JsonProperty("defaultRating")]
public int DefaultRating;
public int DefaultRating { get; set; }

// There should be support for Color type in .NET Standard 1.7
[JsonProperty("color")]
public Color Color;
public Color Color { get; set; }

[JsonExtensionData]
public IDictionary<string, JToken> ExtensionData;

#pragma warning restore CS0649
public IDictionary<string, JsonElement> ExtensionData { get; set; }
}
}
42 changes: 15 additions & 27 deletions Loxone.Client/Transport/Control.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// <copyright file="Control.cs">
// Copyright (c) The Loxone.NET Authors. All rights reserved.
// </copyright>
Expand All @@ -11,43 +11,31 @@
namespace Loxone.Client.Transport
{
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

internal sealed class Control
{
// Suppress 'Field is never assigned to, and will always have its
// default value' warning.
// Justification: Fields are set during deserialization.
#pragma warning disable CS0649

[JsonProperty("uuidAction")]
public Uuid Uuid;
[JsonPropertyName("uuidAction")]
public Uuid Uuid { get; set; }

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

[JsonProperty("type")]
public string ControlType;
[JsonPropertyName("type")]
public string ControlType { get; set; }

[JsonProperty("isFavorite")]
public bool IsFavorite;
public bool IsFavorite { get; set; }

[JsonProperty("isSecured")]
public bool IsSecured;
public bool IsSecured { get; set; }

[JsonProperty("defaultRating")]
public int DefaultRating;
public int DefaultRating { get; set; }

[JsonProperty("room")]
public Uuid? Room;
public Uuid? Room { get; set; }

[JsonProperty("cat")]
public Uuid? Category;
[JsonPropertyName("cat")]
public Uuid? Category { get; set; }

[JsonExtensionData]
public IDictionary<string, JToken> ExtensionData;

#pragma warning restore CS0649
public IDictionary<string, JsonElement> ExtensionData { get; set; }
}
}
1 change: 1 addition & 0 deletions Loxone.Client/Transport/Encryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public string DecodeCommand(string command)

byte[] encryptedResponse = Convert.FromBase64String(command);
string decrypted = AesDecrypt(encryptedResponse);
decrypted = decrypted.TrimEnd('\0'); // Trim trailing zero padding.
return decrypted;
}

Expand Down
24 changes: 7 additions & 17 deletions Loxone.Client/Transport/LXResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// <copyright file="LXResponse.cs">
// Copyright (c) The Loxone.NET Authors. All rights reserved.
// </copyright>
Expand All @@ -10,31 +10,21 @@

namespace Loxone.Client.Transport
{
using Newtonsoft.Json;
using System.Text.Json.Serialization;

internal sealed class LXResponse<TValue>
{
// Suppress 'Field is never assigned to, and will always have its
// default value' warning.
// Justification: Fields are set during deserialization.
#pragma warning disable CS0649

private struct Root
{
[JsonProperty("LL")]
public LXResponse<TValue> Value;
[JsonPropertyName("LL")]
public LXResponse<TValue> Value { get; set; }
}

[JsonProperty("control")]
public string Control;

[JsonProperty("Code")]
public int Code;
public string Control { get; set; }

[JsonProperty("value")]
public TValue Value;
public int Code { get; set; }

#pragma warning restore CS0649
public TValue Value { get; set; }

public static LXResponse<TValue> Deserialize(string s)
{
Expand Down
Loading

0 comments on commit 5744a4e

Please sign in to comment.