解析ISO持续时间与JSON.Net [英] Parsing ISO Duration with JSON.Net
问题描述
我在用下面设置的Web API项目的Global.asax.cs
:
VAR serializerSettings =新JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};serializerSettings.Converters.Add(新IsoDateTimeConverter());VAR jsonFormatter =新JsonMediaTypeFormatter {SerializerSettings = serializerSettings};
jsonFormatter.MediaTypeMappings.Add(GlobalConfiguration.Configuration.Formatters[0].MediaTypeMappings[0]);GlobalConfiguration.Configuration.Formatters [0] = jsonFormatter;WebApiConfig.Register(GlobalConfiguration.Configuration);
尽管如此,Json.Net无法解析 ISO持续时间的。
它抛出这个错误:
错误转换值2007-03-01T13:00:00Z / 2008-05-11T15:30:00Z来
类型'System.TimeSpan'。
块引用>我使用Json.Net V4.5。
我试过,如P1M和其他人没有运气wiki页面上列出的不同的值。
所以,问题是:
- 我缺少的东西吗?
- 或者我必须写一些自定义的格式?
解决方案我遇到了同样的问题,现在用这个自定义转换器转换成.NET时间跨度为ISO 8601时长的字符串。
公共类TimeSpanConverter:JsonConverter
{
公共覆盖无效WriteJson(JsonWriter作家,对象的值,JsonSerializer串行)
{
VAR TS =(时间跨度)值;
变种tsString = XmlConvert.ToString(TS);
serializer.Serialize(作家,tsString);
} 公众覆盖对象ReadJson(JsonReader读者,类型的objectType,对象existingValue,
JsonSerializer串行)
{
如果(reader.TokenType == JsonToken.Null)
{
返回null;
} VAR值= serializer.Deserialize<串GT;(读卡器);
返回XmlConvert.ToTimeSpan(值);
} 公众覆盖布尔CanConvert(类型的objectType)
{
返回的objectType == typeof运算(时间跨度)|| ==的objectType typeof运算(时间跨度?);
}
}I have a Web API project with the following settings in
Global.asax.cs
:var serializerSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc }; serializerSettings.Converters.Add(new IsoDateTimeConverter()); var jsonFormatter = new JsonMediaTypeFormatter { SerializerSettings = serializerSettings }; jsonFormatter.MediaTypeMappings.Add(GlobalConfiguration.Configuration.Formatters[0].MediaTypeMappings[0]); GlobalConfiguration.Configuration.Formatters[0] = jsonFormatter; WebApiConfig.Register(GlobalConfiguration.Configuration);
Despite all this, Json.Net cannot parse ISO durations.
It throws this error:
Error converting value "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z" to type 'System.TimeSpan'.
I'm using Json.Net v4.5.
I've tried different values such as "P1M" and others listed on the wiki page with no luck.
So the question is:
- Am I missing something?
- Or do I have to write some custom formatter?
解决方案I ran into the same problem and am now using this custom converter to Convert .NET TimeSpans to ISO 8601 Duration strings.
public class TimeSpanConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var ts = (TimeSpan) value; var tsString = XmlConvert.ToString(ts); serializer.Serialize(writer, tsString); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return null; } var value = serializer.Deserialize<String>(reader); return XmlConvert.ToTimeSpan(value); } public override bool CanConvert(Type objectType) { return objectType == typeof (TimeSpan) || objectType == typeof (TimeSpan?); } }
这篇关于解析ISO持续时间与JSON.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!