<html><body bgcolor="#FFFFFF"><p><font size="2">One downside I see is that the encoder wouldn't know if this specific field was originally a number instead of a string. If the same Codable struct is used for GET and POST, for example, the post-encode JSON string could be invalid because the server expects a number instead of a string for that field.<br><br>An explicit mapping of JSON value type to Codable ivar type would be needed, but this can create validation issues because while all numbers can be converted to strings (decode is OK), not all strings can be converted to numbers (encode is not OK).<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__=8FBB0B1FDFF0149A8f9e8a93df938690918c8FB@" border="0" alt="Inactive hide details for "Sneed, Brandon via swift-corelibs-dev" ---08/30/2017 01:00:38 PM---Hi everyone, Just throwing this o"><font size="2" color="#424282">"Sneed, Brandon via swift-corelibs-dev" ---08/30/2017 01:00:38 PM---Hi everyone, Just throwing this out to see if anyone else is working on this, or has opinions/sugges</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">"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 01:00 PM</font><br><font size="2" color="#5F5F5F">Subject: </font><font size="2">[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 everyone,<br> <br>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.<br> <br>Type type conversion, I mean given this JSON payload:<br> <br>{<br> "name": "Endeavor”,<br> "abv": 8.9,<br> "brewery": "Saint Arnold”,<br> "style": "ipa"<br>}<br> <br>and a struct defined as:<br> <br>struct Beer: Codable {<br> let name: String<br> let abv: String<br> let brewery: String<br> let style: BeerStyle<br>}<br> <br>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:<br> <br>1. It’s defensive; service types can change without causing my application to crash.<br>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.<br> <br>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.<br> <br>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:<br> <br>struct Beer: Codable, CodingConvertible {<br> let name: String<br> let abv: String<br> let brewery: String<br> let style: BeerStyle<br>}<br> <br>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.<br> <br>Another option would be to box each type, like so:<br> <br>struct Beer: Codable {<br> let name: String<br> let abv: Convertible<String><br> let brewery: String<br> let style: BeerStyle<br>}<br> <br>This seems tedious for developers, but would show which types are being converted. It does however seriously weaken benefit #1 above.<br> <br>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.<br> <br>I’m very open to alternatives, other ideas, or anything else you might have to say on the subject. Thanks for reading!<br> <br> <br> <br>Brandon Sneed<br> <br> <br> <br> <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=utPJ8lPG7RUnVBM2S83fXgDyBRVtivz1iTkYslFYIKI&s=JYMXmKeoK1ETGzoGDIMdIL6M1MNEo8ij571lOudNSWw&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=utPJ8lPG7RUnVBM2S83fXgDyBRVtivz1iTkYslFYIKI&s=JYMXmKeoK1ETGzoGDIMdIL6M1MNEo8ij571lOudNSWw&e=</a></font></tt><tt><font size="2"> <br></font></tt><br><br><BR>
</body></html>