<html><body bgcolor="#FFFFFF"><p><font size="2">Brandon<br><br>></font>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.<br><font size="2"><br>You can override the default behavior with your own custom init(from:) implementation for your Codable struct: </font><a href="https://developer.apple.com/documentation/swift/decodable/2894081-init"><font size="2">https://developer.apple.com/documentation/swift/decodable/2894081-init</font></a><font size="2"><br><br>You can check Foundation source code (i.e., the URL struct) on how this can be implemented.<br></font><br><font size="2">Thanks,<br><br>Youming Lin<br>IBM Cloud, Swift@IBM, Kitura developer<br>Austin, TX<br>GitHub: @youming-lin</font><br><br><img width="16" height="16" src="cid:1__=8FBB0B1FDFFCB0808f9e8a93df938690918c8FB@" border="0" alt="Inactive hide details for "Sneed, Brandon via swift-corelibs-dev" ---08/30/2017 03:07:05 PM---Hi Tony, I like the idea that the"><font size="2" color="#424282">"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</font><br><br><font size="2" color="#5F5F5F">From: </font><font size="2">"Sneed, Brandon via swift-corelibs-dev" <swift-corelibs-dev@swift.org></font><br><font size="2" color="#5F5F5F">To: </font><font size="2">Tony Parker <anthony.parker@apple.com></font><br><font size="2" color="#5F5F5F">Cc: </font><font size="2">"swift-corelibs-dev@swift.org" <swift-corelibs-dev@swift.org></font><br><font size="2" color="#5F5F5F">Date: </font><font size="2">08/30/2017 03:07 PM</font><br><font size="2" color="#5F5F5F">Subject: </font><font size="2">Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode</font><br><font size="2" color="#5F5F5F">Sent by: </font><font size="2">swift-corelibs-dev-bounces@swift.org</font><br><hr width="100%" size="2" align="left" noshade style="color:#8091A5; "><br><br><br>Hi Tony,<br> <br>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> <br>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>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> <br> <br>Brandon Sneed<br> <br><b>From: </b><anthony.parker@apple.com> on behalf of Tony Parker <anthony.parker@apple.com><b><br>Date: </b>Wednesday, August 30, 2017 at 11:30 AM<b><br>To: </b>"Sneed, Brandon" <brsneed@ebay.com><b><br>Cc: </b>Itai Ferber <iferber@apple.com>, "swift-corelibs-dev@swift.org" <swift-corelibs-dev@swift.org><b><br>Subject: </b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<br><font face="Times New Roman"> </font><br><font face="Times New Roman">I’m still not convinced that we should actually provide such a strategy. </font><br><font face="Times New Roman"> </font><br><font face="Times New Roman">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.</font><br><font face="Times New Roman"> </font><br><font face="Times New Roman">JSON has very few types already. I’m not sure we want to blur the line between numbers and strings automatically…</font><br><font face="Times New Roman"> </font><br><font face="Times New Roman">- Tony</font><br><font face="Times New Roman"><br></font><ul><ul><font face="Times New Roman">On Aug 30, 2017, at 11:24 AM, Sneed, Brandon via swift-corelibs-dev <</font><a href="mailto:swift-corelibs-dev@swift.org"><u><font color="#0000FF" face="Times New Roman">swift-corelibs-dev@swift.org</font></u></a><font face="Times New Roman">> wrote:</font><br><font face="Times New Roman"> </font><br>Hi Itai,<br> <br>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. <br> <br>Thanks!<br> <br> <br>Brandon Sneed<br> <br><b>From: </b><<a href="mailto:iferber@apple.com"><u><font color="#0000FF">iferber@apple.com</font></u></a>> on behalf of Itai Ferber <<a href="mailto:iferber@apple.com"><u><font color="#0000FF">iferber@apple.com</font></u></a>><b><br>Date: </b>Wednesday, August 30, 2017 at 11:22 AM<b><br>To: </b>"Sneed, Brandon" <<a href="mailto:brsneed@ebay.com"><u><font color="#0000FF">brsneed@ebay.com</font></u></a>><b><br>Cc: </b>"<a href="mailto:swift-corelibs-dev@swift.org"><u><font color="#0000FF">swift-corelibs-dev@swift.org</font></u></a>" <<a href="mailto:swift-corelibs-dev@swift.org"><u><font color="#0000FF">swift-corelibs-dev@swift.org</font></u></a>><b><br>Subject: </b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<br><font face="Times New Roman"> </font><br><font face="Helvetica">Hi Brandon,</font><br><font face="Helvetica">Thanks for looking at this! We’ve got plans internally to potentially add a strategy to </font><font size="2" face="Courier New">JSONEncoder</font><font face="Helvetica">/</font><font size="2" face="Courier New">JSONDecoder</font><font face="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 </font><font size="2" face="Courier New">JSONDecoder</font><font face="Helvetica"> while not requiring any special annotations on </font><font size="2" face="Courier New">Codable</font><font face="Helvetica"> types.</font><br><font face="Helvetica">— Itai</font><br><font face="Helvetica">On 30 Aug 2017, at 10:59, Sneed, Brandon via swift-corelibs-dev wrote:</font><br><font color="#777777">Hi everyone,</font><br><font color="#777777"> </font><br><font 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.</font><br><font color="#777777"> </font><br><font color="#777777">Type type conversion, I mean given this JSON payload:</font><br><font color="#777777"> </font><br><font color="#777777">{</font><br><font color="#777777"> "name": "Endeavor”,</font><br><font color="#777777"> "abv": 8.9,</font><br><font color="#777777"> "brewery": "Saint Arnold”,</font><br><font color="#777777"> "style": "ipa"</font><br><font color="#777777">}</font><br><font color="#777777"> </font><br><font color="#777777">and a struct defined as:</font><br><font color="#777777"> </font><br><font color="#777777">struct Beer: Codable {</font><br><font color="#777777"> let name: String</font><br><font color="#777777"> let abv: String</font><br><font color="#777777"> let brewery: String</font><br><font color="#777777"> let style: BeerStyle</font><br><font color="#777777">}</font><br><font color="#777777"> </font><br><font 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:</font><br><font color="#777777"> </font><br><font color="#777777">1. It’s defensive; service types can change without causing my application to crash.</font><br><font 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.</font><br><font color="#777777"> </font><br><font 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.</font><br><font color="#777777"> </font><br><font 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:</font><br><font color="#777777"> </font><br><font color="#777777">struct Beer: Codable, CodingConvertible {</font><br><font color="#777777"> let name: String</font><br><font color="#777777"> let abv: String</font><br><font color="#777777"> let brewery: String</font><br><font color="#777777"> let style: BeerStyle</font><br><font color="#777777">}</font><br><font color="#777777"> </font><br><font 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.</font><br><font color="#777777"> </font><br><font color="#777777">Another option would be to box each type, like so:</font><br><font color="#777777"> </font><br><font color="#777777">struct Beer: Codable {</font><br><font color="#777777"> let name: String</font><br><font color="#777777"> let abv: Convertible<String></font><br><font color="#777777"> let brewery: String</font><br><font color="#777777"> let style: BeerStyle</font><br><font color="#777777">}</font><br><font color="#777777"> </font><br><font color="#777777">This seems tedious for developers, but would show which types are being converted. It does however seriously weaken benefit #1 above.</font><br><font color="#777777"> </font><br><font 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.</font><br><font color="#777777"> </font><br><font color="#777777">I’m very open to alternatives, other ideas, or anything else you might have to say on the subject. Thanks for reading!</font><br><font color="#777777"> </font><br><font color="#777777"> </font><br><font color="#777777"> </font><br><font color="#777777">Brandon Sneed</font><br><font color="#777777"> </font><br><font color="#777777"> </font><br><font color="#777777"> </font><br><font color="#777777"> </font><br><font color="#777777" face="Helvetica">_______________________________________________<br>swift-corelibs-dev mailing list</font><u><font color="#0000FF" face="Helvetica"><br></font></u><a href="mailto:swift-corelibs-dev@swift.org"><u><font color="#0000FF" face="Helvetica">swift-corelibs-dev@swift.org</font></u></a><u><font color="#0000FF" face="Helvetica"><br></font></u><a href="https://urldefense.proofpoint.com/v2/url?u=https-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&d=DwMGaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=gkRZBtsmKeGPCOlAIRJoOA&m=ViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg&s=rVZl8iT3jj1nzuDTCqZ1pkhQIZD3-Byi8PUj50swTUg&e="><u><font color="#777777" face="Helvetica">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</font></u></a><br><font size="2" face="Helvetica">_______________________________________________<br>swift-corelibs-dev mailing list</font><u><font size="2" color="#0000FF" face="Helvetica"><br></font></u><a href="mailto:swift-corelibs-dev@swift.org"><u><font size="2" color="#0000FF" face="Helvetica">swift-corelibs-dev@swift.org</font></u></a><u><font size="2" color="#0000FF" face="Helvetica"><br></font></u><a href="https://urldefense.proofpoint.com/v2/url?u=https-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&d=DwMGaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=gkRZBtsmKeGPCOlAIRJoOA&m=ViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg&s=LQ0cgWtC9rXRTDLu0W58VIukWrsRssHsIMGb9U6Y0MU&e="><u><font size="2" color="#0000FF" face="Helvetica">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</font></u></a></ul></ul><font face="Times New Roman"> </font><tt><font size="2">_______________________________________________<br>swift-corelibs-dev mailing list<br>swift-corelibs-dev@swift.org<br></font></tt><tt><font size="2"><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Dcorelibs-2Ddev&d=DwIGaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=gkRZBtsmKeGPCOlAIRJoOA&m=ViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg&s=zRuNQ3NLxpfhFBewRTkoMWZnpvHlm6Ja-ot9_pwAgqI&e=">https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Dcorelibs-2Ddev&d=DwIGaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=gkRZBtsmKeGPCOlAIRJoOA&m=ViDSVPImta3StTVAcktby2PMF_-du5itzz47jo-tNHg&s=zRuNQ3NLxpfhFBewRTkoMWZnpvHlm6Ja-ot9_pwAgqI&e=</a></font></tt><tt><font size="2"> <br></font></tt><br><br><BR>
</body></html>