Coder Social home page Coder Social logo

Comments (6)

xuyecan avatar xuyecan commented on May 5, 2024

能具体描述一下场景吗?文档里T只是一个指代,指代已经支持HandyJSON的类型。

from handyjson.

shiqianren avatar shiqianren commented on May 5, 2024

我想封装一个base 里面包含(code,msg,data),data是任意类型的,我继承了handyJson,请问怎么处理这个data

from handyjson.

xuyecan avatar xuyecan commented on May 5, 2024

如果你是指需要支持泛型的话,你可以这么声明:

class Base<T: HandyJSON> {
    var code: Int?
    var msg: String?
    var data: T?
}

这样的话,你在实例化Base时,需要指明这个T的具体类型,这个类型必须是实现了HandyJSON协议的。

from handyjson.

xuyecan avatar xuyecan commented on May 5, 2024

如果data字段需要到运行时才知道是什么类型,那只能够用specify函数来自定义解析了。
参考:https://github.com/alibaba/HandyJSON/blob/master/README_cn.md#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%A3%E6%9E%90%E8%A7%84%E5%88%99

from handyjson.

shiqianren avatar shiqianren commented on May 5, 2024

class Base<T: HandyJSON> {
var code: Int?
var msg: String?
var data: T?
}
这个时候好像不能用JSONDeserializer.deserializeFrom()处理了data数据了
我是想用这个base去接受服务器传过来的数据,
再在对应的Model层次去处理这个data

from handyjson.

xuyecan avatar xuyecan commented on May 5, 2024

不好意思今天才看到这条信息。。我测试是没问题的。参考:

class BaseResponse<T: HandyJSON>: HandyJSON {
    var code: Int?
    var data: T?

    required init() {}
}

class Sample: HandyJSON {
    var id: Int?

    required init() {}
}

let sample = Sample()
sample.id = 2
let resp = BaseResponse<Sample>()
resp.code = 200
resp.data = sample

let jsonString = resp.toJSONString()!

print(jsonString)
if let mappedObject = JSONDeserializer<BaseResponse<Sample>>.deserializeFrom(json: jsonString) {
    print(mappedObject.data?.id)
}

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.