Skip to content

Commit

Permalink
Merge pull request #24 from alickbass/json-conform-to-json-protocols
Browse files Browse the repository at this point in the history
Extend JSON to conform to JSONInitializable and JSONRepresentable
  • Loading branch information
Oleksii Dykan committed Mar 22, 2017
2 parents 0182b76 + 04cf5c9 commit 8786f4b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions SwiftyJSONModel/JSONTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public protocol JSONRepresentable {
var jsonValue: JSON { get }
}

extension JSON: JSONInitializable, JSONRepresentable {
public init(json: JSON) { self = json }
public var jsonValue: JSON { return self }
}

extension String: JSONInitializable, JSONRepresentable {
public init(json: JSON) throws { self = try json.value() }
public var jsonValue: JSON { return JSON(string: self) }
Expand Down
7 changes: 7 additions & 0 deletions SwiftyJSONModelTests/JSONExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ class JSONExtensionTests: XCTestCase {
XCTAssertEqual(try! JSON(dictionary: json).dictionaryValue(), json)
}

func testJSONConformanceToJSONProtocols() {
let json: JSON = ["Some"]

XCTAssertEqual(JSON(json: json), json)
XCTAssertEqual(json.jsonValue, json)
}

}

0 comments on commit 8786f4b

Please sign in to comment.