JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.73k stars 3.25k forks source link

DateOnly string does not deserialize to DateOnly Object when target is object #2831

Open niklasweimann opened 1 year ago

niklasweimann commented 1 year ago

DateOnly string does not deserialize to DateOnly Object when target is object. But DateTime strings are deserialized to DateTime objects when the string is in the correct format for a DateTime object (see JsonSerializerSettings.DefaultDateFormatString).

Source/destination types

object[]

Source/destination JSON

["2023-03-13T10:12:47.1830163+00:00",123,"9999-12-31"]

Expected behavior

Deserialize as DateOnly Object

Actual behavior

Deserializes as string

Steps to reproduce

var list = new object[] { DateTime.Now, 123, DateOnly.MaxValue };
var json = JsonConvert.SerializeObject(list);
var listDeserializeObject = JsonConvert.DeserializeObject<object[]>(json);
Assert.AreEqual(list[0], listDeserializeObject[0]); // Working: Type == DateTime
Assert.AreEqual(list[1], listDeserializeObject[1]); // Working: Type == int
Assert.AreEqual(list[2], listDeserializeObject[2]); // Not Working: Type == string, but should be DateOnly
aleripe commented 6 months ago

Any news?

CamilloBorges commented 2 months ago

I'm have the same issue. I Foked the project to include this function at link https://github.com/CamilloBorges/Newtonsoft.Json. However is not a FULL Validated and have Broken test that.