68

我数据库中的日期存储为 Utc。但是当我使用实体框架检索它们时,它们会以未指定的类型出现。

当 JSON.Net 序列化它们时,它们不是 Utc 格式。有没有办法告诉 JSON.Net 将 DateTimes 序列化为 Utc,即使它们的类型未指定为 Utc?

4

5 回答 5

127

设置为。DateTimeZoneHandling_ 这将在序列化它们之前将所有日期转换为 UTC。JsonSerializerSettingsUtc

public void SerializeObjectDateTimeZoneHandling()
{
  string json = JsonConvert.SerializeObject(
    new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified),
    new JsonSerializerSettings
    {
      DateTimeZoneHandling = DateTimeZoneHandling.Utc
    });

  Assert.AreEqual(@"""2000-01-01T01:01:01Z""", json);
}

文档:DateTimeZoneHandling 设置

于 2012-04-24T21:00:06.690 回答
16

上面的响应完全有效,所以我用它来创建一个属性来将 API 响应从 PST 转换为 UTC。

首先我需要创建一个JsonConverter

public class UTCDateTimeConverter : Newtonsoft.Json.JsonConverter {
    private TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    public override bool CanConvert(Type objectType) {
        return objectType == typeof(DateTime);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        if (reader.Value == null) return null;
        var pacificTime = DateTime.Parse(reader.Value.ToString());
        return TimeZoneInfo.ConvertTimeToUtc(pacificTime, pacificZone);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
        writer.WriteValue(TimeZoneInfo.ConvertTimeFromUtc((DateTime) value, pacificZone));
    }
}

然后我必须将其应用于需要转换的属性

public class Order{
    [JsonConverter(typeof(UTCDateTimeConverter))]
    public DateTime OrderDate {get;set;}
}
于 2015-09-09T22:24:57.370 回答
2

正如@dez 在评论中提到的那样,您可以在从数据库加载它们并在序列化它们之前直接在.net 代码中将 DateTime 对象“标记”为 UTC:

var item = GetItemFromDb(...);

// mark appropriate DateTime fields manually as needed
item.OrderDate = DateTime.SpecifyKind(item.OrderDate, DateTimeKind.Utc);

// now it will be serialized to "2018-10-17T16:21:23.507Z" with the Z at the end
// and javascript will parse it properly and convert to local timezone as needed
于 2018-10-17T16:59:29.390 回答
1

对我来说,为 DateTime 属性创建 UTC 转换器更简单(基于 Newtonsoft.Json.Converters.IsoDateTimeConverter 的实现)。

public class UtcJsonDateTimeConverter : DateTimeConverterBase
{
    private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFZ";

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        string text;

        if (value is DateTime dateTime)
        {
            text = dateTime.ToString(DefaultDateTimeFormat, CultureInfo.InvariantCulture);
        }
        else
        {
            throw new JsonSerializationException(
                $"Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {value.GetType()}.");
        }

        writer.WriteValue(text);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        bool nullable = objectType == typeof(DateTime?);
        if (reader.TokenType == JsonToken.Null)
        {
            if (!nullable)
            {
                throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
            }

            return null;
        }

        if (reader.TokenType == JsonToken.Date)
        {
            return reader.Value;
        }
        else if (reader.TokenType != JsonToken.String)
        {
            throw new JsonSerializationException($"Unexpected token parsing date. Expected String, got {reader.TokenType}.");
        }

        string date_text = reader.Value.ToString();

        if (string.IsNullOrEmpty(date_text) && nullable)
        {
            return null;
        }

        return DateTime.Parse(date_text, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
    }
}

public class SomeEntity
{

   [JsonProperty(PropertyName = "id", Order = 1)]
   public int ID { get; set; }

   [JsonProperty(PropertyName = "created", Order = 2)]
   [JsonConverter(typeof(UtcJsonDateTimeConverter))]
   public DateTime Created { get; set; }
}
于 2019-04-30T14:10:05.577 回答
1

我使用了接受的答案,但适用于默认设置:

        JsonConvert.DefaultSettings = (() =>
        {
            var settings = new JsonSerializerSettings();
            settings.Converters.Add(new StringEnumConverter());
            settings.Formatting = Formatting.Indented;
            settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            return settings;
        });
于 2020-02-25T03:07:02.260 回答