Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<HassMessage>();

msg!.Id
.Should()
.Be(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class HassEventExtensions
{
var jsonElement = hassEvent.DataElement ??
throw new NullReferenceException("DataElement cannot be empty");
return jsonElement.ToObject<HassStateChangedEventData>();
return jsonElement.Deserialize<HassStateChangedEventData>();
}

/// <summary>
Expand All @@ -26,6 +26,6 @@ public static class HassEventExtensions
{
var jsonElement = hassEvent.DataElement ??
throw new NullReferenceException("DataElement cannot be empty");
return jsonElement.ToObject<HassServiceEventData>();
return jsonElement.Deserialize<HassServiceEventData>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public record HassState

public IReadOnlyDictionary<string, object>? Attributes
{
get => AttributesJson?.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
get => AttributesJson?.Deserialize<Dictionary<string, object>>() ?? new Dictionary<string, object>();
init => AttributesJson = value.ToJsonElement();
}

Expand All @@ -19,6 +19,6 @@ public IReadOnlyDictionary<string, object>? Attributes

public T? AttributesAs<T>()
{
return AttributesJson.HasValue ? AttributesJson.Value.ToObject<T>() : default;
return AttributesJson.HasValue ? AttributesJson.Value.Deserialize<T>() : default;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@

internal static class JsonExtensions
{
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions? options = null)
{
var bufferWriter = new ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
{
element.WriteTo(writer);
}

return JsonSerializer.Deserialize<T?>(bufferWriter.WrittenSpan, options) ?? default!;
}

public static JsonElement? ToJsonElement<T>(this T source, JsonSerializerOptions? options = null)
{
if (source == null) return null;
var json = JsonSerializer.Serialize<T>(source, options);
return JsonDocument.Parse(json).RootElement;
return JsonSerializer.SerializeToElement(source, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task SendCommandAsync<T>(T command, CancellationToken cancelToken)
{
var result = await SendCommandAndReturnResponseRawAsync(command, cancelToken).ConfigureAwait(false);

return result is not null ? result.Value.ToObject<TResult>() : default;
return result is not null ? result.Value.Deserialize<TResult>() : default;
}

public async Task<JsonElement?> SendCommandAndReturnResponseRawAsync<T>(T command, CancellationToken cancelToken)
Expand Down
4 changes: 2 additions & 2 deletions src/HassModel/NetDeamon.HassModel/Entities/EntityState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public record EntityState
/// <summary>
/// The attributes
/// </summary>
public virtual object? Attributes => AttributesJson?.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
public virtual object? Attributes => AttributesJson?.Deserialize<Dictionary<string, object>>() ?? new Dictionary<string, object>();

/// <summary>Last changed, when state changed from and to different values</summary>
public DateTime? LastChanged { get; init; }
Expand Down Expand Up @@ -46,7 +46,7 @@ public record EntityState<TAttributes> : EntityState
/// <param name="source"></param>
public EntityState(EntityState source) : base(source)
{
_attributesLazy = new (() => AttributesJson?.ToObject<TAttributes>() ?? default);
_attributesLazy = new (() => AttributesJson?.Deserialize<TAttributes>() ?? default);
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/HassModel/NetDeamon.HassModel/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public record Event<TData> : Event
/// <summary>Copy constructor from Base type</summary>
public Event(Event source) : base(source)
{
_lazyData = new Lazy<TData?>(() => DataElement?.ToObject<TData>());
_lazyData = new Lazy<TData?>(() => DataElement?.Deserialize<TData>());
}

private Lazy<TData?> _lazyData;
Expand Down
12 changes: 0 additions & 12 deletions src/HassModel/NetDeamon.HassModel/Internal/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,11 @@ namespace NetDaemon.HassModel.Internal;
/// </summary>
internal static class NetDaemonExtensions
{
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions? options = null)
{
var bufferWriter = new ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
{
element.WriteTo(writer);
}

return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options) ?? default!;
}

public static (string? Left, string Right) SplitAtDot(this string id)
{
var firstDot = id.IndexOf('.', System.StringComparison.InvariantCulture);
if (firstDot == -1) return (null, id);

return (id[.. firstDot ], id[ (firstDot + 1) .. ]);
}

}