Comments (5)
Hi @buscarini! Thanks for the report and sorry about that! Do you have some code that illustrates the issue?
from swift-tagged.
Yes, I was preparing a pull request, but here you go:
func testDecodableCustomDates() {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom { decoder in
let seconds = try decoder.singleValueContainer().decode(Int.self)
return Date(timeIntervalSince1970: TimeInterval(seconds))
}
XCTAssertEqual(
[Date(timeIntervalSince1970: 1)],
try decoder.decode([Date].self, from: Data("[1]".utf8))
)
XCTAssertEqual(
[Tagged<Tag, Date>(rawValue: Date(timeIntervalSince1970: 1))],
try decoder.decode([Tagged<Tag, Date>].self, from: Data("[1]".utf8))
)
}
Unfortunately, I've found out that changing this back makes another test fail. In that case wrapped optionals fail to decode if the json value is null. I haven't been able to find a solution to both issues for now.
from swift-tagged.
@buscarini If I define decode
as the following, both appear to work:
extension Tagged: Decodable where RawValue: Decodable {
public init(from decoder: Decoder) throws {
do {
self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self))
} catch {
self.init(rawValue: try .init(from: decoder))
}
}
}
I'm not sure what the repercussions are, and it does indeed feel like a shortcoming or bug of Decodable
. Any thoughts? Would we need to handle Encodable
in a similar way?
from swift-tagged.
:D Funny, I just made a pull request with the same solution. I think this should work fine, and I think it's the only reasonable solution for now. I even checked swift source code, and it seems initializing the date with the decoder directly doesn't hit the date decoding strategy, as this is done by the decoder before getting to the actual type. And solving the optional issue with the singleValueContainer would require to have different code depending on the type, which shouldn't even be possible in a generic function.
As for the encodable protocol, I think it's going to have the same issue. I'll try to get some free time to put that in the pull request too.
from swift-tagged.
It seems that the encoding strategy doesn't require this, and just reverting the commit is enough to get it working.
from swift-tagged.
Related Issues (15)
- Issue building with Carthage when targeting older iOS SDK
- Are consumers able to override default Codable conditional conformances? HOT 2
- Add Info.plist
- Unused dependency in Package.swift? HOT 6
- Deploy to CocoaPods HOT 2
- Decodable issue when using Tagged type as key in Dictionary HOT 1
- Does `ExpressiblebyArrayLiteral` code still need to be commented out? HOT 2
- Has anyone been able to get this library working with SQLite.swift? HOT 6
- New Release? HOT 2
- [Remove me]
- Unexpected behavior HOT 5
- CodingKeyRepresentable Conformance HOT 1
- Support `SignedInteger` where `RawValue: SignedInteger`
- Conform to EntityIdentifierConvertible HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-tagged.