<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> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">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. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p> </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> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><o:p> </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> </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"><iferber@apple.com> on behalf of Itai Ferber <iferber@apple.com><br>
<b>Date: </b>Wednesday, August 30, 2017 at 11:22 AM<br>
<b>To: </b>"Sneed, Brandon" <brsneed@ebay.com><br>
<b>Cc: </b>"swift-corelibs-dev@swift.org" <swift-corelibs-dev@swift.org><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> </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"> </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. 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"> </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"> </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"> "name": "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"> "abv": 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"> "brewery": "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"> "style": "ipa"</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"> </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"> </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"> 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"> 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"> 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"> 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"> </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. 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><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">1. 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. 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"> </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. 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><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">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"> </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"> 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"> 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"> 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"> 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"> </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. 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><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">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"> </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"> 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"> let abv: Convertible<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"> 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"> 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"> </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. 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"> </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. 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"> </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. 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"> </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"> </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"> </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"> </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>
</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&data=02%7C01%7Cbrsneed%40ebay.com%7C0e58a975be44418826d608d4efd427dc%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397141865218008&sdata=ytYIqDtMesw4NnpUbFmiWF2%2FKfxlawG4YuVWPJd099Y%3D&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>