<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Goffredo,<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Apr 26, 2017, at 2:00 PM, Goffredo Marocchi <<a href="mailto:panajev@gmail.com" class="">panajev@gmail.com</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="">Hello Itai,</div><div class=""><br class=""></div><div class="">Sorry for the confusion, but I understood that the following</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><i class="">To answer your second question, the reason is that using the protocol implies that all encoders and decoders must support anything that conforms to that protocol. We’re not sure this is a reasonable requirement. Many formats do not have any kind of support for arbitrary size integers, for example. Therefore, we felt it was best to limit it to a set of concrete types.</i></span><br class=""></blockquote><div class=""><br class=""></div><div class="">meant it would actually hinder that kind of transformation or make it more difficult to write custom decoders and encoders. Sorry if I misunderstood that.</div><div class=""><br class=""></div><div class="">One follow up question: what would happen if inside the JSON mock object you posted I were to remove the 'address' key (in terms of the produced object and how to access its inner properties)? </div><div class=""><br class=""></div><div class="">What would happen if I remove the 'name' one or better if I add another key to the JSON object?</div></div></div></div></blockquote><div><font face="Menlo" class="">Codable</font> conformance is derived by default to require that all non-optional properties be initialized. This means that if you have a non-optional property <font face="Menlo" class="">address: Address</font> but there is no <font face="Menlo" class="">address</font> key in the JSON payload you're decoding from, it will throw an error to indicate that the key was not found.</div><div>On the flip side, if the JSON payload has information in it which your type does not have (e.g. if there is an <font face="Menlo" class="">address</font> in the JSON, but your <font face="Menlo" class="">Person</font> just has <font face="Menlo" class="">name</font>), the extra data is ignored.</div><div><br class=""></div><div>This, however, is just in the default, derived conformance. For more complex cases, you can always provide your own <font face="Menlo" class="">init(from:)</font> and <font face="Menlo" class="">encode(to:)</font> to do custom decoding. If you have a property which may or may not be in the JSON, you can always <font face="Menlo" class="">decodeIfPresent</font>, which will return <font face="Menlo" class="">nil</font> if the key or value was not found.</div><div>If you need to access sub-objects in the JSON data which do not map to your properties 1-to-1, e.g. your payload looks like <font face="Menlo" class="">{"name": "John Doe", "address": { "street": "1 Infinite Loop", ... } }</font>, but your type looks like</div><div><pre class=" language-swift" style="-webkit-print-color-adjust: exact; margin-top: 0.5em; margin-bottom: 0.5em; background-color: rgb(245, 242, 240); border: 1px solid rgb(204, 204, 204); font-size: 13px; line-height: 1.5; overflow: auto; padding: 1em; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; text-shadow: white 0px 1px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; white-space: pre-wrap; word-break: normal; word-wrap: normal; tab-size: 4; -webkit-hyphens: none;"><code class=" language-swift" style="-webkit-print-color-adjust: exact; margin: 0px; padding: 0px; border: none; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-image: none; text-shadow: white 0px 1px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; tab-size: 4; -webkit-hyphens: none;"><span class="token keyword" style="-webkit-print-color-adjust: exact; color: rgb(0, 119, 170);">struct</span> <span class="token builtin" style="-webkit-print-color-adjust: exact; color: rgb(102, 153, 0);">Person</span> <span class="token punctuation" style="-webkit-print-color-adjust: exact; color: rgb(153, 153, 153);">{</span>
<span class="token keyword" style="-webkit-print-color-adjust: exact; color: rgb(0, 119, 170);">let</span> name<span class="token punctuation" style="-webkit-print-color-adjust: exact; color: rgb(153, 153, 153);">:</span> <span class="token builtin" style="-webkit-print-color-adjust: exact; color: rgb(102, 153, 0);">String</span>
<span class="token keyword" style="-webkit-print-color-adjust: exact; color: rgb(0, 119, 170);">let</span> street<span class="token punctuation" style="-webkit-print-color-adjust: exact; color: rgb(153, 153, 153);">:</span> <span class="token builtin" style="-webkit-print-color-adjust: exact; color: rgb(102, 153, 0);">String</span>
<span class="token keyword" style="-webkit-print-color-adjust: exact; color: rgb(0, 119, 170);">let</span> city<span class="token punctuation" style="-webkit-print-color-adjust: exact; color: rgb(153, 153, 153);">:</span> <span class="token builtin" style="-webkit-print-color-adjust: exact; color: rgb(102, 153, 0);">String</span>
<span class="token comment" spellcheck="true" style="-webkit-print-color-adjust: exact; color: slategray;">// ...</span>
<span class="token punctuation" style="-webkit-print-color-adjust: exact; color: rgb(153, 153, 153);">}</span></code></pre><div class="">then you can always access the nested data by requesting a <font face="Menlo" class="">nestedContainer(keyedBy: ..., forKey: .address)</font> which will return a container wrapping the <font face="Menlo" class="">address</font> sub-object, which you can then pull fields out of.</div><div class=""><br class=""></div><div class="">The derived conformance case gives a reasonable default, but you can always write your own <font face="Menlo" class="">init(from:)</font> and <font face="Menlo" class="">encode(to:)</font> to handle custom needs.</div></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><br class="">Sent from my iPhone</div><div class=""><br class="">On 26 Apr 2017, at 21:28, 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 Goffredo,</p><p dir="auto" class="">Unless I'm misunderstanding what you mean here, this is exactly what we're proposing with the API — anything <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">Encodable</code> can encode any type that is <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7" class="">Encodable</code> as a nested value:</p>
<pre style="background-color: rgb(247, 247, 247); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin-left: 15px; margin-right: 15px; max-width: 90vw; overflow-x: auto; padding: 5px;" bgcolor="#F7F7F7" class=""><code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0" bgcolor="#F7F7F7" class=""><span style="color: #008800; font-weight: bold" class="">struct</span> <span style="color: #BB0066; font-weight: bold" class="">Person</span> : Codable {
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">name</span>: <span style="color: #007020" class="">String</span>
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">address</span>: Address
}
<span style="color: #008800; font-weight: bold" class="">struct</span> <span style="color: #BB0066; font-weight: bold" class="">Address</span> : Codable {
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">street</span>: <span style="color: #007020" class="">String</span>
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">city</span>: <span style="color: #007020" class="">String</span>
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">state</span>: <span style="color: #007020" class="">String</span>
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">zipCode</span>: <span style="color: #007020" class="">Int</span>
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">country</span>: <span style="color: #007020" class="">String</span>
}
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">address</span> = Address(street: <span style="background-color: #fff0f0" class="">"1 Infinite Loop"</span>, city: <span style="background-color: #fff0f0" class="">"Cupertino"</span>, state: <span style="background-color: #fff0f0" class="">"CA"</span>, zipCode: <span style="color: #0000DD; font-weight: bold" class="">95014</span>, country: <span style="background-color: #fff0f0" class="">"United States"</span>)
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">person</span> = Person(name: <span style="background-color: #fff0f0" class="">"John Doe"</span>, address: address)
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">encoder</span> = JSONEncoder()
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">payload</span> = try encoder.encode(person)
<span style="color: #007020" class="">print</span>(<span style="color: #007020" class="">String</span>(data: payload, encoding: .utf8)<span style="color: #333333" class="">!</span>) <span style="color: #888888" class="">// => {"name": "John Doe", address: {"street": "1 Infinite Loop", ... } }</span>
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">decoder</span> = JSONDecoder()
<span style="color: #008800; font-weight: bold" class="">let</span> <span style="color: #996633" class="">decoded</span> = try decoder.decode(Person.<span style="color: #008800; font-weight: bold" class="">self</span>, from: payload) <span style="color: #888888" class="">// => Person(name: "John Doe", address: ...)</span>
</code></pre><p dir="auto" class="">Or have I misunderstood you?</p><p dir="auto" class="">— Itai</p><p dir="auto" class="">On 26 Apr 2017, at 13:11, Goffredo Marocchi via swift-evolution wrote:</p><div class=""><br class="webkit-block-placeholder"></div></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="558C7C14-AADC-486E-8E7F-C7C0358786AC" class=""><div dir="auto" class=""><div class=""><br class=""><br class="">Sent from my iPhone</div><div class=""><br class="">On 26 Apr 2017, at 17:24, Tony Parker via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class="">Hi Riley,<br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Apr 25, 2017, at 6:11 PM, Riley Testut via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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 style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I’m sure this has already been discussed, but why are the methods throwing NSErrors and not Enums? If I’m remembering correctly, the original reason for this was because this was meant to be a part of Foundation. Now that this is in the Standard Library, however, it seems strange that we’re still using NSError.</div><div class=""><br class=""></div><div class="">Second question that again I’m sure was asked and answered already, but: why do we require implementations for each concrete numeric type (Int, Int8, Int16, Float, etc), instead of using protocols (such as the new Integer protocols)?</div></div></div></blockquote><div class=""><br class=""></div>To answer your second question, the reason is that using the protocol implies that all encoders and decoders must support anything that conforms to that protocol. </div></div></blockquote><div class=""><br class=""></div><div class="">Would this make it easier to transform nested JSON into a nested object/struct? If so it could be useful, very useful.</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="">We’re not sure this is a reasonable requirement. Many formats do not have any kind of support for arbitrary size integers, for example. Therefore, we felt it was best to limit it to a set of concrete types.</div><div class=""><br class=""></div></div></blockquote><div class=""><br class=""></div><div class="">I honk we would be missing a trick, unless I am missing something here, that was very powerful in libraries like Mantle for iOS: the ability to translate a nested JSON object (some keys in the JSON object having a JSON object as value, etc...) in an MTLModel subclass composed of other MTLModel subclasses where doing the transformation of the root object would call the right model needed to transform for the child JSON objects.</div><div class="">Working with Mantle is safe, rugged (it does not cause crashes if the JSON file changes), and allows you to break the problem into chunks and present a coherent simple view to the code that makes use of the instance you created out of the JSON input. Reference: <a href="https://github.com/Mantle/Mantle/blob/master/README.md" class="">https://github.com/Mantle/Mantle/blob/master/README.md</a></div><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class="">We could change our minds on this before we ship Swift 4, if we feel it was the wrong decision. Now that the proposals are accepted we will be landing these branches in master soon, which means everyone has a great chance to try it out and see how it feels in real world usage before it’s final.</div><div class=""><br class=""></div><div class="">- Tony</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Apr 25, 2017, at 3:59 PM, Douglas Gregor via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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 style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Proposal Link: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md</a><div class=""><br class=""></div><div class="">Hello Swift Community,</div><div class=""><br class=""></div><div class="">The review of SE-0166 “Swift Archival & Serialization” ran from April 6...12, 2017. The proposal is <b class="">accepted</b> with some minor modifications. Specifically, the core protocols and types will be sunk down into the Swift standard library for more tight integration with the Swift language and compiler, and the operations specifically involving Foundation’s “Data” type will be removed. The proposal document has been updated with more detail. Thank you everyone for participating in this review!</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>Review Manager</div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></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><p dir="auto" class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="color:#3983C4" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></p>
</div>
<div style="white-space:normal" class="">
</div>
</div>
</div></blockquote></div></div></blockquote></div><br class=""></body></html>