Skip to content

Commit b54ec18

Browse files
authored
PBE-521 :: Fixed UTC convertion, added UTC serializer. (#109)
* Fixed UTC convertion, added UTC serializer. * Updated CODEOWNERS * added StreamJsonConverterUTC test
1 parent 06cc0e8 commit b54ec18

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @ffenix113 @xernobyl @yaziine
1+
* @xernobyl @JimmyPettersson85 @itsmeadi

src/Utils/StreamJsonConverter.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ public static class StreamJsonConverter
1313
NamingStrategy = new SnakeCaseNamingStrategy(), // this handles ForeignId => foreign_id etc. conversion for us
1414
},
1515
NullValueHandling = NullValueHandling.Ignore,
16+
DateTimeZoneHandling = DateTimeZoneHandling.Utc // always convert time to UTC
17+
};
18+
19+
public static JsonSerializer Serializer { get; } = JsonSerializer.Create(Settings);
20+
public static string SerializeObject(object obj) => JsonConvert.SerializeObject(obj, Settings);
21+
public static T DeserializeObject<T>(string json) => JsonConvert.DeserializeObject<T>(json, Settings);
22+
}
23+
24+
public static class StreamJsonConverterUTC
25+
{
26+
private static JsonSerializerSettings Settings = new JsonSerializerSettings
27+
{
28+
DateFormatString = "yyyy-MM-dd'T'HH:mm:ssZ",
29+
ContractResolver = new DefaultContractResolver
30+
{
31+
NamingStrategy = new SnakeCaseNamingStrategy(), // this handles ForeignId => foreign_id etc. conversion for us
32+
},
33+
NullValueHandling = NullValueHandling.Ignore,
34+
DateTimeZoneHandling = DateTimeZoneHandling.Utc // always convert time to UTC
1635
};
1736

1837
public static JsonSerializer Serializer { get; } = JsonSerializer.Create(Settings);

tests/UtilsTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,15 @@ public async Task TestActivityIdSameAsBackend()
5151

5252
Assert.AreEqual(ActivityIdGenerator.GenerateId(activity.ForeignId, time).ToString(), activity.Id);
5353
}
54+
55+
[Test]
56+
public async Task TestStreamJsonConverterUTC()
57+
{
58+
var date0 = new DateTime(2023, 5, 10, 12, 30, 15, DateTimeKind.Utc);
59+
var date0AsJsonNewtonsoft = Newtonsoft.Json.JsonConvert.SerializeObject(date0);
60+
var date0AsJson = Stream.Utils.StreamJsonConverterUTC.SerializeObject(date0);
61+
62+
Assert.AreEqual(date0AsJsonNewtonsoft, date0AsJson);
63+
}
5464
}
5565
}

0 commit comments

Comments
 (0)