diff --git a/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonDictionaryDataItemTest.cs b/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonDictionaryDataItemTest.cs new file mode 100644 index 0000000..3d539c4 --- /dev/null +++ b/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonDictionaryDataItemTest.cs @@ -0,0 +1,29 @@ +using Cosmos.DataTransfer.Interfaces; +using Microsoft.Extensions.Logging.Abstractions; +using Newtonsoft.Json; + +namespace Cosmos.DataTransfer.JsonExtension.UnitTests +{ + [TestClass] + public class JsonDictionaryDataItemTest + { + [TestMethod] + public void ConvertNumber() + { + var data = new Dictionary + { + { "integer", 1 }, + { "long", int.MaxValue * 2L }, + { "doubleAsNumber1", 1.0 }, + { "doubleAsNumber2", 1.1 }, + }; + + var output = new JsonDictionaryDataItem(data); + + Assert.IsTrue(output.GetValue("integer")?.GetType() == typeof(int)); + Assert.IsTrue(output.GetValue("long")?.GetType() == typeof(long)); + Assert.IsTrue(output.GetValue("doubleAsNumber1")?.GetType() == typeof(double)); + Assert.IsTrue(output.GetValue("doubleAsNumber2")?.GetType() == typeof(double)); + } + } +} \ No newline at end of file diff --git a/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonSourceTests.cs b/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonSourceTests.cs index b6f4022..60d3e32 100644 --- a/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonSourceTests.cs +++ b/Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonSourceTests.cs @@ -61,7 +61,7 @@ public async Task ReadAsync_WithSingleObjectFile_ReadsValues() { counter++; CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray()); - Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double)); + Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int)); Assert.IsNotNull(dataItem.GetValue("name")); } @@ -85,9 +85,9 @@ public async Task ReadAsync_WithSingleObjectsFolder_ReadsValuesInOrder() counter++; CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray()); object? value = dataItem.GetValue("id"); - Assert.IsInstanceOfType(value, typeof(double)); + Assert.IsInstanceOfType(value, typeof(int)); Assert.IsNotNull(dataItem.GetValue("name")); - var current = (double?)value ?? int.MaxValue; + var current = (int?)value ?? int.MaxValue; Assert.IsTrue(current > lastId); lastId = current; } @@ -109,7 +109,7 @@ public async Task ReadAsync_WithArraysFolder_ReadsValues() { counter++; CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray()); - Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double)); + Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int)); Assert.IsNotNull(dataItem.GetValue("name")); } @@ -130,7 +130,7 @@ public async Task ReadAsync_WithMixedObjectsFolder_ReadsValues() { counter++; CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray()); - Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double)); + Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int)); Assert.IsNotNull(dataItem.GetValue("name")); } diff --git a/Extensions/Json/Cosmos.DataTransfer.JsonExtension/JsonDictionaryDataItem.cs b/Extensions/Json/Cosmos.DataTransfer.JsonExtension/JsonDictionaryDataItem.cs index d95e83e..e4fef0d 100644 --- a/Extensions/Json/Cosmos.DataTransfer.JsonExtension/JsonDictionaryDataItem.cs +++ b/Extensions/Json/Cosmos.DataTransfer.JsonExtension/JsonDictionaryDataItem.cs @@ -41,7 +41,13 @@ public IEnumerable GetFieldNames() case JsonValueKind.String: return element.GetString(); case JsonValueKind.Number: + { + if (IsInteger(element.GetRawText())) + return element.GetInt32(); + if (IsLong(element.GetRawText())) + return element.GetInt64(); return element.GetDouble(); + } case JsonValueKind.True: return true; case JsonValueKind.False: @@ -58,5 +64,15 @@ private static JsonDictionaryDataItem GetChildObject(JsonElement element) { return new JsonDictionaryDataItem(element.EnumerateObject().ToDictionary(p => p.Name, p => (object?)p.Value)); } + + private static bool IsInteger(string? value) + { + return int.TryParse(value, out _); + } + + private static bool IsLong(string? value) + { + return long.TryParse(value, out _); + } } } \ No newline at end of file