为什么Json.net不使用自定义的IsoDateTimeConverter



我使用Json.net序列化我的对象,并且我想要自定义DateTime输出:

这里有一个小例子:

[DataContract]
class x
{
    [DataMember]
    [JsonConverter(typeof(IsoDateTimeConverter))]
    public DateTime datum = new DateTime(1232, 3, 23);
}
var dtc = new IsoDateTimeConverter();
dtc.DateTimeFormat = "yy";
JsonConvert.SerializeObject(new x(), dtc);

结果是{"datum":"1232-03-23T00:00:00"}而不是{"datum":"1232"}

此操作正常(返回"32"):

return JsonConvert.SerializeObject(new DateTime(1232, 3, 23), dtc);

捕获物在哪里?

问题是通过[JsonConverter(typeof(IsoDateTimeConverter))]应用的转换器将取代传递到序列化程序中的转换器。这在序列化属性:JsonConverterAttribute:中有记录

JsonConverterAttribute指定Json转换器用于转换对象。

属性可以放置在类或成员上。当放置在类,属性指定的JsonConverter将是序列化该类的默认方式。当属性位于字段或属性,则指定的JsonConverter将始终为用于序列化该值。

使用JsonConverter的优先级是成员属性,然后class属性,最后是传递给JsonSerializer

作为一种变通方法,在应用转换器的ReadJson()WriteJson()方法中,可以在串行器的转换器列表中检查相关转换器,如果找到了,就使用它。decorator模式可以用于将此逻辑与底层转换逻辑分离。首先介绍:

public class OverridableJsonConverterDecorator : JsonConverterDecorator
{
    public OverridableJsonConverterDecorator(Type jsonConverterType) : base(jsonConverterType) { }
    public OverridableJsonConverterDecorator(JsonConverter converter) : base(converter) { }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        foreach (var converter in serializer.Converters)
        {
            if (converter == this)
            {
                Debug.WriteLine("Skipping identical " + converter.ToString());
                continue;
            }
            if (converter.CanConvert(value.GetType()) && converter.CanWrite)
            {
                converter.WriteJson(writer, value, serializer);
                return;
            }
        }
        base.WriteJson(writer, value, serializer);
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        foreach (var converter in serializer.Converters)
        {
            if (converter == this)
            {
                Debug.WriteLine("Skipping identical " + converter.ToString());
                continue;
            }
            if (converter.CanConvert(objectType) && converter.CanRead)
            {
                return converter.ReadJson(reader, objectType, existingValue, serializer);
            }
        }
        return base.ReadJson(reader, objectType, existingValue, serializer);
    }
}
public abstract class JsonConverterDecorator : JsonConverter
{
    readonly JsonConverter converter;
    public JsonConverterDecorator(Type jsonConverterType) : this((JsonConverter)Activator.CreateInstance(jsonConverterType)) { }
    public JsonConverterDecorator(JsonConverter converter)
    {
        if (converter == null)
            throw new ArgumentNullException();
        this.converter = converter;
    }
    public override bool CanConvert(Type objectType)
    {
        return converter.CanConvert(objectType);
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return converter.ReadJson(reader, objectType, existingValue, serializer);
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        converter.WriteJson(writer, value, serializer);
    }
    public override bool CanRead { get { return converter.CanRead; } }
    public override bool CanWrite { get { return converter.CanWrite; } }
}

然后,在IsoDateTimeConverter的顶部应用decorator,如下所示:

[DataContract]
class x
{
    [DataMember]
    [JsonConverter(typeof(OverridableJsonConverterDecorator), typeof(IsoDateTimeConverter))]
    public DateTime datum = new DateTime(1232, 3, 23);
}

现在,静态应用的转换器将根据需要被取代。样品小提琴。

请注意,对于这个特定的测试用例,从Json.NET 4.5.1开始,默认情况下日期在ISO中序列化,不再需要IsoDateTimeConverter。强制以特定格式序列化日期可以通过设置JsonSerializerSettings.DateFormatString:来实现

[DataContract]
class x
{
    [DataMember]
    public DateTime datum = new DateTime(1232, 3, 23);
}
var settings = new JsonSerializerSettings { DateFormatString = "yy" };
var json1 = JsonConvert.SerializeObject(new x(), settings);
Console.WriteLine(json1); // Prints {"datum":"32"}
var json2 = JsonConvert.SerializeObject(new x());
Console.WriteLine(json2); // Prints {"datum":"1232-03-23T00:00:00"}

样品小提琴。然而,总的问题应该得到回答。

相关内容

  • 没有找到相关文章

最新更新