0

This seems to be much like a duplicate of Deserializing dates with dd/mm/yyyy format using Json.Net but the proposed solution doesn't seem to work for me.

I have a test case

void Main()
{
    if (File.Exists("config.json"))
    {
        string json = File.ReadAllText("config.json");
        var dateTimeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy" };
        PositionForwardingConfiguration config = Newtonsoft.Json.JsonConvert.DeserializeObject<PositionForwardingConfiguration>(json, dateTimeConverter);
        foreach (var client in config.ClientConfiguration.clients)
        {
            Debug.WriteLine(string.Format("Date: {0}", client.MiniumDate));
        }
        json = Newtonsoft.Json.JsonConvert.SerializeObject(config, dateTimeConverter);
        Debug.WriteLine(json);
    }

}
public class PositionForwardingConfiguration
{
    public ClientConfiguration ClientConfiguration { get; set; }
}

public class ClientConfiguration
{
    public ClientInfo[] clients { get; set; }
}
public class ClientInfo
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime MiniumDate { get; set; }
    public string Token { get; set; }
    public Uri Endpoint { get; set; }
}

that produces output like

Date: 1/1/0001 12:00:00 AM
Date: 1/1/0001 12:00:00 AM
Date: 1/1/0001 12:00:00 AM
{"ClientConfiguration":{"clients":[{"Id":1,"Name":"1","MiniumDate":"01/01/0001","Token":"Token 1","Endpoint":"http://1"},{"Id":2,"Name":"2","MiniumDate":"01/01/0001","Token":"Token 2","Endpoint":"http://2"},{"Id":3,"Name":"3","MiniumDate":"01/01/0001","Token":"Token 3","Endpoint":"http://3"}]}}

Particular note the date is not deserializing or serializing using the supplied converter. Has something changed with Newtonsoft.JSON to make this not work? My input looks like

{
    "ClientConfiguration": {
        "clients": [
        {
            "Id" : 1,
            "Name" : "1",
            "MinimumDate" : "01/01/2017",
            "Token" : "Token 1",
            "Endpoint" : "http://1"
        },
        {
            "Id" : 2,
            "Name" : "2",
            "MinimumDate" : "02/01/2017",
            "Token" : "Token 2",
            "Endpoint" : "http://2"
        },
        {
            "Id" : 3,
            "Name" : "3",
            "MinimumDate" : "03/01/2017",
            "Token" : "Token 3",
            "Endpoint" : "http://3"
        }
      ]
    }
}

Ideas?

Community
  • 1
  • 1
Kevin Burton
  • 2,032
  • 4
  • 26
  • 43
  • I don't see where you are using the date/time converter for deserialization? – Ron Beyer Mar 15 '17 at 16:37
  • The problem is that the date time converter var dateTimeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy" }; PositionForwardingConfiguration config = Newtonsoft.Json.JsonConvert.DeserializeObject(json); doesn't seem to be used in the nested classes. I am not sure how to make the deserialization process use the converter I am passing to the base. – Kevin Burton Mar 15 '17 at 22:35
  • You aren't passing it to the base converter though, it should be something like `config = ... DeserializeObject(j‌​son, dateTimeConverter)` You only use the `dateTimeConverter` when you serialize at the end... – Ron Beyer Mar 15 '17 at 23:49
  • By the way, your data structure has a member named `MiniumDate` while the input file uses `MinimumDate`, the misspelled member is not getting populated with the data from your input file for obvious reasons. – Ron Beyer Mar 16 '17 at 14:27

0 Answers0