<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=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Aug 30, 2017, at 3:12 PM, Sneed, Brandon &lt;<a href="mailto:brsneed@ebay.com" class="">brsneed@ebay.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; 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; background-color: rgb(255, 255, 255);"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">Thanks Tony,<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class="">&nbsp;</o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">Ah.&nbsp; That should work, and covers benefit #2 I mentioned very nicely.&nbsp; Only downside is that as a developer on an app I may not expect that type to change on the server side so I wouldn’t do it by default, and when it does happen, I then need to apply it to each of its siblings because if it can happen to one, it can happen to the others.</span></div></div></div></blockquote><div><br class=""></div>In this case, the server decides to send a String instead of a numeric value for many kinds of types?</div><div><br class=""></div><div>I understand that some JSON libraries have options to stringify all numeric values in an attempt to preserve ‘exactness’, although I would argue that this depends on what you do with the numeric value on the other side…</div><div><br class=""></div><div>- Tony</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class="WordSection1" style="page: WordSection1; 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; background-color: rgb(255, 255, 255);"><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class="">&nbsp;</o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">Javascript and GSON handle this in a relatively safe way.&nbsp; I think us having a way to opt-in to the same capabilities is good.&nbsp; When I say opt-in, I mean at the containing-struct level, not necessarily per member, nor do I want to do a custom init every time.&nbsp; I’m sure there’s a happy place in the middle, just not sure what it is.&nbsp;<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class="">&nbsp;</o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">I do also want to add that I very much appreciate the discussion.&nbsp; We’re having one internally too, and the divide seems to be about the same as what’s on the mailing list (which seems about 50/50).<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class="">&nbsp;</o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class="">&nbsp;</o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">Brandon Sneed<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class="">&nbsp;</o:p></span></div><div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(181, 196, 223); padding: 3pt 0in 0in;" class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><b class=""><span style="font-family: Calibri;" class="">From:<span class="Apple-converted-space">&nbsp;</span></span></b><span style="font-family: Calibri;" class="">&lt;<a href="mailto:anthony.parker@apple.com" class="">anthony.parker@apple.com</a>&gt; on behalf of Tony Parker &lt;<a href="mailto:anthony.parker@apple.com" class="">anthony.parker@apple.com</a>&gt;<br class=""><b class="">Date:<span class="Apple-converted-space">&nbsp;</span></b>Wednesday, August 30, 2017 at 3:04 PM<br class=""><b class="">To:<span class="Apple-converted-space">&nbsp;</span></b>"Sneed, Brandon" &lt;<a href="mailto:brsneed@ebay.com" class="">brsneed@ebay.com</a>&gt;<br class=""><b class="">Cc:<span class="Apple-converted-space">&nbsp;</span></b>Youming Lin &lt;<a href="mailto:ylin@us.ibm.com" class="">ylin@us.ibm.com</a>&gt;, "<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>&gt;<br class=""><b class="">Subject:<span class="Apple-converted-space">&nbsp;</span></b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<o:p class=""></o:p></span></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">I haven’t tried this myself yet, but you could imagine creating a type which represents an ABV and decodes using a single value container. Then, your custom behavior goes into the Codable implementation of this ABV type.<o:p class=""></o:p></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">Struct Beer then looks like:<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">struct Beer: Codable {<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">&nbsp; &nbsp; let name: String<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">&nbsp; &nbsp; let abv: ABV<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">&nbsp; &nbsp; …<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">}<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">ABV can have API on it to get the numeric value or String value; or that could go into Beer and it would abstract the ABV type from callers.<o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">- Tony<o:p class=""></o:p></div><div class=""><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><br class=""><br class=""><o:p class=""></o:p></div><blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class=""><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class="">On Aug 30, 2017, at 2:42 PM, Sneed, Brandon &lt;<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class="">brsneed@ebay.com</a>&gt; wrote:<o:p class=""></o:p></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div><div class=""><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">Thanks Youming,</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">That’s not quite what I meant.&nbsp; I may have misinterpreted what Tony was saying though.</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">I wanted to do conversion on String, not the containing type.&nbsp; The problem of doing it on the containing type is that as soon as you need one field to be custom, you’re roped into handling all the others.&nbsp; For a small struct, not a big deal, for larger ones, it is.&nbsp; Something like this is what I tried:</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">import Cocoa</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let jsonString = "{" +</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; "\"name\": \"Endeavor\"," +</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; "\"abv\": 8.9," +</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; "\"brewery\": \"Saint Arnold\"," +</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; "\"style\": \"ipa\"}"</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">struct Beer: Codable {</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; let name: String</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; let abv: String</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; let brewery: String</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; let style: String</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">}</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">extension String {</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; init(from decoder: Decoder) throws {</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; &nbsp; &nbsp; print("i got hit.")</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; &nbsp; &nbsp; let value = try decoder.singleValueContainer().decode(String.self)</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; &nbsp; &nbsp; self.init(value)</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp; &nbsp; }</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">}</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let jsonData = jsonString.data(using: .utf8)</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let decoder = JSONDecoder()</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let beer = try! decoder.decode(Beer.self, from: jsonData!)</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><b class=""><span style="font-size: 11pt; font-family: Calibri;" class="">fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [__lldb_expr_17.Beer.(CodingKeys in _C8902E33F84CE6946081129DAF1824E1).abv], debugDescription: "Expected to decode String but found a number instead.", underlyingError: nil)): file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 181</span></b><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">&nbsp;</span><o:p class=""></o:p></div></div><div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(181, 196, 223); padding: 3pt 0in 0in;" class=""><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><b class=""><span style="font-family: Calibri;" class="">From:<span class="apple-converted-space">&nbsp;</span></span></b><span style="font-family: Calibri;" class="">Youming Lin &lt;<a href="mailto:ylin@us.ibm.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">ylin@us.ibm.com</span></a>&gt;<br class=""><b class="">Date:<span class="apple-converted-space">&nbsp;</span></b>Wednesday, August 30, 2017 at 2:35 PM<br class=""><b class="">To:<span class="apple-converted-space">&nbsp;</span></b>"Sneed, Brandon" &lt;<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">brsneed@ebay.com</span></a>&gt;<br class=""><b class="">Cc:<span class="apple-converted-space">&nbsp;</span></b>Tony Parker &lt;<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">anthony.parker@apple.com</span></a>&gt;, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;, "<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a>&gt;<br class=""><b class="">Subject:<span class="apple-converted-space">&nbsp;</span></b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode</span><o:p class=""></o:p></div></div></div><div class=""><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class="">&nbsp;<o:p class=""></o:p></div></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 10pt;" class="">Brandon<br class=""><br class="">I cooked up a simple example and it works as expected.<br class=""><br class=""></span><span style="font-family: Monaco;" class="">ylin@youming-mbpr:~/Swift/Configuration$ swift</span><br class=""><span style="font-family: Monaco;" class="">Welcome to Apple Swift version 4.0-dev (LLVM 2dedb62a0b, Clang b9d76a314c, Swift 0899bd328a). Type :help for assistance.</span><br class=""><span style="font-family: Monaco;" class="">1&gt; import Foundation</span><br class=""><span style="font-family: Monaco;" class="">2&gt; struct A: Codable {<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">3. var integer: Int<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">4.<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">5. public init(integer: Int) {<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">6. self.integer = integer<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">7. }<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">8.<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">9. public init(from decoder: Decoder) throws {<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">10. print("Custom decoder")<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">11. let container = try decoder.container(keyedBy: CodingKeys.self)<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">12. integer = try container.decode(Int.self, forKey: .integer)<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">13. }<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">14. }<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">15.<span class="apple-converted-space">&nbsp;</span></span><br class=""><span style="font-family: Monaco;" class="">16. try! JSONDecoder().decode(A.self, from: JSONEncoder().encode(A(integer: 42)))</span><br class=""><span style="font-family: Monaco;" class="">Custom decoder</span><br class=""><span style="font-family: Monaco;" class="">$R0: A = {</span><br class=""><span style="font-family: Monaco;" class="">integer = 42</span><br class=""><span style="font-family: Monaco;" class="">}</span><span style="font-size: 10pt;" class=""><br class=""></span><br class=""><span style="font-size: 10pt;" class="">You should be able to implement your custom init to convert the number into a string and JSONDecoder should use that automatically.<br class=""></span><br class=""><span style="font-size: 10pt;" class="">Thanks,<br class=""><br class="">Youming Lin<br class="">IBM Cloud, Swift@IBM, Kitura developer<br class="">Austin, TX<br class="">GitHub: @youming-lin</span><br class=""><br class="">&lt;image001.gif&gt;<span style="font-size: 10pt; color: rgb(66, 66, 130);" class="">"Sneed, Brandon" ---08/30/2017 04:14:57 PM---Thanks Youming, Ok, thanks! I did try that, but I can’t seem to figure out how to make it actually</span><br class=""><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">From:<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt;" class="">"Sneed, Brandon" &lt;<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">brsneed@ebay.com</span></a>&gt;</span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">To:<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt;" class="">Youming Lin &lt;<a href="mailto:ylin@us.ibm.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">ylin@us.ibm.com</span></a>&gt;</span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">Cc:<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt;" class="">Tony Parker &lt;<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">anthony.parker@apple.com</span></a>&gt;, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;, "<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a>&gt;</span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">Date:<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt;" class="">08/30/2017 04:14 PM</span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">Subject:<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt;" class="">Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode</span><o:p class=""></o:p></div><div class=""><div class="MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white; background-position: initial initial; background-repeat: initial initial;"><hr size="2" width="100%" noshade="" align="left" style="color: rgb(128, 145, 165);" class=""></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><br class=""><br class=""><br class="">Thanks Youming,<br class=""><br class="">Ok, thanks! I did try that, but I can’t seem to figure out how to make it actually get used in the decoding process. That being my preferred way, I tried it first. I chalked it not working up to Swift not knowing which of the 2 init(from decoder:) functions to call, mine or theirs. But, maybe there’s something I’m missing here.<br class=""><br class="">Any insight is appreciated.<br class=""><br class="">Thanks!<br class=""><br class=""><br class=""><br class="">Brandon Sneed<br class=""><br class=""><b class="">From:<span class="apple-converted-space">&nbsp;</span></b>Youming Lin &lt;<a href="mailto:ylin@us.ibm.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">ylin@us.ibm.com</span></a>&gt;<b class=""><br class="">Date:<span class="apple-converted-space">&nbsp;</span></b>Wednesday, August 30, 2017 at 1:18 PM<b class=""><br class="">To:<span class="apple-converted-space">&nbsp;</span></b>"Sneed, Brandon" &lt;<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">brsneed@ebay.com</span></a>&gt;<b class=""><br class="">Cc:<span class="apple-converted-space">&nbsp;</span></b>Tony Parker &lt;<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">anthony.parker@apple.com</span></a>&gt;, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;, "<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a>&gt;<b class=""><br class="">Subject:<span class="apple-converted-space">&nbsp;</span></b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<o:p class=""></o:p></div></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 10pt;" class="">Brandon<br class=""><br class="">&gt;</span>JSON’s types effectively end up matching specifically to primitives, of which there is no mechanism to override the behavior of how a String gets decoded for instance.<span style="font-size: 10pt;" class=""><br class=""><br class="">You can override the default behavior with your own custom init(from:) implementation for your Codable struct:<span class="apple-converted-space">&nbsp;</span></span><a href="https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.proofpoint.com%2Fv2%2Furl%3Fu%3Dhttps-3A__na01.safelinks.protection.outlook.com_-3Furl-3Dhttps-253A-252F-252Fdeveloper.apple.com-252Fdocumentation-252Fswift-252Fdecodable-252F2894081-2Dinit-26data-3D02-257C01-257Cbrsneed-2540ebay.com-257C6290a819253b47b0eb2408d4efe454ae-257C46326bff992841a0baca17c16c94ea99-257C0-257C0-257C636397211330551372-26sdata-3DoLr1Q10-252BztzwG-252BCXpMinBzJNTwSy-252FjoBsKm9Glg1-252FxY-253D-26reserved-3D0%26d%3DDwMGaQ%26c%3Djf_iaSHvJObTbx-siA1ZOg%26r%3DgkRZBtsmKeGPCOlAIRJoOA%26m%3Dc3lYikOfd2-4q_nd_qMnJ4gXKIuKuxxoJRxIrIZc3Hw%26s%3Dh1azblXptqRwHqDQhejN9sSFa8dR-Gd8OkB7_6VwgNg%26e%3D&amp;data=02%7C01%7Cbrsneed%40ebay.com%7Cebd9669255614ce4dccc08d4efef218c%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397257717072726&amp;sdata=TIZrCfocLCJGcfOj%2B9V2FEiXPSTKN4hnL1Rm03GqHvk%3D&amp;reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt; color: purple;" class="">https://developer.apple.com/documentation/swift/decodable/2894081-init</span></a><span style="font-size: 10pt;" class=""><br class=""><br class="">You can check Foundation source code (i.e., the URL struct) on how this can be implemented.</span><br class=""><span style="font-size: 10pt;" class=""><br class="">Thanks,<br class=""><br class="">Youming Lin<br class="">IBM Cloud, Swift@IBM, Kitura developer<br class="">Austin, TX<br class="">GitHub: @youming-lin</span><br class=""><br class="">&lt;image002.gif&gt;<span style="font-size: 10pt; color: rgb(66, 66, 130);" class="">"Sneed, Brandon via swift-corelibs-dev" ---08/30/2017 03:07:05 PM---Hi Tony, I like the idea that the type itself is responsible for the conversion. My own json encode</span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class=""><br class="">From:<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt;" class="">"Sneed, Brandon via swift-corelibs-dev" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;<span style="color: rgb(95, 95, 95);" class=""><br class="">To:<span class="apple-converted-space">&nbsp;</span></span>Tony Parker &lt;<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">anthony.parker@apple.com</span></a>&gt;<span style="color: rgb(95, 95, 95);" class=""><br class="">Cc:<span class="apple-converted-space">&nbsp;</span></span>"<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;<span style="color: rgb(95, 95, 95);" class=""><br class="">Date:<span class="apple-converted-space">&nbsp;</span></span>08/30/2017 03:07 PM<span style="color: rgb(95, 95, 95);" class=""><br class="">Subject:<span class="apple-converted-space">&nbsp;</span></span>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<span style="color: rgb(95, 95, 95);" class=""><br class="">Sent by:<span class="apple-converted-space">&nbsp;</span></span><a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev-bounces@swift.org</span></a></span><o:p class=""></o:p></div><div class=""><div class="MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white; background-position: initial initial; background-repeat: initial initial;"><hr size="2" width="100%" noshade="" align="left" style="color: rgb(170, 170, 170);" class=""></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><br class=""><br class=""><br class=""><br class="">Hi Tony,<br class=""><br class="">I like the idea that the type itself is responsible for the conversion. My own json encode/decode library worked this way and it was really great, however in trying to leverage Swift4 into it, or to replace it, I just don’t see how that’s possible given how it’s currently structured.<br class=""><br class="">JSON’s types effectively end up matching specifically to primitives, of which there is no mechanism to override the behavior of how a String gets decoded for instance. The only way I can think of to accomplish that is to create *<b class="">another</b>* type, JSONString for example, but since String is a struct, I can’t subclass it, and instead need to have the real value buried inside of it … it seems to start getting messy very quickly. It also adds the obfuscation of dealing with yet another type, which I’m not against, but just feels less than ideal.<br class=""><br class=""><br class="">Brandon Sneed<br class=""><b class=""><br class="">From:<span class="apple-converted-space">&nbsp;</span></b>&lt;<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">anthony.parker@apple.com</span></a>&gt; on behalf of Tony Parker &lt;<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">anthony.parker@apple.com</span></a>&gt;<b class=""><br class="">Date:<span class="apple-converted-space">&nbsp;</span></b>Wednesday, August 30, 2017 at 11:30 AM<b class=""><br class="">To:<span class="apple-converted-space">&nbsp;</span></b>"Sneed, Brandon" &lt;<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">brsneed@ebay.com</span></a>&gt;<b class=""><br class="">Cc:<span class="apple-converted-space">&nbsp;</span></b>Itai Ferber &lt;<a href="mailto:iferber@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">iferber@apple.com</span></a>&gt;, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;<b class=""><br class="">Subject:<span class="apple-converted-space">&nbsp;</span></b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<br class=""><br class="">I’m still not convinced that we should actually provide such a strategy.<span class="apple-converted-space">&nbsp;</span><br class=""><br class="">Conversions like those below seem like the domain of each type that is being decoded. If, in a particular type, the “number” can be either a true number or a string, then that type can try decoding it as one or the other and fall back as required. That puts the responsibility of doing that kind of conversion in the type itself.<br class=""><br class="">JSON has very few types already. I’m not sure we want to blur the line between numbers and strings automatically…<br class=""><br class="">- Tony<span class="apple-converted-space">&nbsp;</span><o:p class=""></o:p></div></div><div style="margin-left: 2in;" class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class="">On Aug 30, 2017, at 11:24 AM, Sneed, Brandon via swift-corelibs-dev &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt; wrote:<br class=""><br class="">Hi Itai,<br class=""><br class="">No problem! Thanks for the heads up. Is there any way I could be involved? Happy to do the work to whatever guidance your team might have. I’m mostly just interested in it being there soon, hence volunteering.<span class="apple-converted-space">&nbsp;</span><br class=""><br class="">Thanks!<br class=""><br class=""><br class="">Brandon Sneed<br class=""><b class=""><br class="">From:<span class="apple-converted-space">&nbsp;</span></b>&lt;<a href="mailto:iferber@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">iferber@apple.com</span></a>&gt; on behalf of Itai Ferber &lt;<a href="mailto:iferber@apple.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">iferber@apple.com</span></a>&gt;<b class=""><br class="">Date:<span class="apple-converted-space">&nbsp;</span></b>Wednesday, August 30, 2017 at 11:22 AM<b class=""><br class="">To:<span class="apple-converted-space">&nbsp;</span></b>"Sneed, Brandon" &lt;<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">brsneed@ebay.com</span></a>&gt;<b class=""><br class="">Cc:<span class="apple-converted-space">&nbsp;</span></b>"<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>" &lt;<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a>&gt;<b class=""><br class="">Subject:<span class="apple-converted-space">&nbsp;</span></b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<br class=""><span style="font-family: Helvetica;" class=""><br class="">Hi Brandon,<br class="">Thanks for looking at this! We’ve got plans internally to potentially add a strategy to<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" class="">JSONEncoder</span><span style="font-family: Helvetica;" class="">/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" class="">JSONDecoder</span><span class="apple-converted-space"><span style="font-family: Helvetica;" class="">&nbsp;</span></span><span style="font-family: Helvetica;" class="">to allow lenient conversions like this — i.e. implicitly stringify numbers (or parse them from string input), among some others.<br class="">This would be opt-in for consumers of<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" class="">JSONDecoder</span><span class="apple-converted-space"><span style="font-family: Helvetica;" class="">&nbsp;</span></span><span style="font-family: Helvetica;" class="">while not requiring any special annotations on<span class="apple-converted-space">&nbsp;</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" class="">Codable</span><span class="apple-converted-space"><span style="font-family: Helvetica;" class="">&nbsp;</span></span><span style="font-family: Helvetica;" class="">types.<br class="">— Itai<br class="">On 30 Aug 2017, at 10:59, Sneed, Brandon via swift-corelibs-dev wrote:</span><span style="color: rgb(119, 119, 119);" class=""><br class="">Hi everyone,</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">Just throwing this out to see if anyone else is working on this, or has opinions/suggestions on how it’s implemented. I’d like to add this to the Codable/JSONDecoder/JSONEncoder system if no one else is working on it.</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">Type type conversion, I mean given this JSON payload:</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">{<br class="">"name": "Endeavor”,<br class="">"abv": 8.9,<br class="">"brewery": "Saint Arnold”,<br class="">"style": "ipa"<br class="">}</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">and a struct defined as:</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">struct Beer: Codable {<br class="">let name: String<br class="">let abv: String<br class="">let brewery: String<br class="">let style: BeerStyle<br class="">}</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">Notice that “abv” is a number in the JSON, but a String in the struct. I’d like to make it such that I can let the system know it’s ok to convert it from a number to a string as opposed to throwing an exception. The benefits are:</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">1. It’s defensive; service types can change without causing my application to crash.<br class="">2. It allows a developer to work with the types they want to work with as opposed to what the server provides, thus saving them time of writing a custom encode/decode code for all members.</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">The argument against it that I’ve heard is generally “it’s a service bug, make them fix it”, which is valid but the reality is we’re not all in control of the services we injest. The same type of logic could be applied to a member name changing, though I haven’t seen this happen often in practice. I do see types in a json payload change with some frequency though. I think much of the reason stems from the fact that type conversion in javascript is effectively free, ie: you ask for a String, you get a String if possible.</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">To implement this type conversion in practice, looking at it from the point of view using Codable/JSON(en/de)coder, one way would be to make it opt-in:</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">struct Beer: Codable, CodingConvertible {<br class="">let name: String<br class="">let abv: String<br class="">let brewery: String<br class="">let style: BeerStyle<br class="">}</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">I like this because looking at the struct, the members still remain clear and relatively unambiguous. The downside is it’s unknown which member is likely to get converted. And since it’s opt-in, conversion doesn’t happen if the CodingConvertible conformance isn’t adhered to.</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">Another option would be to box each type, like so:</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">struct Beer: Codable {<br class="">let name: String<br class="">let abv: Convertible&lt;String&gt;<br class="">let brewery: String<br class="">let style: BeerStyle<br class="">}</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">This seems tedious for developers, but would show which types are being converted. It does however seriously weaken benefit #1 above.</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">Those example usages above aside, I do think it’d be best if this conversion behavior was the default and no end-developer changes required. I think that could be done without impact to code that’s been already been written against the JSON en/decode bits.</span><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">I’m very open to alternatives, other ideas, or anything else you might have to say on the subject. Thanks for reading!</span><br class=""><br class=""><br class=""><span style="color: rgb(119, 119, 119);" class=""><br class="">Brandon Sneed</span><br class=""><br class=""><br class=""><br class=""><span style="font-family: Helvetica; color: rgb(119, 119, 119);" class=""><br class="">_______________________________________________<br class="">swift-corelibs-dev mailing list</span><u class=""><span style="color: blue;" class=""><br class=""></span></u><a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="font-family: Helvetica; color: purple;" class="">swift-corelibs-dev@swift.org</span></a><u class=""><span style="color: blue;" class=""><br class=""></span></u><a href="https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.proofpoint.com%2Fv2%2Furl%3Fu%3Dhttps-3A__na01.safelinks.protection.outlook.com_-3Furl-3Dhttps-253A-252F-252Flists.swift.org-252Fmailman-252Flistinfo-252Fswift-2Dcorelibs-2Ddev-26data-3D02-257C01-257Cbrsneed-2540ebay.com-257C0e58a975be44418826d608d4efd427dc-257C46326bff992841a0baca17c16c94ea99-257C0-257C0-257C636397141865218008-26sdata-3DytYIqDtMesw4NnpUbFmiWF2-252FKfxlawG4YuVWPJd099Y-253D-26reserved-3D0%26d%3DDwMGaQ%26c%3Djf_iaSHvJObTbx-siA1ZOg%26r%3DgkRZBtsmKeGPCOlAIRJoOA%26m%3DViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg%26s%3DrVZl8iT3jj1nzuDTCqZ1pkhQIZD3-Byi8PUj50swTUg%26e%3D&amp;data=02%7C01%7Cbrsneed%40ebay.com%7C6290a819253b47b0eb2408d4efe454ae%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397211330551372&amp;sdata=GkxX7xyhtHr1zqISNmjeBhvOkVoZdOihXcaugXSK9tA%3D&amp;reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-family: Helvetica; color: rgb(119, 119, 119);" class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</span></a><span style="font-size: 10pt; font-family: Helvetica;" class=""><br class="">_______________________________________________<br class="">swift-corelibs-dev mailing list</span><u class=""><span style="color: blue;" class=""><br class=""></span></u><a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt; font-family: Helvetica; color: purple;" class="">swift-corelibs-dev@swift.org</span></a><u class=""><span style="color: blue;" class=""><br class=""></span></u><a href="https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.proofpoint.com%2Fv2%2Furl%3Fu%3Dhttps-3A__na01.safelinks.protection.outlook.com_-3Furl-3Dhttps-253A-252F-252Flists.swift.org-252Fmailman-252Flistinfo-252Fswift-2Dcorelibs-2Ddev-26data-3D02-257C01-257Cbrsneed-2540ebay.com-257Cf2eba37a5b40474e09b108d4efd5372d-257C46326bff992841a0baca17c16c94ea99-257C0-257C0-257C636397146413883032-26sdata-3DC1-252F8MXq-252Fh7NHgyxeKDkcHDcigtQjSztCaAeUxBzYZ3g-253D-26reserved-3D0%26d%3DDwMGaQ%26c%3Djf_iaSHvJObTbx-siA1ZOg%26r%3DgkRZBtsmKeGPCOlAIRJoOA%26m%3DViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg%26s%3DLQ0cgWtC9rXRTDLu0W58VIukWrsRssHsIMGb9U6Y0MU%26e%3D&amp;data=02%7C01%7Cbrsneed%40ebay.com%7C6290a819253b47b0eb2408d4efe454ae%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397211330551372&amp;sdata=afQLw7%2FVip%2Bts1P60ALOHrTNap93519tWLjYzkS2o5Q%3D&amp;reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt; font-family: Helvetica; color: purple;" class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</span></a><o:p class=""></o:p></div></div><div class=""><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;; background-color: white;" class=""><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" class="">_______________________________________________<br class="">swift-corelibs-dev mailing list<br class=""><a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class=""><span style="color: purple;" class="">swift-corelibs-dev@swift.org</span></a><u class=""><span style="color: blue;" class=""><br class=""></span></u></span><a href="https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.proofpoint.com%2Fv2%2Furl%3Fu%3Dhttps-3A__na01.safelinks.protection.outlook.com_-3Furl-3Dhttps-253A-252F-252Furldefense.proofpoint.com-252Fv2-252Furl-253Fu-253Dhttps-2D3A-5F-5Flists.swift.org-5Fmailman-5Flistinfo-5Fswift-2D2Dcorelibs-2D2Ddev-2526d-253DDwIGaQ-2526c-253Djf-5FiaSHvJObTbx-2DsiA1ZOg-2526r-253DgkRZBtsmKeGPCOlAIRJoOA-2526m-253DViDSVPImta3StTVAcktby2PMF-5F-2Ddu5itzz47jo-2DtNHg-2526s-253DzRuNQ3NLxpfhFBewRTkoMWZnpvHlm6Ja-2Dot9-5FpwAgqI-2526e-253D-26data-3D02-257C01-257Cbrsneed-2540ebay.com-257C6290a819253b47b0eb2408d4efe454ae-257C46326bff992841a0baca17c16c94ea99-257C0-257C0-257C636397211330551372-26sdata-3DkUgv-252B3QRVpUH5JGBclTqYHheS-252FfaDPIWTrTk-252F-252BX8J-252Bs-253D-26reserved-3D0%26d%3DDwMGaQ%26c%3Djf_iaSHvJObTbx-siA1ZOg%26r%3DgkRZBtsmKeGPCOlAIRJoOA%26m%3Dc3lYikOfd2-4q_nd_qMnJ4gXKIuKuxxoJRxIrIZc3Hw%26s%3DlhWK6MIAPUmU4rsIhtD7P7EgacPwfGk17SvpQdMJogc%26e%3D&amp;data=02%7C01%7Cbrsneed%40ebay.com%7Cebd9669255614ce4dccc08d4efef218c%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397257717072726&amp;sdata=jJucdx3j9pb2ylcsHYaVAtze%2B%2BQWr%2F%2Bm2GpBRlHaBTk%3D&amp;reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: purple;" class="">https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Dcorelibs-2Ddev&amp;d=DwIGaQ&amp;c=jf_iaSHvJObTbx-siA1ZOg&amp;r=gkRZBtsmKeGPCOlAIRJoOA&amp;m=ViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg&amp;s=zRuNQ3NLxpfhFBewRTkoMWZnpvHlm6Ja-ot9_pwAgqI&amp;e=</span></a><span class="apple-converted-space"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" class="">&nbsp;</span></span><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><o:p class=""></o:p></div></div></div></blockquote></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: &quot;Times New Roman&quot;;" class=""><o:p class="">&nbsp;</o:p></div></div></div></div></div></blockquote></div><br class=""></body></html>