This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum JSONValue { | |
| case string(String) | |
| case int(Int) | |
| case double(Double) | |
| case bool(Bool) | |
| case object([String: JSONValue]) | |
| case array([JSONValue]) | |
| } | |
| extension JSONValue: Encodable { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func parse(_ json: String) -> Any? { | |
| return Bool(json) ?? Int(json) ?? Double(json) ?? parseString(json) | |
| } | |
| private func parseString(_ text: String) -> String? { | |
| switch (text.first, text.last) { | |
| case ("\""?, "\""?): return "" | |
| default: return nil | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func parse(_ json: String) -> Any? { | |
| return Bool(json) ?? Int(json) ?? Double(json) ?? "" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func testEmptyString() { | |
| assert("\"\"", parsesTo: "") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func assert<T: Equatable>(_ json: String, parsesTo target: T) { | |
| guard let parsed = parser.parse(json) as? T else { XCTFail("Parsed item \(json) is not of type \(T.self)"); return } | |
| XCTAssertEqual(parsed, target) | |
| } | |
| func assertNil(_ json: String) { | |
| XCTAssertNil(parser.parse(json)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class JSONParser { | |
| func parse(_ json: String) -> Any? { | |
| return Bool(json) ?? Int(json) ?? Double(json) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func testDouble() { | |
| XCTAssertEqual(parser.parse("0.0") as! Double, 0) | |
| } |