Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/RxCodable/Maybe+RxCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ public extension PrimitiveSequenceType where TraitType == MaybeTrait, ElementTyp
}
}

public extension PrimitiveSequenceType where TraitType == MaybeTrait, ElementType == [String: Any] {
public func map<T>(_ type: T.Type, using decoder: JSONDecoder? = nil) -> PrimitiveSequence<TraitType, T> where T: Decodable {
return self
.map { dict in try JSONSerialization.data(withJSONObject: dict) }
.map(type, using: decoder)
}
}

public extension PrimitiveSequenceType where TraitType == MaybeTrait, ElementType: Encodable {
public func toDictionary() -> PrimitiveSequence<TraitType, [String:Any]> {
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 ?? [:]
}
}
}

public extension PrimitiveSequenceType where TraitType == MaybeTrait, ElementType: Encodable {
public func toJSONString(_ encoding: String.Encoding = .utf8) -> PrimitiveSequence<TraitType, String> {
return self.map { encodable -> String in
let data = try JSONEncoder().encode(encodable)
let json = String(data: data, encoding: encoding)
return json ?? "{}"
Copy link
Copy Markdown
Owner

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

}
}
}
28 changes: 28 additions & 0 deletions Sources/RxCodable/ObservableType+RxCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]> {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. How about renaming it to mapDictionary()?
  2. I think we need a parameter of JSONEncoder. map() takes an instance of JSONDecoder as a parameter.
  3. Could you please append a whitespace after a colon?
    - [String:Any]
    + [String: Any]
  4. How do you think of supporting mapping json array?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. like toBlocking(), toArray(). toXXX() feels extract some data. toDictionary() is more clear for me, aren't you? ( I understand your side, changing data should start with map... )
  2. Yes. It seems necessary
  3. my mistake.
  4. why not

Copy link
Copy Markdown
Owner

@devxoul devxoul Jan 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It's not about extracting data. It's obviously mapping data. I think it would be better to rename it to mapDictionary().
  2. Could you write that one?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. then toJSONString() needs rename to mapJSONString(), too?

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 ?? [:]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point!
access empty dictionary makes crash.. it would be better throw error

}
}
}

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 ?? "{}"
}
}
}
28 changes: 28 additions & 0 deletions Sources/RxCodable/Single+RxCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ public extension PrimitiveSequenceType where TraitType == SingleTrait, ElementTy
.map(type, using: decoder)
}
}

public extension PrimitiveSequenceType where TraitType == SingleTrait, ElementType == [String: Any] {
public func map<T>(_ type: T.Type, using decoder: JSONDecoder? = nil) -> PrimitiveSequence<TraitType, T> where T: Decodable {
return self
.map { dict in try JSONSerialization.data(withJSONObject: dict) }
.map(type, using: decoder)
}
}

public extension PrimitiveSequenceType where TraitType == SingleTrait, ElementType: Encodable {
public func toDictionary() -> PrimitiveSequence<TraitType, [String:Any]> {
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 ?? [:]
}
}
}

public extension PrimitiveSequenceType where TraitType == SingleTrait, ElementType: Encodable {
public func toJSONString(_ encoding: String.Encoding = .utf8) -> PrimitiveSequence<TraitType, String> {
return self.map { encodable -> String in
let data = try JSONEncoder().encode(encodable)
let json = String(data: data, encoding: encoding)
return json ?? "{}"
}
}
}
54 changes: 54 additions & 0 deletions Tests/RxCodableTests/RxCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,58 @@ class RxCodableTests: XCTestCase {
XCTAssertEqual(try Single.just(jsonString).map([User].self).toBlocking().first()!, users)
XCTAssertEqual(try Maybe.just(jsonString).map([User].self).toBlocking().first()!, users)
}

func testMapCodableFromDictionary() {
let dictionary:[String:Any] = [
"id": 123,
"name": "iamchiwon",
]
let user = User(id: 123, name: "iamchiwon")
XCTAssertEqual(try Observable.just(dictionary).map(User.self).toBlocking().first()!, user)
XCTAssertEqual(try Single.just(dictionary).map(User.self).toBlocking().first()!, user)
XCTAssertEqual(try Maybe.just(dictionary).map(User.self).toBlocking().first()!, user)
}

func testMapCodableToDictionary() {
let dictionary:[String:Any] = [
"id": 123,
"name": "iamchiwon",
]
let user = User(id: 123, name: "iamchiwon")
XCTAssertEqual(try Observable.just(user).toDictionary().toBlocking().first()! as NSObject, dictionary as NSObject)
XCTAssertEqual(try Single.just(user).toDictionary().toBlocking().first()! as NSObject, dictionary as NSObject)
XCTAssertEqual(try Maybe.just(user).toDictionary().toBlocking().first()! as NSObject, dictionary as NSObject)
}

func testMapCodableToJSONString() {
let jsonString = """
{
"id": 123,
"name": "iamchiwon"
}
"""
let user = User(id: 123, name: "iamchiwon")
XCTAssertEqual(try Observable.just(user).toJSONString().toBlocking().first()!,
try Observable.just(jsonString).map(User.self).toJSONString().toBlocking().first()!)
XCTAssertEqual(try Single.just(user).toJSONString().toBlocking().first()!,
try Single.just(jsonString).map(User.self).toJSONString().toBlocking().first()!)
XCTAssertEqual(try Maybe.just(user).toJSONString().toBlocking().first()!,
try Maybe.just(jsonString).map(User.self).toJSONString().toBlocking().first()!)
}

func testMapCodableFromJSONStringToDictionary() {
let jsonString = """
{
"id": 123,
"name": "iamchiwon"
}
"""
let dictionary:[String:Any] = [
"id": 123,
"name": "iamchiwon",
]
XCTAssertEqual(try Observable.just(jsonString).map(User.self).toDictionary().toBlocking().first()! as NSObject, dictionary as NSObject)
XCTAssertEqual(try Single.just(jsonString).map(User.self).toDictionary().toBlocking().first()! as NSObject, dictionary as NSObject)
XCTAssertEqual(try Maybe.just(jsonString).map(User.self).toDictionary().toBlocking().first()! as NSObject, dictionary as NSObject)
}
}