Skip to content

Commit 40ffa00

Browse files
authored
normalizing json serialization settings in statemanager (#505)
1 parent 7f43b7b commit 40ffa00

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/Redis.OM/Modeling/RedisCollectionStateManager.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ namespace Redis.OM.Modeling
1212
/// </summary>
1313
public class RedisCollectionStateManager
1414
{
15-
private static JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
15+
private static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
1616
{
17-
NullValueHandling = NullValueHandling.Ignore, Converters = new List<JsonConverter> { new DateTimeJsonConvertNewtonsoft() },
17+
NullValueHandling = NullValueHandling.Ignore,
18+
DateFormatHandling = DateFormatHandling.IsoDateFormat,
19+
DateParseHandling = DateParseHandling.DateTimeOffset,
20+
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
21+
Converters = new List<JsonConverter> { new DateTimeJsonConvertNewtonsoft() },
1822
};
1923

2024
/// <summary>
@@ -82,7 +86,7 @@ internal void InsertIntoSnapshot(string key, object value)
8286

8387
if (DocumentAttribute.StorageType == StorageType.Json)
8488
{
85-
var json = JToken.FromObject(value, Newtonsoft.Json.JsonSerializer.Create(new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = new List<JsonConverter> { new DateTimeJsonConvertNewtonsoft() } }));
89+
var json = JToken.FromObject(value, Newtonsoft.Json.JsonSerializer.Create(_jsonSerializerSettings));
8690
Snapshot.Add(key, json);
8791
}
8892
else
@@ -110,7 +114,7 @@ internal bool TryDetectDifferencesSingle(string key, object value, out IList<IOb
110114
if (DocumentAttribute.StorageType == StorageType.Json)
111115
{
112116
var dataJson = JsonSerializer.Serialize(value, RedisSerializationSettings.JsonSerializerOptions);
113-
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
117+
var current = JsonConvert.DeserializeObject<JObject>(dataJson, _jsonSerializerSettings);
114118
var snapshot = (JToken)Snapshot[key];
115119
var diff = FindDiff(current!, snapshot);
116120
differences = BuildJsonDifference(diff, "$", snapshot);
@@ -146,7 +150,7 @@ internal IDictionary<string, IList<IObjectDiff>> DetectDifferences()
146150
if (Data.ContainsKey(key))
147151
{
148152
var dataJson = JsonSerializer.Serialize(Data[key], RedisSerializationSettings.JsonSerializerOptions);
149-
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
153+
var current = JsonConvert.DeserializeObject<JObject>(dataJson, _jsonSerializerSettings);
150154
var snapshot = (JToken)Snapshot[key];
151155
var diff = FindDiff(current!, snapshot);
152156
var diffArgs = BuildJsonDifference(diff, "$", snapshot);

test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ public class SearchTests
6060
})
6161
};
6262

63+
private readonly RedisReply _mockedReplyObjectWithDateTimeOffset = new[]
64+
{
65+
new RedisReply(1),
66+
new RedisReply(
67+
"Redis.OM.Unit.Tests.ObjectWithDateTimeOffsetJson:01FVN836BNQGYMT80V7RCVY73N"),
68+
new RedisReply(new RedisReply[]
69+
{
70+
"$",
71+
"{\"Id\":\"01FVN836BNQGYMT80V7RCVY73N\",\"DateTime\":1729592130000,\"Offset\":\"2024-12-25T00:00:00.000+01:00\"}"
72+
})
73+
};
74+
6375
private readonly RedisReply _mockedReplyObjectWIthMultipleByteArrays = new[]
6476
{
6577
new RedisReply(1),
@@ -1087,6 +1099,38 @@ public async Task TestUpdateJsonWithMultipleDateTimes()
10871099
Scripts.ShaCollection.Clear();
10881100
}
10891101

1102+
[Fact]
1103+
public async Task TestSaveJsonWithDateTimeOffset()
1104+
{
1105+
_substitute.ExecuteAsync("FT.SEARCH", Arg.Any<object[]>()).Returns(_mockedReplyObjectWithDateTimeOffset);
1106+
1107+
_substitute.ExecuteAsync("EVALSHA", Arg.Any<object[]>()).Returns(Task.FromResult(new RedisReply("42")));
1108+
_substitute.ExecuteAsync("SCRIPT", Arg.Any<object[]>())
1109+
.Returns(Task.FromResult(new RedisReply("cbbf1c4fab5064f419e469cc51c563f8bf51e6fb")));
1110+
var collection = new RedisCollection<ObjectWithDateTimeOffsetJson>(_substitute);
1111+
var obj = (await collection.Where(x => x.Id == "01FVN836BNQGYMT80V7RCVY73N").ToListAsync()).First();
1112+
obj.DateTime = obj.DateTime.AddMilliseconds(1);
1113+
await collection.SaveAsync();
1114+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.ObjectWithDateTimeOffsetJson:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.DateTime", "1729592130001");
1115+
Scripts.ShaCollection.Clear();
1116+
}
1117+
1118+
[Fact]
1119+
public async Task TestUpdateJsonWithDateTimeOffset()
1120+
{
1121+
_substitute.ExecuteAsync("FT.SEARCH", Arg.Any<object[]>()).Returns(_mockedReplyObjectWithDateTimeOffset);
1122+
1123+
_substitute.ExecuteAsync("EVALSHA", Arg.Any<object[]>()).Returns(Task.FromResult(new RedisReply("42")));
1124+
_substitute.ExecuteAsync("SCRIPT", Arg.Any<object[]>())
1125+
.Returns(Task.FromResult(new RedisReply("cbbf1c4fab5064f419e469cc51c563f8bf51e6fb")));
1126+
var collection = new RedisCollection<ObjectWithDateTimeOffsetJson>(_substitute);
1127+
var obj = (await collection.Where(x => x.Id == "01FVN836BNQGYMT80V7RCVY73N").ToListAsync()).First();
1128+
obj.DateTime = obj.DateTime.AddMilliseconds(1);
1129+
await collection.UpdateAsync(obj);
1130+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.ObjectWithDateTimeOffsetJson:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.DateTime", "1729592130001");
1131+
Scripts.ShaCollection.Clear();
1132+
}
1133+
10901134

10911135
[Fact]
10921136
public async Task TestUpdateJsonWithMultipleDateTimesHash()

0 commit comments

Comments
 (0)