Coder Social home page Coder Social logo

Comments (6)

ZhangZihe avatar ZhangZihe commented on May 5, 2024

json为nil

from handyjson.

xuyecan avatar xuyecan commented on May 5, 2024

Date没有默认实现HandyJSON协议,需要构造一个Transformer指定解析,或者选择一个HandyJSON提供的默认Transformer。如:

struct DateSample: HandyJSON {

    var aInt: Int?
    var date: Date?

    mutating func mapping(mapper: HelpingMapper) {
        mapper <<<
            self.date <-- DateTransform()
    }
}

let json = "{\"aInt\": \"2\", \"date\": 1484196879}"
let model = JSONDeserializer<DateSample>.deserializeFrom(json: json)!
print(model.date!)  // print: 2017-01-12 04:54:39 +0000
print(model.toJSON()!) // print: ["aInt": 2, "date": Optional(1484196879.0)]

HandyJSON还提供了以下的Date相关的Transformer:

  • DateTransform
  • DateFormatterTransform
  • ISO8601DateTransform
  • CustomDateFormatTransform

具体用法,可以点开对应文件看到该Transformer的实现。

from handyjson.

ZhangZihe avatar ZhangZihe commented on May 5, 2024

@xuyecan 如果能有一个全局的方式, 可以设置默认的时间解析方法, 那必是极好的

from handyjson.

codetalks-new avatar codetalks-new commented on May 5, 2024

@ZhangZihe 可以的.你在你自己的项目中,扩展 Date 使它实现 HandyJSONCustomTransformable 协议即可.
extension Date: HandyJSONCustomTransformable
然后你自己实现从 NSObject 到 Date 的转换.

比如测试代码中,有实现 URL 默认解析方式的示例测试代码.

https://github.com/alibaba/HandyJSON/blob/master/HandyJSONTests/CustomJSONTransformableTypeTest.swift

from handyjson.

ZhangZihe avatar ZhangZihe commented on May 5, 2024

@banxi1988 貌似HandyJSONCustomTransformable 并不在已发布的1.5.2版本之中

from handyjson.

codetalks-new avatar codetalks-new commented on May 5, 2024

@ZhangZihe 你可以直接在 Podfile 中配置 HandyJSON 指向 master

from handyjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.