Skip to content

Commit 9068e5a

Browse files
committed
Improving date output for JSON sink
1 parent 9339d74 commit 9068e5a

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

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

+77
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,88 @@ public async Task WriteAsync_WithFlatObjects_WritesToValidFile()
4444
Assert.IsTrue(outputData.Any(o => o.Id == 2 && o.Name == "Two"));
4545
Assert.IsTrue(outputData.Any(o => o.Id == 3 && o.Name == "Three"));
4646
}
47+
48+
[TestMethod]
49+
public async Task WriteAsync_WithSourceDates_PreservesDateFormats()
50+
{
51+
var sink = new JsonDataSinkExtension();
52+
53+
var now = DateTime.UtcNow;
54+
var randomTime = DateTime.UtcNow.AddMinutes(Random.Shared.NextDouble() * 10000);
55+
var data = new List<DictionaryDataItem>
56+
{
57+
new(new Dictionary<string, object?>
58+
{
59+
{ "Id", 1 },
60+
{ "Created", now },
61+
}),
62+
new(new Dictionary<string, object?>
63+
{
64+
{ "Id", 2 },
65+
{ "Created", DateTime.UnixEpoch },
66+
}),
67+
new(new Dictionary<string, object?>
68+
{
69+
{ "Id", 3 },
70+
{ "Created", randomTime },
71+
}),
72+
};
73+
string outputFile = $"{now:yy-MM-dd}_DateOutput.json";
74+
var config = TestHelpers.CreateConfig(new Dictionary<string, string>
75+
{
76+
{ "FilePath", outputFile }
77+
});
78+
79+
await sink.WriteAsync(data.ToAsyncEnumerable(), config, new JsonDataSourceExtension(), NullLogger.Instance);
80+
81+
string json = await File.ReadAllTextAsync(outputFile);
82+
var outputData = JsonConvert.DeserializeObject<List<TestDataObject>>(json);
83+
84+
Assert.IsTrue(outputData.Any(o => o.Id == 1 && o.Created == now));
85+
Assert.IsTrue(outputData.Any(o => o.Id == 2 && o.Created == DateTime.UnixEpoch));
86+
Assert.IsTrue(outputData.Any(o => o.Id == 3 && o.Created == randomTime));
87+
}
88+
89+
90+
[TestMethod]
91+
public async Task WriteAsync_WithDateArray_PreservesDateFormats()
92+
{
93+
var sink = new JsonDataSinkExtension();
94+
95+
var now = DateTime.UtcNow;
96+
var randomTime = DateTime.UtcNow.AddMinutes(Random.Shared.NextDouble() * 10000);
97+
var data = new List<DictionaryDataItem>
98+
{
99+
new(new Dictionary<string, object?>
100+
{
101+
{ "Id", 1 },
102+
{ "Dates", new[] { now, randomTime, DateTime.UnixEpoch } },
103+
})
104+
};
105+
106+
string outputFile = $"{now:yy-MM-dd}_DateArrayOutput.json";
107+
var config = TestHelpers.CreateConfig(new Dictionary<string, string>
108+
{
109+
{ "FilePath", outputFile }
110+
});
111+
112+
await sink.WriteAsync(data.ToAsyncEnumerable(), config, new JsonDataSourceExtension(), NullLogger.Instance);
113+
114+
string json = await File.ReadAllTextAsync(outputFile);
115+
var outputData = JsonConvert.DeserializeObject<List<TestDataObject>>(json);
116+
117+
Assert.AreEqual(now, outputData.Single().Dates.ElementAt(0));
118+
Assert.AreEqual(randomTime, outputData.Single().Dates.ElementAt(1));
119+
Assert.AreEqual(DateTime.UnixEpoch, outputData.Single().Dates.ElementAt(2));
120+
}
121+
47122
}
48123

49124
public class TestDataObject
50125
{
51126
public int Id { get; set; }
52127
public string? Name { get; set; }
128+
public DateTime? Created { get; set; }
129+
public List<DateTime>? Dates { get; set; }
53130
}
54131
}

Interfaces/Cosmos.DataTransfer.Interfaces/DataItemJsonConverter.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Collections;
2+
using System.Text;
23
using System.Text.Json;
34

45
namespace Cosmos.DataTransfer.Interfaces;
@@ -52,7 +53,7 @@ private static void WriteFieldValue(Utf8JsonWriter writer, string fieldName, obj
5253
{
5354
WriteDataItem(writer, child, includeNullFields, fieldName);
5455
}
55-
else if (fieldValue is IEnumerable<object> children)
56+
else if (fieldValue is not string && fieldValue is IEnumerable children)
5657
{
5758
writer.WriteStartArray(fieldName);
5859
foreach (object arrayItem in children)
@@ -69,6 +70,10 @@ private static void WriteFieldValue(Utf8JsonWriter writer, string fieldName, obj
6970
{
7071
writer.WriteBooleanValue(boolean);
7172
}
73+
else if (arrayItem is DateTime date)
74+
{
75+
writer.WriteStringValue(date.ToString("O"));
76+
}
7277
else
7378
{
7479
writer.WriteStringValue(arrayItem.ToString());
@@ -84,6 +89,10 @@ private static void WriteFieldValue(Utf8JsonWriter writer, string fieldName, obj
8489
{
8590
writer.WriteBoolean(fieldName, boolean);
8691
}
92+
else if (fieldValue is DateTime date)
93+
{
94+
writer.WriteString(fieldName, date.ToString("O"));
95+
}
8796
else
8897
{
8998
writer.WriteString(fieldName, fieldValue.ToString());

0 commit comments

Comments
 (0)