使用JSON.NET自定义DateTime序列化



我正在尝试创建自定义DateTime转换器。问题:我有许多序列化对象,其中一些包含datetime的属性带有dateTime.minvalue。我想将其序列化为空。但是我要求装饰物体内部适当的所有解决方案(我做不到)据我所知,我在下面发现的其他解决方案是创建转换器,此转换器仅作用DateTime对象明确返回而不是在其他对象中。请帮助。

public class DateTimeConverter : JsonConverter
{
    private readonly Type[] types;
    public DateTimeConverter(params Type[] types)
    {
        this.types = types;
    }
    public override bool CanConvert(Type objectType)
    {
        return types.Any(t => t == objectType);
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JToken t = JToken.FromObject(value);
        if (t.Type != JTokenType.Object)
        {
            if (value is DateTime && value.Equals(DateTime.MinValue))
            {
                t = JToken.FromObject(null);
                t.WriteTo(writer);
            }
            else
            {
                t.WriteTo(writer);
            }
        }
        else
        {
            if (value.Equals(DateTime.MinValue)) {
                t = JToken.FromObject(null);
                t.WriteTo(writer);
            }
            else {
                t.WriteTo(writer);
            }
        }
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
    }

我认为您过于复杂的事情。您的转换器只应担心转换日期值,而不是可能包含日期值的对象。每当在任何其他对象内遇到日期时,序列化器都会自然地调用您的转换器。

这是一个简单的转换器,可以执行您想要的事情:

public class MinDateToNullConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        // This converter handles date values directly
        return (objectType == typeof(DateTime));
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        // The CanConvert method guarantees the value will be a DateTime
        DateTime date = (DateTime)value;
        if (date == DateTime.MinValue)
        {
            writer.WriteNull();
        }
        else
        {
            writer.WriteValue(date);
        }
    }
    public override bool CanRead
    {
        get { return false; }
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

这是一个演示,显示转换器在对象的嵌套层次结构中工作:

class Program
{
    static void Main(string[] args)
    {
        Example example = new Example
        {
            Date1 = new DateTime(2014, 2, 2),
            Date2 = DateTime.MinValue,
            Inner = new Inner
            {
                DateA = DateTime.MinValue,
                DateB = new DateTime(1954, 1, 26)
            },
            MoreDates = new List<DateTime>
            {
                new DateTime(1971, 11, 15),
                DateTime.MinValue
            }
        };
        // Set up the serializer to use our date converter
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Converters.Add(new MinDateToNullConverter());
        settings.Formatting = Formatting.Indented;
        string json = JsonConvert.SerializeObject(example, settings);
        Console.WriteLine(json);
    }
}
class Example
{
    public DateTime Date1 { get; set; }
    public DateTime Date2 { get; set; }
    public Inner Inner { get; set; }
    public List<DateTime> MoreDates { get; set; }
}
class Inner
{
    public DateTime DateA { get; set; }
    public DateTime DateB { get; set; }
}

输出:

{
  "Date1": "2014-02-02T00:00:00",
  "Date2": null,
  "Inner": {
    "DateA": null,
    "DateB": "1954-01-26T00:00:00"
  },
  "MoreDates": [
    "1971-11-15T00:00:00",
    null
  ]
}

当转换器实现CanConvert(并且您的确实)时,您可以将其添加到序列化器设置:

JsonConvert.SerializeObject(foo, new JsonSerializerSettings {
    Converters = {
        new DateTimeConverter()
    }
});

这样,它将应用于其支持的所有对象。

找到了我问题的答案。实际上,我在Jsonserializer设置中有2个转换器。我注意到,当我删除包含DateTime MinValue属性的对象A上的转换器时,我的序列化器工作正常。这使我了解到,DateTime对NULL的转换器在其他转换器下不起作用。因此,我要做的是在对象中手动转换一个转换器,而不是完全干净的解决方案,我希望DateTimetonull转换器也将在转换器内部使用。这是我在转换器中所做的:

    public class AConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(A));
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JToken jToken = JToken.FromObject(value);
        JObject jObject = (JObject)jToken;
        A aInfo = (A) value;
        string statusString = aInfo.Status == null ? string.Empty : SomeUtil.GetStateString((State)aInfo.Status.State, aInfo.Status.Downloading);
        jObject.AddFirst(new JProperty("MachineState", statusString));
        if (aInfo.UploadTime == DateTime.MinValue) {
            jObject.Remove("UploadTime");
            jObject.Add(new JProperty("UploadTime", null));
        }
        jObject.WriteTo(writer);
    }
    public override bool CanRead
    {
        get
        {
            return false;
        }
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException("It's OK!");
    }

}

相关内容

  • 没有找到相关文章

最新更新