Pircate / CleanJSON

Swift JSON decoder for Codable
MIT License
292 stars 40 forks source link

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

Closed youngsing closed 5 years ago

youngsing commented 5 years ago
let decoder = CleanJSONDecoder()
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
    return Date()
})

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

Pircate commented 5 years ago

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

Pircate commented 5 years ago
Date(timeIntervalSinceNow: 1512217128)

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

youngsing commented 5 years ago

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

Pircate commented 5 years ago

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

youngsing commented 5 years ago

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