Swifter.Core
Swifter.Core copied to clipboard
序列化和反序列化时,时间类型可自定义格式吗?或有相关的配置项?
如题
类似在Newtonsoft.Json中的配置:
//时间格式
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//本地时间
setting.DateTimeZoneHandling = DateTimeZoneHandling.Local;
以前想装换成jil,因为jil不支持这种灵活的时间配置,还是继续用Newtonsoft.Json。
https://github.com/Dogwei/Swifter.Json 刚发布了新版本,看这个词条
public class Demo
{
public static void Main()
{
var jsonFormatter = new JsonFormatter();
jsonFormatter.SetDateTimeFormat("yyyy-MM-dd HH:mm:ss");
var json = jsonFormatter.Serialize(DateTime.Now);
Console.WriteLine(json); // "2019-02-13 11:03:46"
}
}
你这个只是序列化单一值(变量)。 我没有进行测试,请问可以在配置格式之后,在对类实例或集合实例序列化时,会自动应用到类或集合元素的时间属性字段吗? 如果不行,还得对每个字段进行设置。
另外建议加上以下类似特性,如 [Format(DateTimeFormat = "yyyy-MM-dd HH:mm:ss")] [Format(StringFormat = "n2")]
可以的,特性的话我打算下一版本增加自定义处理器。不过要可能要等两三个月