Skip to content

Commit

Permalink
Use self as the decoder when decoding a ByteBuffer (#1221)
Browse files Browse the repository at this point in the history
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 1a9a543
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/NIOFoundationCompat/Codable+ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extension CodableByteBufferTest {
("testReadWriteJSONDecodableWorks", testReadWriteJSONDecodableWorks),
("testGetSetJSONDecodableWorks", testGetSetJSONDecodableWorks),
("testFailingReadsDoNotChangeReaderIndex", testFailingReadsDoNotChangeReaderIndex),
("testCustomEncoderIsRespected", testCustomEncoderIsRespected),
("testCustomDecoderIsRespected", testCustomDecoderIsRespected),
]
}
}
Expand Down
32 changes: 32 additions & 0 deletions Tests/NIOFoundationCompatTests/Codable+ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1a9a543

Please sign in to comment.