Newtonsoft.json IsoDateTimeConverter和DateFormatHandling [英] Newtonsoft.json IsoDateTimeConverter and DateFormatHandling

查看:612
本文介绍了Newtonsoft.json IsoDateTimeConverter和DateFormatHandling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是问题,但我只想澄清一下概念. 当使用asp.net Web API时,我们使用Newtonsoft.Json的 SerializerSettings .在此设置中,我们可以设置

This is not an issue but concept I just want to make it clear. When working with asp.net web api we use Newtonsoft.Json's SerializerSettings. In this setting we can set

jsonSetting.Converters.Add(new IsoDateTimeConverter());
jsonSetting.DateFormatHandling = DateFormatHandling.IsoDateFormat;

我了解 IsoDateTimeConverter 在做什么,但不清楚 DateFormatHandling.IsoDateFormat .如果您仍在使用 IsoDateTimeConverter ,则 DateFormatHandling.MicrosoftDateFormat 似乎没有任何作用.

I understood what IsoDateTimeConverter is doing but but not clear about DateFormatHandling.IsoDateFormat. It seems to be there is no effect of DateFormatHandling.MicrosoftDateFormat if you still using IsoDateTimeConverter.

除了有一些对此有所了解的解释之外.

Still excepting some explanation who know about this.

推荐答案

您实际上有几个不同的相关问题.让我们分解一下:

You actually have several different related questions. Let's break them down:

当我为Json.NET本地支持的类型添加转换器时会发生什么?

如果这样做,则转换器会取代本机Json.NET功能.例如,您可以替换其对byte []字段的序列化.或者,您可以更改它对数字进行序列化和反序列化的方式:这个有点愚蠢的转换器通过添加1来对整数进行序列化:

If you do that, your converter supersedes the native Json.NET functionality. For instance, you can replace its serialization of byte [] fields. Or you can change how it serializes and deserializes numbers: this slightly silly converter serializes integers by adding 1:

public class IntConverter : JsonConverter
{ 
    public override bool CanConvert(Type objectType) { return objectType == typeof(int); }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return (int)JValue.Load(reader) - 1;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(1 + (int)value);
    }
}

Json.NET为什么要提供各种DateTime转换器以及对多种DateTime格式的本机支持?

Why does Json.NET provide various DateTime converters as well as native support for multiple DateTime formats?

这在文档用JSON序列化日期

JSON中的日期时间很困难.

DateTimes in JSON are hard.

问题来自JSON规范本身:JSON中没有日期的文字语法.规范包含对象,数组,字符串,整数和浮点数,但它没有定义日期的标准.

The problem comes from the JSON spec itself: there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers, and floats, but it defines no standard for what a date looks like.

对日期的本地支持可处理一些常见的格式;如果您使用其他格式的日期,则可以从 DateTimeConverterBase . (有关示例,请参阅如何转换新使用JSON.Net 重载日期(年,月,日).这样做之后,您的转换器将取代

The native support for dates handles some commonly encountered formats; if you have dates in some other format you can derive from DateTimeConverterBase. (For an example, see How to convert new Date(year, month, day) overload with JSON.Net.) Having done so, your converter will supersede the native conversion specified by DateFormatHandling.

Json.NET为什么有一个类IsoDateTimeConverter似乎在重复DateFormatHandling.IsoDateFormat的功能?

Why does Json.NET have a class IsoDateTimeConverter that seems to duplicate the functionality of DateFormatHandling.IsoDateFormat?

这是一个遗留类.从在JSON中序列化日期

从Json.NET 4.5开始,默认情况下,日期使用ISO 8601格式编写,因此不需要使用此转换器.

From Json.NET 4.5 and onwards dates are written using the ISO 8601 format by default, and using this converter is unnecessary.

这篇关于Newtonsoft.json IsoDateTimeConverter和DateFormatHandling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆