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

时间:2011-06-17 09:56:53

标签: datetime json.net converter

我使用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);

渔获物在哪里?

1 个答案:

答案 0 :(得分:0)

问题在于转换器通过[JsonConverter(typeof(IsoDateTimeConverter))] 取代转换为传递给序列化器的转换器。这在Serialization Attributes: JsonConverterAttribute

中有记录
  

在   JsonConverterAttribute   指定哪个   JsonConverter   用于转换对象。

     

该属性可以放在类或成员上。当放在一个   class,由属性指定的JsonConverter将是   序列化该类的默认方式。当属性在a上时   字段或属性,然后指定的JsonConverter将始终   用于序列化该值。

     

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

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

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之上,如下所示:

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

现在,静态应用的转换器将根据需要被取代。样本fiddle

请注意,对于此特定测试用例,默认情况下,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"}

示例fiddle。然而,一般性问题值得回答。