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

提一个建议,望改进 #109

Closed
Mosaics opened this issue Mar 28, 2017 · 12 comments
Closed

提一个建议,望改进 #109

Mosaics opened this issue Mar 28, 2017 · 12 comments

Comments

@Mosaics
Copy link

Mosaics commented Mar 28, 2017

大家都知道,现在网络请求的数据多数以json格式解析,json格式有个好处是在控制台打印出来后有结构性,结构层次一清二楚,虽然也可以将请求的json格式转换成json字符串,但是通过字符串转换相当的不直观,不知道能不能以json格式转换?我吃了几次亏,后台接口写的字段和实际的并不相符,我建模型的时候字段老是对不上,还在傻傻的去找错误!实际上json字符串一长串,根本就不好找。

归根结底,现在只支持jsonString -> model, 并不支持json -> model
其实我觉得提供额外方法(最偷懒的做法)也快,直接将返回的json序列化成字符串再调用现有的接口即可

@xuyecan
Copy link
Contributor

xuyecan commented Apr 11, 2017

@Mosaics 你意思是,当拿到网络层返回的数据的时候,已经是JSON格式了,并不是字符串,对吧?那请问下,这个JSON格式,具体是代码中的什么类型呢?

如Foundation中提供的JSONSerialization.jsonObject(with:options:)函数,实际上,它的返回值实际是NSArray或者NSDictionary,是可以转换成NSArray/NSDictionary然后通过HandyJSON现有的API进行反序列化的。

@Mosaics
Copy link
Author

Mosaics commented Apr 11, 2017 via email

@xuyecan
Copy link
Contributor

xuyecan commented Apr 11, 2017

@Mosaics 我还是不是特别理解。“所以我才想能不能你这边的库层级提供一个api通过json格式序列化”这句话里指的‘JSON格式’具体是什么格式?在代码里,它的类型是什么呢?

@Mosaics
Copy link
Author

Mosaics commented Apr 11, 2017 via email

@Mosaics
Copy link
Author

Mosaics commented Apr 11, 2017 via email

@Mosaics
Copy link
Author

Mosaics commented Apr 11, 2017 via email

@xuyecan
Copy link
Contributor

xuyecan commented Apr 11, 2017

@Mosaics 我明白你意思了。但是,正如‘Alamofire 请求返回的 responseJSON方法的json格式’,它的responseJSON()中拿到的数据,实际正是从JSONSerialization.jsonObject(with:options:)得到的(Alamofire文档明确提到它使用NSJSONSerialization做JSON反序列化)。它是Any类型,实际使用时,需要转换到NSDictionary或者Dictionary<String, Any>,而HandyJSON是有接口直接接受这两个类型的。

所以这就是我要说的,其实没必要先转换到String,直接做个cast就可以传入HandyJSON现有的接口了。

@Mosaics
Copy link
Author

Mosaics commented Apr 11, 2017 via email

@xuyecan
Copy link
Contributor

xuyecan commented Apr 11, 2017

所以现在不需要修改了呀。Foundation提供的NSJSON相关的方法,在提到JSON格式时,指的东西本质上一般是NSDictionary(虽然JSON顶层对象也允许NSArray)。现在的接口是支持的。

public static func deserialize(from dict: NSDictionary?, designatedPath: String? = nil) -> Self?

@Mosaics
Copy link
Author

Mosaics commented Apr 11, 2017 via email

@xuyecan
Copy link
Contributor

xuyecan commented Apr 11, 2017

@Mosaics 但你想一下,如果要提供一个FromJSON接口,接口形式是怎样的呢?

deserialize(from json: NSDictionary?)
还是 deserialize(from json: Any?)呢?

这两个接口,不是都和 public static func deserialize(from dict: NSDictionary?, designatedPath: String? = nil) -> Self? 本质上是重合的吗?

@xuyecan
Copy link
Contributor

xuyecan commented Apr 12, 2017

如上述,暂不考虑添加此接口。

@xuyecan xuyecan closed this as completed Apr 12, 2017
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