-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing issue with timestamps always being updated (#498)
* fixing issue with timestamps always being updated * fixing issue with byte array updates
- Loading branch information
1 parent
48ea85e
commit 57c271a
Showing
7 changed files
with
228 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Redis.OM.Modeling; | ||
|
||
/// <summary> | ||
/// Converter for Newtonsoft. | ||
/// </summary> | ||
internal class DateTimeJsonConvertNewtonsoft : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Determines is the object is convertable. | ||
/// </summary> | ||
/// <param name="objectType">the object type.</param> | ||
/// <returns>whether it can be converted.</returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(DateTime) || objectType == typeof(DateTime?); | ||
} | ||
|
||
/// <summary> | ||
/// writes the object to json. | ||
/// </summary> | ||
/// <param name="writer">the writer.</param> | ||
/// <param name="value">the value.</param> | ||
/// <param name="serializer">the serializer.</param> | ||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) | ||
{ | ||
if (value is DateTime dateTime) | ||
{ | ||
// Convert DateTime to Unix timestamp | ||
long unixTimestamp = ((DateTimeOffset)dateTime).ToUnixTimeMilliseconds(); | ||
writer.WriteValue(unixTimestamp); | ||
} | ||
else | ||
{ | ||
writer.WriteNull(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// reads an object back from json. | ||
/// </summary> | ||
/// <param name="reader">the reader.</param> | ||
/// <param name="objectType">the object type.</param> | ||
/// <param name="existingValue">the existing value.</param> | ||
/// <param name="serializer">the serializer.</param> | ||
/// <returns>The converted object.</returns> | ||
/// <exception cref="JsonSerializationException">thrown if issue comes up deserializing.</exception> | ||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
{ | ||
return objectType == typeof(DateTime?) ? null : DateTime.MinValue; | ||
} | ||
|
||
if (reader.TokenType == JsonToken.Integer && reader.Value is long unixTimestamp) | ||
{ | ||
// Convert Unix timestamp back to DateTime | ||
return DateTimeOffset.FromUnixTimeMilliseconds(unixTimestamp).UtcDateTime; | ||
} | ||
|
||
throw new JsonSerializationException("Invalid token type for Unix timestamp conversion."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
test/Redis.OM.Unit.Tests/RediSearchTests/ObjectWIthMultipleDateTimes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using Redis.OM.Modeling; | ||
|
||
namespace Redis.OM.Unit.Tests.RediSearchTests; | ||
|
||
[Document(StorageType = StorageType.Json, Prefixes = new []{"obj"})] | ||
public class ObjectWIthMultipleDateTimes | ||
{ | ||
[RedisIdField] | ||
[Indexed] | ||
public string Id { get; set; } | ||
public DateTime DateTime1 { get; set; } | ||
public DateTime DateTime2 { get; set; } | ||
} | ||
|
||
[Document(Prefixes = new []{"obj"})] | ||
public class ObjectWIthMultipleDateTimesHash | ||
{ | ||
[RedisIdField] | ||
[Indexed] | ||
public string Id { get; set; } | ||
public DateTime DateTime1 { get; set; } | ||
public DateTime DateTime2 { get; set; } | ||
} |
15 changes: 15 additions & 0 deletions
15
test/Redis.OM.Unit.Tests/RediSearchTests/ObjectWithByteArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Redis.OM.Modeling; | ||
|
||
namespace Redis.OM.Unit.Tests.RediSearchTests; | ||
|
||
[Document(StorageType = StorageType.Json, Prefixes = new []{"obj"})] | ||
public class ObjectWithByteArray | ||
{ | ||
[RedisIdField] | ||
[Indexed] | ||
public string Id { get; set; } | ||
|
||
public byte[] Bytes1 { get; set; } | ||
|
||
public byte[] Bytes2 { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters