From 1852a1254c3d81810c457447ea6ebdc0ca1d7260 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 10 Sep 2022 23:09:18 -0700 Subject: [PATCH] Use builtin SerializeToElement/Deserialize over current json extensions --- .../JsonElementExtensionTest.cs | 20 ------------------- .../Extensions/HassEventExtensions.cs | 4 ++-- .../Common/HomeAssistant/Model/HassState.cs | 4 ++-- .../Internal/Extensions/JsonExtensions.cs | 14 +------------ .../Internal/HomeAssistantConnection.cs | 2 +- .../Entities/EntityState.cs | 4 ++-- src/HassModel/NetDeamon.HassModel/Event.cs | 2 +- .../Internal/ExtensionMethods.cs | 12 ----------- 8 files changed, 9 insertions(+), 53 deletions(-) diff --git a/src/Client/NetDaemon.HassClient.Tests/ExtensionsTest/JsonElementExtensionTest.cs b/src/Client/NetDaemon.HassClient.Tests/ExtensionsTest/JsonElementExtensionTest.cs index 11f8b117d..9141691a5 100644 --- a/src/Client/NetDaemon.HassClient.Tests/ExtensionsTest/JsonElementExtensionTest.cs +++ b/src/Client/NetDaemon.HassClient.Tests/ExtensionsTest/JsonElementExtensionTest.cs @@ -13,24 +13,4 @@ public void TestToJsonElementShouldReturnCorrectElement() .Should() .Be("get_services"); } - - [Fact] - public void TestToObjectFromElementShouldReturnCorrectObject() - { - var jsonDoc = JsonDocument.Parse( - @" - { - ""id"": 3, - ""type"": ""result"", - ""success"": true, - ""result"": null - } - " - ); - var msg = jsonDoc.RootElement.ToObject(); - - msg!.Id - .Should() - .Be(3); - } } \ No newline at end of file diff --git a/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Extensions/HassEventExtensions.cs b/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Extensions/HassEventExtensions.cs index f8e4073fc..60962a46b 100644 --- a/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Extensions/HassEventExtensions.cs +++ b/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Extensions/HassEventExtensions.cs @@ -14,7 +14,7 @@ public static class HassEventExtensions { var jsonElement = hassEvent.DataElement ?? throw new NullReferenceException("DataElement cannot be empty"); - return jsonElement.ToObject(); + return jsonElement.Deserialize(); } /// @@ -26,6 +26,6 @@ public static class HassEventExtensions { var jsonElement = hassEvent.DataElement ?? throw new NullReferenceException("DataElement cannot be empty"); - return jsonElement.ToObject(); + return jsonElement.Deserialize(); } } \ No newline at end of file diff --git a/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Model/HassState.cs b/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Model/HassState.cs index 6c6cf6cc8..d3af18737 100644 --- a/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Model/HassState.cs +++ b/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Model/HassState.cs @@ -6,7 +6,7 @@ public record HassState public IReadOnlyDictionary? Attributes { - get => AttributesJson?.ToObject>() ?? new Dictionary(); + get => AttributesJson?.Deserialize>() ?? new Dictionary(); init => AttributesJson = value.ToJsonElement(); } @@ -19,6 +19,6 @@ public IReadOnlyDictionary? Attributes public T? AttributesAs() { - return AttributesJson.HasValue ? AttributesJson.Value.ToObject() : default; + return AttributesJson.HasValue ? AttributesJson.Value.Deserialize() : default; } } \ No newline at end of file diff --git a/src/Client/NetDaemon.HassClient/Internal/Extensions/JsonExtensions.cs b/src/Client/NetDaemon.HassClient/Internal/Extensions/JsonExtensions.cs index ca213418a..897660130 100644 --- a/src/Client/NetDaemon.HassClient/Internal/Extensions/JsonExtensions.cs +++ b/src/Client/NetDaemon.HassClient/Internal/Extensions/JsonExtensions.cs @@ -2,21 +2,9 @@ internal static class JsonExtensions { - public static T ToObject(this JsonElement element, JsonSerializerOptions? options = null) - { - var bufferWriter = new ArrayBufferWriter(); - using (var writer = new Utf8JsonWriter(bufferWriter)) - { - element.WriteTo(writer); - } - - return JsonSerializer.Deserialize(bufferWriter.WrittenSpan, options) ?? default!; - } - public static JsonElement? ToJsonElement(this T source, JsonSerializerOptions? options = null) { if (source == null) return null; - var json = JsonSerializer.Serialize(source, options); - return JsonDocument.Parse(json).RootElement; + return JsonSerializer.SerializeToElement(source, options); } } \ No newline at end of file diff --git a/src/Client/NetDaemon.HassClient/Internal/HomeAssistantConnection.cs b/src/Client/NetDaemon.HassClient/Internal/HomeAssistantConnection.cs index ccad40fd5..c63b7f04e 100644 --- a/src/Client/NetDaemon.HassClient/Internal/HomeAssistantConnection.cs +++ b/src/Client/NetDaemon.HassClient/Internal/HomeAssistantConnection.cs @@ -99,7 +99,7 @@ public async Task SendCommandAsync(T command, CancellationToken cancelToken) { var result = await SendCommandAndReturnResponseRawAsync(command, cancelToken).ConfigureAwait(false); - return result is not null ? result.Value.ToObject() : default; + return result is not null ? result.Value.Deserialize() : default; } public async Task SendCommandAndReturnResponseRawAsync(T command, CancellationToken cancelToken) diff --git a/src/HassModel/NetDeamon.HassModel/Entities/EntityState.cs b/src/HassModel/NetDeamon.HassModel/Entities/EntityState.cs index 45710cb16..efa82cf2d 100644 --- a/src/HassModel/NetDeamon.HassModel/Entities/EntityState.cs +++ b/src/HassModel/NetDeamon.HassModel/Entities/EntityState.cs @@ -16,7 +16,7 @@ public record EntityState /// /// The attributes /// - public virtual object? Attributes => AttributesJson?.ToObject>() ?? new Dictionary(); + public virtual object? Attributes => AttributesJson?.Deserialize>() ?? new Dictionary(); /// Last changed, when state changed from and to different values public DateTime? LastChanged { get; init; } @@ -46,7 +46,7 @@ public record EntityState : EntityState /// public EntityState(EntityState source) : base(source) { - _attributesLazy = new (() => AttributesJson?.ToObject() ?? default); + _attributesLazy = new (() => AttributesJson?.Deserialize() ?? default); } /// diff --git a/src/HassModel/NetDeamon.HassModel/Event.cs b/src/HassModel/NetDeamon.HassModel/Event.cs index 13af79399..f4a64f906 100644 --- a/src/HassModel/NetDeamon.HassModel/Event.cs +++ b/src/HassModel/NetDeamon.HassModel/Event.cs @@ -40,7 +40,7 @@ public record Event : Event /// Copy constructor from Base type public Event(Event source) : base(source) { - _lazyData = new Lazy(() => DataElement?.ToObject()); + _lazyData = new Lazy(() => DataElement?.Deserialize()); } private Lazy _lazyData; diff --git a/src/HassModel/NetDeamon.HassModel/Internal/ExtensionMethods.cs b/src/HassModel/NetDeamon.HassModel/Internal/ExtensionMethods.cs index 7a5b4ff28..b12602168 100644 --- a/src/HassModel/NetDeamon.HassModel/Internal/ExtensionMethods.cs +++ b/src/HassModel/NetDeamon.HassModel/Internal/ExtensionMethods.cs @@ -5,17 +5,6 @@ namespace NetDaemon.HassModel.Internal; /// internal static class NetDaemonExtensions { - public static T ToObject(this JsonElement element, JsonSerializerOptions? options = null) - { - var bufferWriter = new ArrayBufferWriter(); - using (var writer = new Utf8JsonWriter(bufferWriter)) - { - element.WriteTo(writer); - } - - return JsonSerializer.Deserialize(bufferWriter.WrittenSpan, options) ?? default!; - } - public static (string? Left, string Right) SplitAtDot(this string id) { var firstDot = id.IndexOf('.', System.StringComparison.InvariantCulture); @@ -23,5 +12,4 @@ public static (string? Left, string Right) SplitAtDot(this string id) return (id[.. firstDot ], id[ (firstDot + 1) .. ]); } - } \ No newline at end of file