<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="">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.<div class=""><br class=""></div><div class="">Struct Beer then looks like:</div><div class=""><br class=""></div><div class="">struct Beer: Codable {</div><div class=""> let name: String</div><div class=""> let abv: ABV</div><div class=""> …</div><div class="">}</div><div class=""><br class=""></div><div 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.</div><div class=""><br class=""></div><div class="">- Tony<br class=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Aug 30, 2017, at 2:42 PM, Sneed, Brandon <<a href="mailto:brsneed@ebay.com" class="">brsneed@ebay.com</a>> 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">Thanks Youming,<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">That’s not quite what I meant. I may have misinterpreted what Tony was saying though.<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">I wanted to do conversion on String, not the containing type. 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. For a small struct, not a big deal, for larger ones, it is. Something like this is what I tried:<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">import Cocoa<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let jsonString = "{" +<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> "\"name\": \"Endeavor\"," +<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> "\"abv\": 8.9," +<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> "\"brewery\": \"Saint Arnold\"," +<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> "\"style\": \"ipa\"}"<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">struct Beer: Codable {<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> let name: String<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> let abv: String<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> let brewery: String<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> let style: String<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">extension String {<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> init(from decoder: Decoder) throws {<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> print("i got hit.")<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> let value = try decoder.singleValueContainer().decode(String.self)<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""> self.init(value)<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" 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: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let jsonData = jsonString.data(using: .utf8)<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let decoder = JSONDecoder()<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class="">let beer = try! decoder.decode(Beer.self, from: jsonData!)<o:p class=""></o:p></span></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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: "Times New Roman";" 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><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: "Times New Roman";" 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: "Times New Roman";" 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: "Times New Roman";" class=""><span style="font-size: 11pt; font-family: Calibri;" class=""><o:p class=""> </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: "Times New Roman";" class=""><b class=""><span style="font-family: Calibri;" class="">From:<span class="Apple-converted-space"> </span></span></b><span style="font-family: Calibri;" class="">Youming Lin <<a href="mailto:ylin@us.ibm.com" style="color: purple; text-decoration: underline;" class="">ylin@us.ibm.com</a>><br class=""><b class="">Date:<span class="Apple-converted-space"> </span></b>Wednesday, August 30, 2017 at 2:35 PM<br class=""><b class="">To:<span class="Apple-converted-space"> </span></b>"Sneed, Brandon" <<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class="">brsneed@ebay.com</a>><br class=""><b class="">Cc:<span class="Apple-converted-space"> </span></b>Tony Parker <<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class="">anthony.parker@apple.com</a>>, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>>, "<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a>" <<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a>><br class=""><b class="">Subject:<span class="Apple-converted-space"> </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: "Times New Roman";" class=""><o:p class=""> </o:p></div></div><p style="margin-right: 0in; margin-left: 0in; font-size: 12pt; font-family: "Times New Roman";" 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> import Foundation</span><br class=""><span style="font-family: Monaco;" class="">2> struct A: Codable {<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">3. var integer: Int<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">4.<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">5. public init(integer: Int) {<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">6. self.integer = integer<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">7. }<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">8.<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">9. public init(from decoder: Decoder) throws {<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">10. print("Custom decoder")<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">11. let container = try decoder.container(keyedBy: CodingKeys.self)<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">12. integer = try container.decode(Int.self, forKey: .integer)<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">13. }<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">14. }<span class="Apple-converted-space"> </span></span><br class=""><span style="font-family: Monaco;" class="">15.<span class="Apple-converted-space"> </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=""><span id="cid:image001.gif@01D3219E.3340CAF0"><image001.gif></span><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"> </span></span><span style="font-size: 10pt;" class="">"Sneed, Brandon" <<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class="">brsneed@ebay.com</a>></span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">To:<span class="Apple-converted-space"> </span></span><span style="font-size: 10pt;" class="">Youming Lin <<a href="mailto:ylin@us.ibm.com" style="color: purple; text-decoration: underline;" class="">ylin@us.ibm.com</a>></span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">Cc:<span class="Apple-converted-space"> </span></span><span style="font-size: 10pt;" class="">Tony Parker <<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class="">anthony.parker@apple.com</a>>, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>>, "<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a>" <<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a>></span><br class=""><span style="font-size: 10pt; color: rgb(95, 95, 95);" class="">Date:<span class="Apple-converted-space"> </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"> </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></p><div class="MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";"><hr size="2" width="100%" noshade="" align="left" style="color: rgb(128, 145, 165);" class=""></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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"> </span></b>Youming Lin <<a href="mailto:ylin@us.ibm.com" style="color: purple; text-decoration: underline;" class="">ylin@us.ibm.com</a>><b class=""><br class="">Date:<span class="Apple-converted-space"> </span></b>Wednesday, August 30, 2017 at 1:18 PM<b class=""><br class="">To:<span class="Apple-converted-space"> </span></b>"Sneed, Brandon" <<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class="">brsneed@ebay.com</a>><b class=""><br class="">Cc:<span class="Apple-converted-space"> </span></b>Tony Parker <<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class="">anthony.parker@apple.com</a>>, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>>, "<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a>" <<a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a>><b class=""><br class="">Subject:<span class="Apple-converted-space"> </span></b>Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode<o:p class=""></o:p></div><p style="margin-right: 0in; margin-left: 0in; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 10pt;" class="">Brandon<br class=""><br class="">></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"> </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&data=02%7C01%7Cbrsneed%40ebay.com%7Cebd9669255614ce4dccc08d4efef218c%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397257717072726&sdata=TIZrCfocLCJGcfOj%2B9V2FEiXPSTKN4hnL1Rm03GqHvk%3D&reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt;" 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=""><span id="cid:image002.gif@01D3219E.3340CAF0"><image002.gif></span><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"> </span></span><span style="font-size: 10pt;" class="">"Sneed, Brandon via swift-corelibs-dev" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>><span style="color: rgb(95, 95, 95);" class=""><br class="">To:<span class="Apple-converted-space"> </span></span>Tony Parker <<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class="">anthony.parker@apple.com</a>><span style="color: rgb(95, 95, 95);" class=""><br class="">Cc:<span class="Apple-converted-space"> </span></span>"<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>><span style="color: rgb(95, 95, 95);" class=""><br class="">Date:<span class="Apple-converted-space"> </span></span>08/30/2017 03:07 PM<span style="color: rgb(95, 95, 95);" class=""><br class="">Subject:<span class="Apple-converted-space"> </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"> </span></span><a href="mailto:swift-corelibs-dev-bounces@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev-bounces@swift.org</a></span><o:p class=""></o:p></p><div class="MsoNormal" style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";"><hr size="2" width="100%" noshade="" align="left" style="color: rgb(170, 170, 170);" class=""></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" 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"> </span></b><<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class="">anthony.parker@apple.com</a>> on behalf of Tony Parker <<a href="mailto:anthony.parker@apple.com" style="color: purple; text-decoration: underline;" class="">anthony.parker@apple.com</a>><b class=""><br class="">Date:<span class="Apple-converted-space"> </span></b>Wednesday, August 30, 2017 at 11:30 AM<b class=""><br class="">To:<span class="Apple-converted-space"> </span></b>"Sneed, Brandon" <<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class="">brsneed@ebay.com</a>><b class=""><br class="">Cc:<span class="Apple-converted-space"> </span></b>Itai Ferber <<a href="mailto:iferber@apple.com" style="color: purple; text-decoration: underline;" class="">iferber@apple.com</a>>, "<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>><b class=""><br class="">Subject:<span class="Apple-converted-space"> </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"> </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"> </span><o:p class=""></o:p></div><div style="margin: 0in 0in 0.0001pt 2in; font-size: 12pt; font-family: "Times New Roman";" class="">On Aug 30, 2017, at 11:24 AM, Sneed, Brandon via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>> 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"> </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"> </span></b><<a href="mailto:iferber@apple.com" style="color: purple; text-decoration: underline;" class="">iferber@apple.com</a>> on behalf of Itai Ferber <<a href="mailto:iferber@apple.com" style="color: purple; text-decoration: underline;" class="">iferber@apple.com</a>><b class=""><br class="">Date:<span class="Apple-converted-space"> </span></b>Wednesday, August 30, 2017 at 11:22 AM<b class=""><br class="">To:<span class="Apple-converted-space"> </span></b>"Sneed, Brandon" <<a href="mailto:brsneed@ebay.com" style="color: purple; text-decoration: underline;" class="">brsneed@ebay.com</a>><b class=""><br class="">Cc:<span class="Apple-converted-space"> </span></b>"<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>" <<a href="mailto:swift-corelibs-dev@swift.org" style="color: purple; text-decoration: underline;" class="">swift-corelibs-dev@swift.org</a>><b class=""><br class="">Subject:<span class="Apple-converted-space"> </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"> </span></span><span style="font-size: 10pt; font-family: "Courier New";" class="">JSONEncoder</span><span style="font-family: Helvetica;" class="">/</span><span style="font-size: 10pt; font-family: "Courier New";" class="">JSONDecoder</span><span style="font-family: Helvetica;" class=""><span class="Apple-converted-space"> </span>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"> </span></span><span style="font-size: 10pt; font-family: "Courier New";" class="">JSONDecoder</span><span style="font-family: Helvetica;" class=""><span class="Apple-converted-space"> </span>while not requiring any special annotations on<span class="Apple-converted-space"> </span></span><span style="font-size: 10pt; font-family: "Courier New";" class="">Codable</span><span style="font-family: Helvetica;" class=""><span class="Apple-converted-space"> </span>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<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="">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;" 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&data=02%7C01%7Cbrsneed%40ebay.com%7C6290a819253b47b0eb2408d4efe454ae%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397211330551372&sdata=GkxX7xyhtHr1zqISNmjeBhvOkVoZdOihXcaugXSK9tA%3D&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;" 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&data=02%7C01%7Cbrsneed%40ebay.com%7C6290a819253b47b0eb2408d4efe454ae%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397211330551372&sdata=afQLw7%2FVip%2Bts1P60ALOHrTNap93519tWLjYzkS2o5Q%3D&reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt; font-family: Helvetica;" class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</span></a><o:p class=""></o:p></div><div style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman";" class=""><span style="font-size: 10pt; font-family: "Courier New";" 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="">swift-corelibs-dev@swift.org</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&data=02%7C01%7Cbrsneed%40ebay.com%7Cebd9669255614ce4dccc08d4efef218c%7C46326bff992841a0baca17c16c94ea99%7C0%7C0%7C636397257717072726&sdata=jJucdx3j9pb2ylcsHYaVAtze%2B%2BQWr%2F%2Bm2GpBRlHaBTk%3D&reserved=0" style="color: purple; text-decoration: underline;" class=""><span style="font-size: 10pt; font-family: "Courier New";" class="">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=</span></a><span style="font-size: 10pt; font-family: "Courier New";" class=""><span class="Apple-converted-space"> </span></span><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><br class=""></div></div></body></html>