-
-
Save minacle/9ca00dd95732486c3f14faed2d3740dd to your computer and use it in GitHub Desktop.
This file contains 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
import AnyCodable | |
import Foundation | |
let jsonEncoder = JSONEncoder() | |
let plistEncoder = PropertyListEncoder() | |
func testEncode<E>(_ e: E) | |
where E: Encodable { | |
if !(e is AnyEncodable) { | |
print() | |
testEncode(AnyEncodable(e)) | |
} | |
print() | |
if let e = e as? AnyEncodable { | |
print("\(type(of: e))<\(type(of: e.value))>") | |
} | |
else { | |
print(String(describing: type(of: e))) | |
} | |
// JSON | |
do { | |
_ = try jsonEncoder.encode(e) | |
print("[JSON] PASS.") | |
} | |
catch { | |
print("[JSON] ERROR.") | |
} | |
// PLIST | |
do { | |
_ = try plistEncoder.encode(e) | |
print("[PLIST] PASS.") | |
} | |
catch { | |
print("[PLIST] ERROR.") | |
} | |
} | |
// Array<Int> | |
testEncode([1, 256, 3, 2, 42, 7]) | |
// Calendar | |
testEncode(Calendar.current) | |
// CharacterSet | |
testEncode(CharacterSet.urlQueryAllowed) | |
// Data | |
testEncode("Hello 반가워요 初めまして ביי 👩👩👧".data(using: .utf8)!) | |
// Date | |
testEncode(Date()) | |
// DateInterval | |
testEncode(DateInterval(start: .init(timeIntervalSince1970: 0), end: .init())) | |
// Decimal | |
testEncode(Decimal(integerLiteral: 42)) | |
// Dictionary<String, Int> | |
testEncode(["A": 1, "b": 256, "B": 3, "F": 2, "D": 42, "c": 7]) | |
// IndexPath | |
testEncode(IndexPath(indexes: [8, 42])) | |
// IndexSet | |
testEncode(IndexSet([8, 42, 7, 3, 256])) | |
// Locale | |
testEncode(Locale.current) | |
// Set | |
testEncode(Set([1, 256, 3, 2, 42, 7])) | |
// String | |
testEncode("Hello 어서오세요 初めまして ביי 👩👩👧") | |
// TimeZone | |
testEncode(TimeZone.current) | |
// URL | |
testEncode(URL(string: "https://example.com")!) | |
// UUID | |
testEncode(UUID()) | |
print() | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment