<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Title" content="">
<meta name="Keywords" content="">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Courier New";
        panose-1:2 7 3 9 2 2 5 2 4 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:0 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman";}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:Calibri;
        color:windowtext;}
span.msoIns
        {mso-style-type:export-only;
        mso-style-name:"";
        text-decoration:underline;
        color:teal;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body bgcolor="white" lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Hi Itai,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">No problem!&nbsp; Thanks for the heads up.&nbsp; Is there any way I could be involved?&nbsp; Happy to do the work to whatever guidance your team might have.&nbsp; I’m mostly just interested in it being there
 soon, hence volunteering. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Thanks!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Brandon Sneed<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p>&nbsp;</o:p></span></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From: </span>
</b><span style="font-family:Calibri;color:black">&lt;iferber@apple.com&gt; on behalf of Itai Ferber &lt;iferber@apple.com&gt;<br>
<b>Date: </b>Wednesday, August 30, 2017 at 11:22 AM<br>
<b>To: </b>&quot;Sneed, Brandon&quot; &lt;brsneed@ebay.com&gt;<br>
<b>Cc: </b>&quot;swift-corelibs-dev@swift.org&quot; &lt;swift-corelibs-dev@swift.org&gt;<br>
<b>Subject: </b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
<div>
<div>
<p><span style="font-family:Helvetica">Hi Brandon,<o:p></o:p></span></p>
<p><span style="font-family:Helvetica">Thanks for looking at this! We’ve got plans internally to potentially add a strategy to
</span><code><span style="font-size:10.0pt;background:#F7F7F7">JSONEncoder</span></code><span style="font-family:Helvetica">/</span><code><span style="font-size:10.0pt;background:#F7F7F7">JSONDecoder</span></code><span style="font-family:Helvetica"> to allow
 lenient conversions like this — i.e. implicitly stringify numbers (or parse them from string input), among some others.<br>
This would be opt-in for consumers of </span><code><span style="font-size:10.0pt;background:#F7F7F7">JSONDecoder</span></code><span style="font-family:Helvetica"> while not requiring any special annotations on
</span><code><span style="font-size:10.0pt;background:#F7F7F7">Codable</span></code><span style="font-family:Helvetica"> types.<o:p></o:p></span></p>
<p><span style="font-family:Helvetica">— Itai<o:p></o:p></span></p>
<p><span style="font-family:Helvetica">On 30 Aug 2017, at 10:59, Sneed, Brandon via swift-corelibs-dev wrote:<o:p></o:p></span></p>
</div>
<blockquote style="border:none;border-left:solid #777777 1.5pt;padding:0in 0in 0in 4.0pt;margin-left:0in;margin-right:0in;margin-bottom:3.75pt">
<div id="51AAF58D-1B44-4118-93E0-827F3EEB8D2A">
<div>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">Hi everyone,</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">Just throwing this out to see if anyone else is working on this, or has opinions/suggestions on how it’s implemented.&nbsp; I’d like to add this to the Codable/JSONDecoder/JSONEncoder
 system if no one else is working on it.</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">Type type conversion, I mean given this JSON payload:</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">{</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot;: &quot;Endeavor”,</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; &nbsp; &nbsp; &quot;abv&quot;: 8.9,</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; &nbsp; &nbsp; &quot;brewery&quot;: &quot;Saint Arnold”,</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; &nbsp; &nbsp; &quot;style&quot;: &quot;ipa&quot;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">}</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">and a struct defined as:</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">struct Beer: Codable {</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let name: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let abv: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let brewery: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let style: BeerStyle</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">}</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">Notice that “abv” is a number in the JSON, but a String in the struct.&nbsp; 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.&nbsp; The benefits are:</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;It’s defensive; service types can change without causing my application to crash.</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">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.&nbsp; The same type of logic could be applied to a member name changing, though I haven’t seen this happen often in practice.&nbsp; I do see types in a json payload change with some frequency though.&nbsp; 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><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">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><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">struct Beer: Codable, CodingConvertible {</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let name: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let abv: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let brewery: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let style: BeerStyle</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">}</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">I like this because looking at the struct, the members still remain clear and relatively unambiguous.&nbsp; The downside is it’s unknown which member is
 likely to get converted.&nbsp; And since it’s opt-in, conversion doesn’t happen if the CodingConvertible conformance isn’t adhered to.</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">Another option would be to box each type, like so:</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">struct Beer: Codable {</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let name: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let abv: Convertible&lt;String&gt;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let brewery: String</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp; &nbsp; let style: BeerStyle</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">}</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">This seems tedious for developers, but would show which types are being converted.&nbsp; It does however seriously weaken benefit #1 above.</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">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.&nbsp; I think
 that could be done without impact to code that’s been already been written against the JSON en/decode bits.</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">I’m very open to alternatives, other ideas, or anything else you might have to say on the subject.&nbsp; Thanks for reading!</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">Brandon Sneed</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:11.0pt;font-family:Calibri;color:#777777">&nbsp;</span><span style="font-family:Calibri;color:#777777"><o:p></o:p></span></p>
</div>
</div>
</blockquote>
<div>
<blockquote style="border:none;border-left:solid #777777 1.5pt;padding:0in 0in 0in 4.0pt;margin-left:0in;margin-right:0in;margin-bottom:3.75pt">
<p><span style="font-family:Helvetica;color:#777777">_______________________________________________<br>
swift-corelibs-dev mailing list<br>
swift-corelibs-dev@swift.org<br>
<a href="https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.swift.org%2Fmailman%2Flistinfo%2Fswift-corelibs-dev&amp;data=02%7C01%7Cbrsneed%40ebay.com%7C0e58a975be44418826d608d4efd427dc%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397141865218008&amp;sdata=ytYIqDtMesw4NnpUbFmiWF2%2FKfxlawG4YuVWPJd099Y%3D&amp;reserved=0"><span style="color:#777777">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</span></a><o:p></o:p></span></p>
</blockquote>
</div>
</div>
</div>
</body>
</html>