Skip to content

Commit

Permalink
Merge f3a6058 into af1cb6f
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real authored Dec 23, 2020
2 parents af1cb6f + f3a6058 commit ff0cb4d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NetDaemon.Infrastructure.Extensions
{
internal static class JsonElementExtensions
{
public static object? ToDynamicValue(this JsonElement elem)
public static object? ConvertToDynamicValue(this JsonElement elem)
{
switch (elem.ValueKind)
{
Expand All @@ -31,7 +31,7 @@ internal static class JsonElementExtensions
var list = new List<object?>();
foreach (var val in elem.EnumerateArray())
{
list.Add(val.ToDynamicValue());
list.Add(val.ConvertToDynamicValue());
}
return (IEnumerable<object?>)list;

Expand All @@ -40,7 +40,7 @@ internal static class JsonElementExtensions

foreach (var prop in elem.EnumerateObject())
{
obj[prop.Name] = prop.Value.ToDynamicValue();
obj[prop.Name] = prop.Value.ConvertToDynamicValue();
}
return (IDictionary<string, object?>)obj;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Daemon/NetDaemon.Daemon/Mapping/EntityStateMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text.Json;
using JoySoftware.HomeAssistant.Client;
using NetDaemon.Infrastructure.Extensions;
using NetDaemon.Common;

namespace NetDaemon.Mapping
Expand Down Expand Up @@ -45,7 +46,7 @@ private static void MapAttributes(EntityState entityState, HassState hassState)
{
if (value is JsonElement elem)
{
var dynValue = elem.ToDynamicValue();
var dynValue = elem.ConvertToDynamicValue();

if (dynValue != null)
dict[key] = dynValue;
Expand Down
14 changes: 7 additions & 7 deletions tests/NetDaemon.Daemon.Tests/Daemon/ExtensionMethodsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void JsonElementToDynamicValueWhenStringShouldReturnString()
var prop = doc.RootElement.GetProperty("str");

// ACT
var obj = JsonElementExtensions.ToDynamicValue(prop);
var obj = JsonElementExtensions.ConvertToDynamicValue(prop);

// ASSERT
Assert.IsType<string>(obj);
Expand All @@ -34,7 +34,7 @@ public void JsonElementToDynamicValueWhenTrueShouldReturnBoolTrue()
var prop = doc.RootElement.GetProperty("bool");

// ACT
var obj = JsonElementExtensions.ToDynamicValue(prop);
var obj = JsonElementExtensions.ConvertToDynamicValue(prop);

// ASSERT
Assert.IsType<bool>(obj);
Expand All @@ -49,7 +49,7 @@ public void JsonElementToDynamicValueWhenFalseShouldReturnBoolFalse()
var prop = doc.RootElement.GetProperty("bool");

// ACT
var obj = JsonElementExtensions.ToDynamicValue(prop);
var obj = JsonElementExtensions.ConvertToDynamicValue(prop);

// ASSERT
Assert.IsType<bool>(obj);
Expand All @@ -64,7 +64,7 @@ public void JsonElementToDynamicValueWhenIntegerShouldReturnInteger()
var prop = doc.RootElement.GetProperty("int");
long expectedValue = 10;
// ACT
var obj = JsonElementExtensions.ToDynamicValue(prop);
var obj = JsonElementExtensions.ConvertToDynamicValue(prop);

// ASSERT
Assert.IsType<long>(obj);
Expand All @@ -79,7 +79,7 @@ public void JsonElementToDynamicValueWhenDoubleShouldReturnDouble()
var prop = doc.RootElement.GetProperty("int");
double expectedValue = 10.5;
// ACT
var obj = JsonElementExtensions.ToDynamicValue(prop);
var obj = JsonElementExtensions.ConvertToDynamicValue(prop);

// ASSERT
Assert.IsType<double>(obj);
Expand All @@ -94,7 +94,7 @@ public void JsonElementToDynamicValueWhenArrayShouldReturnArray()
var prop = doc.RootElement.GetProperty("array");

// ACT
var obj = JsonElementExtensions.ToDynamicValue(prop);
var obj = JsonElementExtensions.ConvertToDynamicValue(prop);
var arr = obj as IEnumerable<object?>;

// ASSERT
Expand All @@ -109,7 +109,7 @@ public void JsonElementToDynamicValueWhenObjectShouldReturnObject()
var doc = JsonDocument.Parse("{\"str\": \"string\"}");

// ACT
var obj = JsonElementExtensions.ToDynamicValue(doc.RootElement) as IDictionary<string, object?>;
var obj = JsonElementExtensions.ConvertToDynamicValue(doc.RootElement) as IDictionary<string, object?>;

// ASSERT
Assert.Equal("string", obj?["str"]);
Expand Down

0 comments on commit ff0cb4d

Please sign in to comment.