gpt4 book ai didi

c# - 将 JSON DateTime 值转换为零值的最佳方法(例如 "0000-00-00 00:00:00")

转载 作者:太空狗 更新时间:2023-10-30 01:35:08 25 4
gpt4 key购买 nike

转换仅包含零的 JSON 日期时间值(例如“0000-00-00 00:00:00”)不适用于标准 Json.net IsoDateTimeConverter。我开发了一个自定义转换器来保存此值 DateTime.MinValue。 DateTime.MinValue 也将被写为“ZeroDateString”。所有其他字符串都由基础 IsoDateTimeConverter 类处理。我在 DateTime 属性的 JsonNet 注释上使用此转换器。

有没有更好、更简单的方法来处理这个问题,例如在 mor 基础级别上,不需要注释?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace DataLayer
{
/// <summary>
/// Custom IsoDateTimeConverter for DateTime strings with zeros.
///
/// Usage Sample
/// [JsonConverter(typeof(ZerosIsoDateTimeConverter), "yyyy-MM-dd hh:mm:ss", "0000-00-00 00:00:00")]
/// public DateTime Zerodate { get; set; }

/// </summary>
public class ZerosIsoDateTimeConverter : Newtonsoft.Json.Converters.IsoDateTimeConverter
{
/// <summary>
/// The string representing a datetime value with zeros. E.g. "0000-00-00 00:00:00"
/// </summary>
private readonly string _zeroDateString;

/// <summary>
/// Initializes a new instance of the <see cref="ZerosIsoDateTimeConverter"/> class.
/// </summary>
/// <param name="dateTimeFormat">The date time format.</param>
/// <param name="zeroDateString">The zero date string.
/// Please be aware that this string should match the date time format.</param>
public ZerosIsoDateTimeConverter(string dateTimeFormat, string zeroDateString)
{
DateTimeFormat = dateTimeFormat;
_zeroDateString = zeroDateString;
}

/// <summary>
/// Writes the JSON representation of the object.
/// If a DateTime value is DateTime.MinValue than the zeroDateString will be set as output value.
/// </summary>
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is DateTime && (DateTime) value == DateTime.MinValue)
{
value = _zeroDateString;
serializer.Serialize(writer, value);
}
else
{
base.WriteJson(writer, value, serializer);
}
}

/// <summary>
/// Reads the JSON representation of the object.
/// If an input value is same a zeroDateString than DateTime.MinValue will be set as return value
/// </summary>
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>
/// The object value.
/// </returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
return reader.Value.ToString() == _zeroDateString
? DateTime.MinValue
: base.ReadJson(reader, objectType, existingValue, serializer);
}
}
}

最佳答案

我遇到了同样的问题,为此我使用了自定义转换器...

class MyDateConverter: Newtonsoft.Json.Converters.IsoDateTimeConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value != null && reader.Value.ToString().StartsWith("0000")) return null;
else return base.ReadJson(reader, objectType, existingValue, serializer);
}
}

关于c# - 将 JSON DateTime 值转换为零值的最佳方法(例如 "0000-00-00 00:00:00"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com