You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA
Votes
0
Component/s
Foundation
Labels
Bug
Assignee
None
Priority
Medium
md5: 474c771f2a7abc910ab783c0bdc3cf2c
Issue Description:
Swift's URL type tries to avoid letting you create an empty URL, but you can still do it with NSURL(string: "")! as URL (personally, I wish URL supported the empty URL case natively). Such an empty URL can be encoded with JSONEncoder, but attempting to decode the results throws a decoding error. This is surprising.
import Foundation
letdata=tryJSONEncoder().encode(["url":NSURL(string:"")! asURL])letdict=tryJSONDecoder().decode(Dictionary<String,URL>.self, from: data)
Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "url", intValue: nil)], debugDescription: "Invalid U
RL string.", underlyingError: nil)): file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1001.8.63.13/swift/stdlib/public/core/ErrorType.swift, line 200
The text was updated successfully, but these errors were encountered:
We specifically decided that `URL` shouldn't support the empty string (that's why it's different than `NSURL`), because it brought a lot of complications to the behavior that we didn't like.
I think it is reasonable that JSONDecoder should treat the empty string as an invalid value when it finds it, for consistency.
Environment
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA
md5: 474c771f2a7abc910ab783c0bdc3cf2c
Issue Description:
Swift's
URL
type tries to avoid letting you create an empty URL, but you can still do it withNSURL(string: "")! as URL
(personally, I wishURL
supported the empty URL case natively). Such an empty URL can be encoded withJSONEncoder
, but attempting to decode the results throws a decoding error. This is surprising.The text was updated successfully, but these errors were encountered: