I am using the Facebook Graph API which is returning a a json object that contains a datetime in a string in the following format: "created_time": "2013-01-25T00:11:02+0000"
The deserialized object contains this datetime: "0001-01-01 00:00:00", which I suppose is the equivalent of null. How can I deserialize the datetime string properly?
Here is my DTO class:
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Newtonsoft.Json.Converters;
using project.DTOs;
namespace project.DTOs.Facebook
{
public class FacebookCommentResponseDto
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("from")]
public FacebookUserDto From { get; set; }
[JsonPropertyName("message")]
public string Message { get; set; }
[JsonPropertyName("created_time")]
[JsonConverter(typeof(CustomDateTimeConverter))]
public DateTime CreatedTime { get; set;}
}
internal class CustomDateTimeConverter : IsoDateTimeConverter
{
public CustomDateTimeConverter()
{
base.DateTimeFormat = "yyyy-MM-ddTH:mm:ss.fffK";
}
}
}
This is the code where I deserialize the object:
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);