<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I assume this issue affects other implementations of Encoder and Decoder as well. The most obvious way to represent a key with multiple types is an enum, but enums don’t have their conformances to Codable generated automatically. It’s possible to write your own, but it can get a bit ugly (e.g., <a href="https://gist.github.com/kenada/9acbd7200cff3590bda5e83118f62523" class="">https://gist.github.com/kenada/9acbd7200cff3590bda5e83118f62523</a>).</div><div class=""><br class=""></div><div class="">-- </div><div class="">Randy</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Jun 23, 2017, at 1:14 PM, Jon Shier via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""></div><div class="">Itai:</div><div class=""><br class=""></div><div class="">No need to apologize, I do appreciate the difficulties of designing this entire feature as quickly and completely as was required. </div><div class=""><br class=""></div><div class="">An intermediate JSON type would be a great fix, though most useful if it exists outside of JSONDecoder so it's useful for encoding as well. As long as it can be initialize from outside and decoded on its own, I think it could solve much of that issue. </div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Jo </div><div class=""><br class="">On Jun 23, 2017, at 12:54 PM, Itai Ferber <<a href="mailto:iferber@apple.com" class="">iferber@apple.com</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class="">
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8" class="">
<div style="font-family:sans-serif" class=""><div style="white-space:normal" class=""><p dir="auto" class="">Hi Jon,</p><p dir="auto" class="">I just joined this mailing list and have tried to catch up on the history of this thread, so please excuse me if I’ve missed something.</p><p dir="auto" class="">I’m sorry the Codable API at the moment does not answer your needs — you’re clearly not the only one who’s run into this, so let’s see how we can work together to make the API better for everyone.<br class="">
For one thing, in the case of grabbing a subtree of JSON as "unevaluated" or "unmapped" (as it appears to be in the metadata case), it should be fairly simple to add a <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">JSONDecoder.UnevaluatedJSON</code> type that will allow you to essentially decode that part of the tree as an <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">Any</code>. <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">JSONDecoder</code> would have knowledge of this type and would be able to return the subtree inside of it — you’d decode a property as <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">JSONDecoder.UnevaluatedJSON.self</code> and access the contents through <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">var value: Any?</code>, or something similar. This would be simple additive API, which although might not make it in the upcoming betas, should be fairly simple introduce. Would this solve that use case?</p><p dir="auto" class="">We’re also working on improving <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">NSISO8601DateFormatter</code>. I don’t think I saw it in any of your emails — what specific use case are you looking for that it doesn’t at the moment support?</p><p dir="auto" class="">— Itai</p><p dir="auto" class="">On 23 Jun 2017, at 9:24, Jon Shier via swift-users wrote:</p>
</div>
<div style="white-space:normal" class=""></div>
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px" class=""><div id="3C5DA499-CC76-4203-A2BF-ACD1E8FEF35B" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">David:<div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>I never called the design silly (though I think it’s inadequate for some important usage and makes some strange decisions), I was referring to the fact that the (apparent) official solution can’t actually decode all of the JSON people use. It’s the same reason I brought up NSISO8601DateFormatter. After years of using third party libraries or writing your own, limited, implementation, finally, finally there was an official solution from Apple. The official 8601 date formatter. Only, come to find out, it doesn’t actually handle all of 8601 and those third party libraries or custom implementations are still required if you venture outside supported scenarios. I’m concerned about the same thing happening here. Now, if JSONDecoder isn’t actually intended to serve as a general JSON parsing solution, which I think would be surprising to a lot people, then fair enough. Apple and the Swift team just need to be far more clear that that’s the case, rather letting everyone believe otherwise. And frankly, if that’s the case, I think a huge opportunity has been missed. At the same time, if / when an official solution for JSON parsing comes out, or an actual JSON representation, how will it interact with the previous implementation?</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>These concerns, and the general concerns I expressed during the evolution review (which still exist) aside, this is fixable, if the Swift team is interested in doing so. However, if the limitations of JSONDecoder aren’t even seen as limitations, or interest in fixing them aren’t there, then there’s little point to continuing the discussion. Something as simple as an additional decode(_ type: from: Any) on JSONDecoder would solve the issues with decoding partially deserialized blobs or representations from other APIs. Something to help represent Any in Codable types might be useful, though I recognize that there isn’t any way to currently differentiate between Codable types and those just used by JSON. </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>All of that said, my concerns mainly lie within the JSON realm. Codable works great for serialization to disk or other scenarios where I can just deal with the Data result and not have to worry about weakly typed intermediate results. I’ll certainly be using it everywhere I can. And I’m super happy that conformance is generated by the compiler rather than manually, like we had to do with Objective-C for over a decade. Even the JSON side is useful if I can control both sides of the API, which makes Swift on the server very powerful.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>So if I seem overly strident in my expression here it’s because I experience the pain of consuming poorly designed JSON in Swift on practically a daily basis and had hoped that a native implementation would alleviate that. That it doesn’t, for me and others, currently, is very disappointing. That the Swift team doesn’t seem to see the current limitations as important or at all is doubly so, since it seems like these issues will never be fixed. </div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 23, 2017, at 4:34 AM, David Hart <<a href="mailto:david@hartbit.com" class="">david@hartbit.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On 23 Jun 2017, at 03:45, Jon Shier via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>I’m sorry, are you complaining about my use of Codable instead of more precisely referring to the JSON endcode/decode functionality based on it in Foundation, or are you honestly trying to say that said functionality was never intended to be a general purpose JSON solution? If it’s not actually intended to handle all JSON you should probably call it something else.</div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Hi Jon,</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">First of all, I'd like to point out that I've found your tone to be quite rude. Calling the design of Codable, that has gotten a lot of work from Apple and swift-evolution, as silly is insulting and can leave people hurt. If you have found it lacking, please say so: we're all here to discuss any feedback people have had with Swift. But please do so with respect for the people and the work behind it.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Now, concerning Codable, I find its name quite apt. It was never intended to be used a full JSON parser but as a strongly-typed Swift equivalent of Objective-C's NSCoding, which is nothing more than a framework for serializing and deserializing types into different file formats.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">David.</div><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><div class=""><div class="">Jon<br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 22, 2017, at 9:42 PM, Greg Parker <<a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space;"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 22, 2017, at 6:00 PM, Jon Shier via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space;"><br class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>My main concern here is that, as Swift’s official JSON parsing method, Codable should be able to handle any JSON representation and use and it doesn’t.<span class="Apple-converted-space"> </span></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Is this true? Is Codable intended to be Swift's official JSON parsing system? Is Codable intended to be a general-purpose JSON parsing system? </div><div class=""><br class=""></div><div class="">My understanding was that Codable was designed to serialize Swift types, not to be able to import arbitrary JSON text into Swift nor to interoperate with every existing JSON API.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-- </div><div class="">Greg Parker <span class="Apple-converted-space"> </span><a href="mailto:gparker@apple.com" class="">gparker@apple.com</a> Runtime Wrangler</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></blockquote></div><br class=""></div></div></blockquote><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-users mailing list</span><br class=""><span class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span></div></blockquote></div></blockquote></div><br class=""></div></div></div></blockquote>
<div style="white-space:normal" class="">
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px" class="">
</blockquote><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px" class=""><p dir="auto" class="">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" style="color:#777" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></p>
</blockquote></div>
<div style="white-space:normal" class="">
</div>
</div>
</div></blockquote></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>