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
This is actually done on purpose — overall, it would be significantly more surprising to people if {{URL}}s they got from potentially arbitrary sources encoded as a dictionary instead of as a string. We encode {{URL}}s as absolute strings because most consumers of JSON would expect that; unfortunately, it means that we lose the relativeness.
We can potentially add a strategy to tell JSONEncoder to preserve the structure as-is for round-tripping.
The Linux behavior can actually be considered a bug; it will be fixed once PR-11315 is pulled into a Swift release.
@itaiferber Thanks for the explanation. And +1 on the new option; I could see this being used for debug logging in JSON format, in which case a lossless archival of the object state might be beneficial.
Also, for future reference, are there any other native/Foundation types that aren't losslessly encoded by JSONEncoder?
ylin (JIRA User) There shouldn't be, no. All other types which have multiple potential representations encode as a dictionary; URL is just unique in that most consumers of the JSON would be expecting a string, so we do this for maximum compatibility.
Environment
macOS, Swift 4 08-21 snapshot
Additional Detail from JIRA
md5: e6d42a434f747ae22d710ada3ecdf7b6
Issue Description:
While playing around JSONEncoder/JSONDecoder, I found that the `URL` type is lossy on encode -> decode.
Example:
The same URL is read back losslessly using the `PropertyList` version, however:
Even more oddly, on Ubuntu 14.04 the same 08-21 snapshot produces the correct result:
The text was updated successfully, but these errors were encountered: