Skip to content

Commit 24350f8

Browse files
authored
Use MachineLearningDateTimeFormatter (#4435)
This commit uses the MachineLearningDateTimeFormatter when a model property is attributed with MachineLearningDateTimeAttribute. Without the application of the formatter, an attempt is made to deserialize a returned timestamp numeric value as a string in PreviewDataFeedApiTests
1 parent 75e205f commit 24350f8

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/Nest/CommonAbstractions/SerializationBehavior/Attributes/MachineLearningDateTimeAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static class DateTimeUtil
1111
}
1212

1313
/// <summary>
14-
/// Signals that this date time property is used in Machine learning API's some of which will always return the date as
14+
/// Signals that this date time property is used in Machine learning APIs some of which will always return the date as
1515
/// epoch.
1616
/// </summary>
1717
[AttributeUsage(AttributeTargets.Property)]
@@ -50,6 +50,8 @@ public virtual void Serialize(ref JsonWriter writer, DateTimeOffset value, IJson
5050

5151
internal class MachineLearningDateTimeFormatter : IJsonFormatter<DateTime>
5252
{
53+
public static readonly MachineLearningDateTimeFormatter Instance = new MachineLearningDateTimeFormatter();
54+
5355
public DateTime Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
5456
{
5557
var token = reader.GetCurrentJsonToken();

src/Nest/CommonAbstractions/SerializationBehavior/JsonFormatters/NestFormatterResolver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ private JsonProperty GetMapping(MemberInfo member)
116116
break;
117117
}
118118
}
119+
else if (member.GetCustomAttribute<MachineLearningDateTimeAttribute>() != null)
120+
property.JsonFormatter = MachineLearningDateTimeFormatter.Instance;
119121

120122
return property;
121123
}

0 commit comments

Comments
 (0)