Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
williamreg opened this issue Jan 26, 2019 · 3 comments

Comments

@williamreg
Copy link

如题

类似在Newtonsoft.Json中的配置:

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

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

@Dogwei
Copy link
Owner

Dogwei commented Feb 13, 2019

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

@williamreg
Copy link
Author

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

@Dogwei
Copy link
Owner

Dogwei commented Feb 14, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants