Skip to content

Commit ee76585

Browse files
authored
Merge pull request #74 from MarioGit1/convert_json_number
Fixes #71 Updates JSON parsing to recognize specific numeric data types
2 parents c8ad5a8 + 83e58b0 commit ee76585

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Cosmos.DataTransfer.Interfaces;
2+
using Microsoft.Extensions.Logging.Abstractions;
3+
using Newtonsoft.Json;
4+
5+
namespace Cosmos.DataTransfer.JsonExtension.UnitTests
6+
{
7+
[TestClass]
8+
public class JsonDictionaryDataItemTest
9+
{
10+
[TestMethod]
11+
public void ConvertNumber()
12+
{
13+
var data = new Dictionary<string, object?>
14+
{
15+
{ "integer", 1 },
16+
{ "long", int.MaxValue * 2L },
17+
{ "doubleAsNumber1", 1.0 },
18+
{ "doubleAsNumber2", 1.1 },
19+
};
20+
21+
var output = new JsonDictionaryDataItem(data);
22+
23+
Assert.IsTrue(output.GetValue("integer")?.GetType() == typeof(int));
24+
Assert.IsTrue(output.GetValue("long")?.GetType() == typeof(long));
25+
Assert.IsTrue(output.GetValue("doubleAsNumber1")?.GetType() == typeof(double));
26+
Assert.IsTrue(output.GetValue("doubleAsNumber2")?.GetType() == typeof(double));
27+
}
28+
}
29+
}

Extensions/Json/Cosmos.DataTransfer.JsonExtension.UnitTests/JsonSourceTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task ReadAsync_WithSingleObjectFile_ReadsValues()
6161
{
6262
counter++;
6363
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
64-
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double));
64+
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int));
6565
Assert.IsNotNull(dataItem.GetValue("name"));
6666
}
6767

@@ -85,9 +85,9 @@ public async Task ReadAsync_WithSingleObjectsFolder_ReadsValuesInOrder()
8585
counter++;
8686
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
8787
object? value = dataItem.GetValue("id");
88-
Assert.IsInstanceOfType(value, typeof(double));
88+
Assert.IsInstanceOfType(value, typeof(int));
8989
Assert.IsNotNull(dataItem.GetValue("name"));
90-
var current = (double?)value ?? int.MaxValue;
90+
var current = (int?)value ?? int.MaxValue;
9191
Assert.IsTrue(current > lastId);
9292
lastId = current;
9393
}
@@ -109,7 +109,7 @@ public async Task ReadAsync_WithArraysFolder_ReadsValues()
109109
{
110110
counter++;
111111
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
112-
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double));
112+
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int));
113113
Assert.IsNotNull(dataItem.GetValue("name"));
114114
}
115115

@@ -130,7 +130,7 @@ public async Task ReadAsync_WithMixedObjectsFolder_ReadsValues()
130130
{
131131
counter++;
132132
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
133-
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double));
133+
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int));
134134
Assert.IsNotNull(dataItem.GetValue("name"));
135135
}
136136

Extensions/Json/Cosmos.DataTransfer.JsonExtension/JsonDictionaryDataItem.cs

+16
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public IEnumerable<string> GetFieldNames()
4141
case JsonValueKind.String:
4242
return element.GetString();
4343
case JsonValueKind.Number:
44+
{
45+
if (IsInteger(element.GetRawText()))
46+
return element.GetInt32();
47+
if (IsLong(element.GetRawText()))
48+
return element.GetInt64();
4449
return element.GetDouble();
50+
}
4551
case JsonValueKind.True:
4652
return true;
4753
case JsonValueKind.False:
@@ -58,5 +64,15 @@ private static JsonDictionaryDataItem GetChildObject(JsonElement element)
5864
{
5965
return new JsonDictionaryDataItem(element.EnumerateObject().ToDictionary(p => p.Name, p => (object?)p.Value));
6066
}
67+
68+
private static bool IsInteger(string? value)
69+
{
70+
return int.TryParse(value, out _);
71+
}
72+
73+
private static bool IsLong(string? value)
74+
{
75+
return long.TryParse(value, out _);
76+
}
6177
}
6278
}

0 commit comments

Comments
 (0)