<div dir="ltr">Hi Tony,<div>Sounds really excellent. Looking forward to it. </div><div>Please let me know if there's anything regarding proposal or implementation that I can help with.</div><div>/morten<br><br><div class="gmail_quote"><div dir="ltr">On Thu, Oct 19, 2017 at 5:09 PM Tony Parker <<a href="mailto:anthony.parker@apple.com">anthony.parker@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">Hi Morten,<div><br></div><div>I’ve actually been working on this same idea already and will have something to propose soon.</div><div><br></div><div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div>- Tony<br></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><blockquote type="cite"><div>On Oct 19, 2017, at 2:03 AM, Morten Bek Ditlevsen via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_-1676161584420168186Apple-interchange-newline"></blockquote></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><blockquote type="cite"><div><div dir="ltr"><span style="font-size:13px">Hi all,</span><div style="font-size:13px">At work we have just added Codable support for a whole bunch of model objects in our code base.</div><div style="font-size:13px">Many places we have added CodingKeys enumeration in order to convert the camel cased property names to snake case for our JSON keys.</div><div style="font-size:13px">As an experiment I have tried adding a KeyCodingStrategy option to a copy of the JSONEncoder and JSONDecoder implementations.</div><div style="font-size:13px">This is currently an enumeration with the following values</div><div style="font-size:13px">.original</div><div style="font-size:13px">.snakeCase</div><div style="font-size:13px">.custom((String) -> String)</div><div style="font-size:13px"><br></div><div style="font-size:13px">I just extended CodingKey as follows:</div><div style="font-size:13px"><div>extension CodingKey {</div><div> func stringValue(with encodingStrategy: StructuralEncoder.KeyEncodingStrategy) -> String {</div><div> switch encodingStrategy {</div><div> case .original:</div><div> return stringValue</div><div> case .snakeCase:</div><div> let pattern = "([a-z0-9])([A-Z])"</div><div> let regex = try! NSRegularExpression(pattern: pattern, options: [])</div><div> let range = NSRange(location: 0, length: stringValue.characters.count)</div><div> return regex.stringByReplacingMatches(in: stringValue, options: [], range: range, withTemplate: "$1_$2").lowercased()</div><div> case .custom(let t):</div><div> return t(stringValue)</div><div> }</div><div> }</div><div>}</div><div><br></div><div>and then I replaced all references to key.stringValue with key.stringValue(with: self.encoder.options.keyCodingStrategy)</div><div><br></div><div>This seems to work very nicely.</div><div><br></div><div>So my question is: Do anyone else see the benefit of such an addition to the JSONEncoder and JSONDecoder?</div><div><br></div><div>The downside as I see it, is that the current CodingKeys are guaranteed to be unique by the compiler, and it would be possible to create collisions by using key name transforms.</div><div>Is this downside bigger than the gains?</div></div><div style="font-size:13px"><br></div><div style="font-size:13px">One advantage is that one could argue that one CodingKey strategy may not fit all serialization mechanisms. For instance one might wish to have upper camel cased keys in Plists (just an example) and snake case in JSON. This method could easily support this, while the current CodingKeys strategy cannot...</div><div style="font-size:13px"><br></div><div style="font-size:13px">Looking forward to hearing feedback.</div><div style="font-size:13px"><br></div><div style="font-size:13px">Sincerely,</div><div style="font-size:13px">/morten</div><div><br></div></div></div></blockquote></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><blockquote type="cite"><div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div></div></div></blockquote></div></div></div>