0

I have this structure of object:

public class FirstClassResponse
{
    public int draw { get; set; }
    public int recordsTotal { get; set; }
    public int recordsFiltered { get; set; }
    public List<SecondClass> data { get; set; }
}

public class SecondClass
{
    public long Id { get; set; }
    public System.DateTime InsertionDate { get; set; }
    public System.DateTime DeletionDate { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string ZipCode { get; set; }
    public string EmailAddress { get; set; }
    public string PhoneNumber { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public System.DateTime LastUpdate { get; set; }
}

I need to serialize the FirstClassResponse and currently I use Json.NET for it. But the InsertionDate, DeletionDate, and LastUpdate have their own DateTime format for serialization.

I got this post as a reference, however, I am still wondering how to implement it in my case, which has the multiple datetime properties inside the List.

public class FirstClassResponseJSONConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {

        JObject result = new JObject();

        FirstClassResponse data = (SubContractorResponse)value;

        //result.Add("data.LastUpdate", JToken.FromObject(data.data..ToString("MM.dd.yyyy")));
        //result.Add("data.DeletionDate", JToken.FromObject(data.DateTwo));
        //still don't have any idea for this part..

        result.WriteTo(writer);
    }

    // Other JsonConverterMethods
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(SubContractorResponse);
    }

    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }
    public override bool CanRead
    {
        get
        {
            return false;
        }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
Community
  • 1
  • 1
Yusril Maulidan Raji
  • 1,682
  • 1
  • 21
  • 46
  • Use the [second solution](http://stackoverflow.com/a/22152171/10263) in the other question you referenced. Subclass the `IsoDateTimeConverter` for each different format you need, then add `[JsonConverter]` attributes to the `InsertionDate`, `DeletionDate` and `LastUpdate` properties in your `SecondClass`. – Brian Rogers Nov 17 '16 at 17:05
  • Fiddle: https://dotnetfiddle.net/v6JJSN – Brian Rogers Nov 17 '16 at 17:20
  • No, in this case your question is pretty much the same as the other one, and the solution is the same, so I'm just going to close this question as a duplicate. – Brian Rogers Nov 18 '16 at 15:58

0 Answers0