Skip to content

Commit 11d3a41

Browse files
committed
Date Encoding
1 parent 08a6ad5 commit 11d3a41

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Sources/KeyValueEncoder.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ extension KeyValueEncoder {
107107

108108
extension KeyValueEncoder.NilEncodingStrategy {
109109

110-
func isNull(_ value: Any?) -> Bool {
110+
public func isNull(_ value: Any?) -> Bool {
111111
guard let value else { return true }
112112
return isNull(value)
113113
}
114114

115-
func isNull(_ value: Any) -> Bool {
115+
public func isNull(_ value: Any) -> Bool {
116116
switch self {
117117
case .removed:
118118
return Self.isOptionalNone(value)
@@ -187,8 +187,11 @@ private extension KeyValueEncoder {
187187
}
188188

189189
func encodeToValue<T>(_ value: T) throws -> EncodedValue where T: Encodable {
190-
try value.encode(to: self)
191-
return try getEncodedValue()
190+
guard let encoded = EncodedValue(value) else {
191+
try value.encode(to: self)
192+
return try getEncodedValue()
193+
}
194+
return encoded
192195
}
193196
}
194197
}

Tests/KeyValueDecoderTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,11 @@ final class KeyValueDecoderTests: XCTestCase {
738738
try KeyValueDecoder.makePlistCompatible().decode([Int?].self, from: plistAny),
739739
[1, 2, Int?.none, 4]
740740
)
741+
742+
XCTAssertEqual(
743+
try KeyValueDecoder.makePlistCompatible().decode(String?.self, from: "$null"),
744+
nil
745+
)
741746
}
742747

743748
func testJSONCompatibleDecoder() throws {

Tests/KeyValueEncoderTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,14 @@ final class KeyValueEncodedTests: XCTestCase {
645645
)
646646
}
647647

648+
func testEncoder_Encodes_Dates() {
649+
let date = Date.now
650+
XCTAssertEqual(
651+
try KeyValueEncoder().encode(date) as? Date,
652+
date
653+
)
654+
}
655+
648656
func testJSONCompatibleEncoder() throws {
649657
let keyValueAny = try KeyValueEncoder.makeJSONCompatible().encode([1, 2, Int?.none, 4])
650658
XCTAssertEqual(

0 commit comments

Comments
 (0)