diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 0057f4a6f..ccdcf4032 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -8,7 +8,7 @@ import SQLite3 #endif /// DatabaseDateComponents reads and stores DateComponents in the database. -public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnConvertible { +public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnConvertible, Codable { /// The available formats for reading and storing date components. public enum Format: String { @@ -159,4 +159,30 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC return SQLiteDateParser().components(from: string) } + + // MARK: - Codable adoption + + + /// Creates a new instance by decoding from the given decoder. + /// + /// - parameters: + /// - decoder: The decoder to read data from. + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let stringValue = try container.decode(String.self) + guard let decodedValue = DatabaseDateComponents.fromDatabaseValue(stringValue.databaseValue) else { + throw DecodingError.dataCorruptedError(in: container, + debugDescription: "Unable to initialise databaseDateComponent") + } + self = decodedValue + } + + /// Encodes this value into the given encoder. + /// + /// - parameters: + /// - encoder: The encoder to write data to. + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(String.fromDatabaseValue(databaseValue)!) + } } diff --git a/Tests/GRDBTests/FoundationDateComponentsTests.swift b/Tests/GRDBTests/FoundationDateComponentsTests.swift index e49b05389..696412109 100644 --- a/Tests/GRDBTests/FoundationDateComponentsTests.swift +++ b/Tests/GRDBTests/FoundationDateComponentsTests.swift @@ -506,4 +506,27 @@ class FoundationDateComponentsTests : GRDBTestCase { let databaseDateComponents = DatabaseDateComponents.fromDatabaseValue("foo".databaseValue) XCTAssertTrue(databaseDateComponents == nil) } + + func testJSONEncodingOfDatabaseDateComponents() { + let year = 2018 + let month = 12 + let day = 31 + let dbDateComponents = DatabaseDateComponents( + DateComponents(year: year, month: month, day: day, hour: nil, minute: nil, second: nil, nanosecond: nil), + format: .YMD) + let encoded = try! JSONEncoder().encode(dbDateComponents) + XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "\"\(year)-\(month)-\(day)\"") + } + + func testJSONDecodingOfDatabaseDateComponents() { + let year = 2018 + let month = 12 + let day = 31 + let dbDateComponents = DatabaseDateComponents( + DateComponents(year: year, month: month, day: day, hour: nil, minute: nil, second: nil, nanosecond: nil), + format: .YMD) + let json = "\"\(year)-\(month)-\(day)\"".data(using: .utf8)! + let decodedDatabaseDateComponents = try! JSONDecoder().decode(DatabaseDateComponents.self, from: json) + XCTAssertEqual(decodedDatabaseDateComponents.dateComponents, dbDateComponents.dateComponents) + } }