Swifter.Core icon indicating copy to clipboard operation
Swifter.Core copied to clipboard

序列化和反序列化时,时间类型可自定义格式吗?或有相关的配置项?

Open williamreg opened this issue 6 years ago • 3 comments

如题

类似在Newtonsoft.Json中的配置:

//时间格式
                setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //本地时间
                setting.DateTimeZoneHandling = DateTimeZoneHandling.Local;

以前想装换成jil,因为jil不支持这种灵活的时间配置,还是继续用Newtonsoft.Json。

williamreg avatar Jan 26 '19 16:01 williamreg

https://github.com/Dogwei/Swifter.Json 刚发布了新版本,看这个词条

Dogwei avatar Feb 13 '19 08:02 Dogwei

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")]

williamreg avatar Feb 14 '19 01:02 williamreg

可以的,特性的话我打算下一版本增加自定义处理器。不过要可能要等两三个月

Dogwei avatar Feb 14 '19 08:02 Dogwei