-
Notifications
You must be signed in to change notification settings - Fork 6
add toDictionary, toJSONString #2
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
a044722
7df8bfe
770349a
c07dd44
ef78616
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,31 @@ public extension ObservableType where E == String { | |
| .map(type, using: decoder) | ||
| } | ||
| } | ||
|
|
||
| public extension ObservableType where E == [String: Any] { | ||
| public func map<T>(_ type: T.Type, using decoder: JSONDecoder? = nil) -> Observable<T> where T: Decodable { | ||
| return self | ||
| .map { dict in try JSONSerialization.data(withJSONObject: dict) } | ||
| .map(type, using: decoder) | ||
| } | ||
| } | ||
|
|
||
| public extension ObservableType where E: Encodable { | ||
| public func toDictionary() -> Observable<[String:Any]> { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return self.map { encodable -> [String: Any] in | ||
| let data = try JSONEncoder().encode(encodable) | ||
| let dictionary = try JSONSerialization.jsonObject(with: data) as? [String: Any] | ||
| return dictionary ?? [:] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This default value can cause an unexpected result. I think we should treat a casting failure as an error.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point! |
||
| } | ||
| } | ||
| } | ||
|
|
||
| public extension ObservableType where E: Encodable { | ||
| public func toJSONString(_ encoding: String.Encoding = .utf8) -> Observable<String> { | ||
| return self.map { encodable -> String in | ||
| let data = try JSONEncoder().encode(encodable) | ||
| let json = String(data: data, encoding: encoding) | ||
| return json ?? "{}" | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use default value here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it