Вместо
JObject.Parse()
, кстати, можно использовать
JOjbect.Load()
- который Parse внутреннее и использует, но только в Load уже можно передать настроенный на парсинг
DateTimeOffset
JsonReader
.
Вот текущая реализация
Parse:
public new static JObject Parse(string json, JsonLoadSettings? settings)
{
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
{
JObject o = Load(reader, settings);
while (reader.Read())
{
// Any content encountered here other than a comment will throw in the reader.
}
return o;
}
}
А вот такой будет исправленная версия, поддерживающая парсинг в
DateTimeOffset
:
static JObject ParseWithDateTimeOffset(string json, JsonLoadSettings? settings)
{
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
{
reader.DateParseHandling = DateParseHandling.DateTimeOffset;
JObject o = JObject.Load(reader, settings);
while (reader.Read())
{
// Any content encountered here other than a comment will throw in the reader.
}
return o;
}
}
И тогда результат выполнения кода:
var json = "{ \"Date\":\"2024-09-14T00:00:00+04:00\"}";
var jsonObject = ParseWithDateTimeOffset(json);
Console.WriteLine(jsonObject.ToString());
Будет:
{
"Date": "2024-09-14T00:00:00+04:00"
}