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

请问怎么自定义解析Date类型? #4

Closed
youngsing opened this issue Mar 29, 2019 · 5 comments
Closed

请问怎么自定义解析Date类型? #4

youngsing opened this issue Mar 29, 2019 · 5 comments

Comments

@youngsing
Copy link

let decoder = CleanJSONDecoder()
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
    return Date()
})

上面的代码没有生效,看源码里面解析的时候用的是self.options.dateDecodingStrategy,这个在库外面可以配置吗?

@Pircate
Copy link
Owner

Pircate commented Mar 29, 2019

可以配置的,你的 JSON 数据是什么样的呢?

@Pircate
Copy link
Owner

Pircate commented Mar 29, 2019

Date(timeIntervalSinceNow: 1512217128)

默认是用这个方法解析时间戳转成 Date 的。

@youngsing
Copy link
Author

我这边JSON数据里面其实是没有Date类型数据的,我只是想在转换的时候插入一个当前时间戳。。
然后我看JSONAdapter里面好像没有Date相关协议,CleanJSONDecoder.options好像也不是公开属性?是有其它什么途径来配置吗?

@Pircate
Copy link
Owner

Pircate commented Mar 29, 2019

JSONAdapter 暂时不支持 Date 类型.
建议你设一个默认值.

@youngsing
Copy link
Author

多谢,我再研究研究!
不过像"time": 1553875884这种Unix时间戳,由现在默认的DateDecodingStrategy.deferredToDate解析出来是不对。

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