DateTime 序列化程序省略毫秒?
DateTime serializers omitting milliseconds?
我注意到当值为 0.[=14 时,JSON.NET 和 System.Text.Json 都不会序列化日期时间的毫秒数=]
如,我看到的值如下:“3000-01-01T00:00:00”(无毫秒),和“3000-01-01T00:00:00.999 “
这是一个 fiddle 演示问题:https://dotnetfiddle.net/yi47EY
问题是,我们有一些客户正在崩溃,因为他们希望格式一致(例如,始终返回毫秒,即使 .000
)
我找到了这个参考:https://www.w3.org/TR/NOTE-datetime
其中指出:
An adopting standard that permits fractions of a second must specify
both the minimum number of digits (a number greater than or equal to
one) and the maximum number of digits (the maximum may be stated to be
"unlimited")."
那么,这是否意味着:
- JSON.NET 和 System.Text.Json 违反规范,因为我们发送的 'formats' 不同?或者
- 我们遵守规范,但所有客户端都应该灵活来处理不同的格式?
JSON.NET and System.Text.Json is breaking the specification, because we are sending different 'formats'?
JSON 不是“采用标准”,因为它没有引用 ISO 8601,也没有规定任何特定的日期和时间格式。在 JSON 中,它们只是字符串。所以序列化器可以自由地以他们喜欢的任何方式表示日期。两个序列化程序都选择 ISO 8601,不需要小数秒并且通常无用的额外字节。
这是 JSON 过于简单的缺点。
System.Text.Json 具有可用于覆盖序列化程序的默认行为的自定义转换器:How to write custom converters for JSON serialization (marshalling) in .NET
我注意到当值为 0.[=14 时,JSON.NET 和 System.Text.Json 都不会序列化日期时间的毫秒数=]
如,我看到的值如下:“3000-01-01T00:00:00”(无毫秒),和“3000-01-01T00:00:00.999 “
这是一个 fiddle 演示问题:https://dotnetfiddle.net/yi47EY
问题是,我们有一些客户正在崩溃,因为他们希望格式一致(例如,始终返回毫秒,即使 .000
)
我找到了这个参考:https://www.w3.org/TR/NOTE-datetime
其中指出:
An adopting standard that permits fractions of a second must specify both the minimum number of digits (a number greater than or equal to one) and the maximum number of digits (the maximum may be stated to be "unlimited")."
那么,这是否意味着:
- JSON.NET 和 System.Text.Json 违反规范,因为我们发送的 'formats' 不同?或者
- 我们遵守规范,但所有客户端都应该灵活来处理不同的格式?
JSON.NET and System.Text.Json is breaking the specification, because we are sending different 'formats'?
JSON 不是“采用标准”,因为它没有引用 ISO 8601,也没有规定任何特定的日期和时间格式。在 JSON 中,它们只是字符串。所以序列化器可以自由地以他们喜欢的任何方式表示日期。两个序列化程序都选择 ISO 8601,不需要小数秒并且通常无用的额外字节。
这是 JSON 过于简单的缺点。
System.Text.Json 具有可用于覆盖序列化程序的默认行为的自定义转换器:How to write custom converters for JSON serialization (marshalling) in .NET