JsonSubTypes icon indicating copy to clipboard operation
JsonSubTypes copied to clipboard

DateTime issue with 1.8.0

Open Ultre00 opened this issue 5 years ago • 6 comments

My classes:

    [JsonConverter(typeof(JsonSubtypes), "discriminator")]
    [JsonSubtypes.KnownSubType(typeof(RangeTierCreateUpdateDto), "RangeTier")]
    public class TierCreateUpdateDto : EntityDto<int>
    {
        public DateTime StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public decimal Price { get; set; }
    }

    public class RangeTierCreateUpdateDto : TierCreateUpdateDto
    {
        public int From { get; set; }
        public int? Until { get; set; }
    }

With version 1.8.0 the DateTimes are being serialized to a default DateTime object.

Json received:

{
  "id": 4,
  "discriminator": "RangeTier",
  "price": 1,
  "startDate": "2020-01-01T00:00:00Z",
  "endDate": null,
  "from": 0,
  "until": 2
}

with version 1.8.0 the object is serialized to:

image

after i converted back to 1.7.0:

image

Ultre00 avatar Nov 12 '20 14:11 Ultre00

Hi @Ultre00

I do not reproduce your issue : https://dotnetfiddle.net/hyp5ON

Could provide a running sample of the code that shows the issue ?

Thanks

manuc66 avatar Nov 12 '20 21:11 manuc66

@manuc66 Thanks for the reply.

When calling the Deserialize from the JsonConvert.DeserializeObject directly it does work indeed. However when the serialization happens inside an API (.NET Core 3.1 Web.Api) it doesn't seem to work. I will have to investigate deeper because I am not sure how they call the actual NewtonSoft serializer.

I will try and create a running sample soon

Ultre00 avatar Nov 13 '20 12:11 Ultre00

@manuc66 I couldn't reproduce the issue in a clean project. The problem has been fixed but I am still confused why..

The project uses the abp.io framework (2.7.0) and had this line specified:

Configure<AbpJsonOptions>(options =>
{
    options.DefaultDateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ";
});

which I changed to "yyyy-MM-ddTHH:mm:ss.FFFFFFZ" and made it work with 1.8.0. I checked the abp github and this seemed to be used in a custom IsoDateTimeConverter.

However I still wanted to know why it wasn't working in the latest version, but was working in 1.7.0. So then when I add the source code of 1.8.0 and skip this bit :

private object ReadObject(JsonReader reader, Type objectType, JsonSerializer serializer)
        {
            // I would prefer to create a new reader, but can't work out how to
            // This seems a reasonable alternative - https://github.com/JamesNK/Newtonsoft.Json/issues/1605#issuecomment-602673364
            //var savedDateParseSettings = reader.DateParseHandling; <- skip
            //reader.DateParseHandling = DateParseHandling.None; <- skip
            var jObject = JObject.Load(reader);
            //reader.DateParseHandling = savedDateParseSettings; <- skip

            var targetType = GetType(jObject, objectType, serializer) ?? objectType;

            return ThreadStaticReadObject(reader, serializer, jObject, targetType);
        }

Then it does work in my own project (without changing the DefaultDateTimeFormat).

I can't tell what is different on the jObject as the seem identical to me when I debug them.

Any ideas why it wouldn't work when creating the JObject when setting DateParseHandling.None ?

Ultre00 avatar Nov 15 '20 14:11 Ultre00

Maybe the problem could be reproduced by adding a custom converted like the one in abp.io

manuc66 avatar Feb 26 '21 23:02 manuc66

Conflict with #113 #114

manuc66 avatar Feb 26 '21 23:02 manuc66

Also it seems jsonextensiondata will always have dates as stringvalues in 1.8.0, because of this line.

arthurvb avatar Dec 06 '21 16:12 arthurvb