Skip to content
Permalink
Browse files

Use self as the decoder when decoding a ByteBuffer (#1221)

Motivation:

JSONDecoder settings should be respected but were not.

Modifications:

This allows actually configuring the `JSONDecoder` instead of being stuck with the defaults.

Result:

Settings respected.
  • Loading branch information...
gwynne authored and weissi committed Nov 6, 2019
1 parent fbe0c8a commit 1a9a543b6bb4efb844be09daa55deee35881e192
@@ -104,6 +104,7 @@ extension JSONDecoder {
/// - returns: The decoded object.
public func decode<T: Decodable>(_ type: T.Type, from buffer: ByteBuffer) throws -> T {
return try buffer.getJSONDecodable(T.self,
decoder: self,
at: buffer.readerIndex,
length: buffer.readableBytes)! // must work, enough readable bytes
}
@@ -36,6 +36,8 @@ extension CodableByteBufferTest {
("testReadWriteJSONDecodableWorks", testReadWriteJSONDecodableWorks),
("testGetSetJSONDecodableWorks", testGetSetJSONDecodableWorks),
("testFailingReadsDoNotChangeReaderIndex", testFailingReadsDoNotChangeReaderIndex),
("testCustomEncoderIsRespected", testCustomEncoderIsRespected),
("testCustomDecoderIsRespected", testCustomDecoderIsRespected),
]
}
}
@@ -156,6 +156,38 @@ class CodableByteBufferTest: XCTestCase {
}
XCTAssertNoThrow(try self.buffer.readJSONDecodable(StringAndInt.self, length: writtenBytes ?? -1))
}

func testCustomEncoderIsRespected() {
let expectedDate = Date(timeIntervalSinceReferenceDate: 86400)
let strategyExpectation = XCTestExpectation(description: "Custom encoding strategy invoked")
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .custom({ date, encoder in
var container = encoder.singleValueContainer()
try container.encode(date.timeIntervalSinceReferenceDate)
strategyExpectation.fulfill()
})
XCTAssertNoThrow(try encoder.encode(["date": expectedDate], into: &self.buffer))
XCTAssertEqual(XCTWaiter().wait(for: [strategyExpectation], timeout: 0.0), .completed)
}

func testCustomDecoderIsRespected() {
let expectedDate = Date(timeIntervalSinceReferenceDate: 86400)
let strategyExpectation = XCTestExpectation(description: "Custom decoding strategy invoked")
let encoder = JSONEncoder()
let decoder = JSONDecoder()
encoder.dateEncodingStrategy = .custom({ date, encoder in
var container = encoder.singleValueContainer()
try container.encode(date.timeIntervalSinceReferenceDate)
})
decoder.dateDecodingStrategy = .custom({ decoder in
strategyExpectation.fulfill()
let container = try decoder.singleValueContainer()
return Date(timeIntervalSinceReferenceDate: try container.decode(Double.self))
})
XCTAssertNoThrow(try encoder.encode(["date": expectedDate], into: &self.buffer))
XCTAssertNoThrow(XCTAssertEqual(["date": expectedDate], try decoder.decode(Dictionary<String, Date>.self, from: self.buffer)))
XCTAssertEqual(XCTWaiter().wait(for: [strategyExpectation], timeout: 0.0), .completed)
}
}

struct StringAndInt: Codable, Equatable {

0 comments on commit 1a9a543

Please sign in to comment.
You can’t perform that action at this time.