Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixes #71 #74

Merged
merged 2 commits into from
Jun 27, 2023
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
@@ -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<string, object?>
{
{ "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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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;
}
Expand All @@ -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"));
}

Expand All @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public IEnumerable<string> 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:
Expand All @@ -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 _);
}
}
}