-
Notifications
You must be signed in to change notification settings - Fork 317
fix: fixed date time deserializer #827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,16 @@ | |
| } | ||
| } | ||
|
|
||
| public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
|
Check failure on line 30 in src/Twilio/Converters/CustomConverters.cs
|
||
| { | ||
| if (reader.TokenType == JsonToken.Null) | ||
| { | ||
| return null; | ||
| } | ||
| if (reader.TokenType == JsonToken.Date) | ||
| { | ||
| return (DateTime)reader.Value; | ||
| } | ||
|
Comment on lines
+36
to
+44
|
||
| if (reader.TokenType == JsonToken.StartArray) | ||
| { | ||
| var dateTimes = new List<DateTime>(); | ||
|
|
@@ -42,6 +50,10 @@ | |
| dateTimes.Add(dateTime); | ||
| } | ||
| } | ||
| else if (reader.TokenType == JsonToken.Date) | ||
| { | ||
| dateTimes.Add((DateTime)reader.Value); | ||
| } | ||
|
Comment on lines
32
to
+61
|
||
| else if (reader.TokenType == JsonToken.EndArray) | ||
| { | ||
| return dateTimes; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReadJsonreturns aDateTimeforJsonToken.Dateregardless ofobjectType. If this converter is used forList<DateTime>, a non-array JSON value (e.g., a single date) will cause the converter to return the wrong runtime type and Newtonsoft will throw during assignment. Consider branching onobjectTypeand either (a) wrapping a single date into a singleton list forList<DateTime>or (b) throwing a clear exception when the JSON shape doesn't match the target type (and similarly avoid returning a list whenobjectTypeisDateTime?).