-
-
Save itaiferber/3a504c6abdc7d4e390815e0866a83782 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 Foundation | |
struct AnyEncodable: Encodable { | |
let value: Encodable | |
init(_ value: Encodable) { | |
self.value = value | |
} | |
func encode(to encoder: Encoder) throws { | |
try value.encode(to: encoder) | |
} | |
} | |
struct Model<T : Encodable> : Encodable { | |
let value: T | |
init(_ value: T) { | |
self.value = value | |
} | |
} | |
let formatter = DateFormatter() | |
formatter.dateFormat = "yyyy-MM-dd" | |
let encoder = JSONEncoder() | |
encoder.dateEncodingStrategy = .formatted(formatter) | |
let now = Date() | |
let d1 = try encoder.encode(Model(now)) | |
let d2 = try encoder.encode(Model(AnyEncodable(now))) | |
print(String(data: d1, encoding: .utf8)!) | |
print(String(data: d2, encoding: .utf8)!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment