<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="">Hi Arsen,<div class=""><br class=""></div><div class="">What’s the difference between providing a method that takes a transformable object on the keyed container protocol and just transforming it yourself before you encode it?</div><div class=""><br class=""></div><div class="">- Tony<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 18, 2017, at 8:51 AM, Arsen Gasparyan via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Hello everyone,</div><div class=""><br class=""></div><div class="">I’m suggesting to add a new way to encode/decode JSON properties for relevantly complex data formats such as regular expressions,&nbsp;well-known text, hex colours, custom date formats.</div><div class=""><br class=""></div><div class=""><span style="font-size: 14px;" class="">The problem:</span></div><div class=""><span style="font-size: 14px;" class=""><br class=""></span></div><div class="">Image you have a struct called House</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class=""><span style="color: #36568a" class="">struct</span> House {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class="">&nbsp; &nbsp; <span style="color: #36568a" class="">let</span> color: <span style="color: #c35900" class="">UIColor</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class="">}</div></div><div class=""><div class=""><br class=""></div><div class="">and it has a properly called <i class="">color</i> and the color represented in JSON as hex value (#ffffff). Currently to make it working you have to extract the underline value (string with a hex value) and then try to make a UIColor from it. It works but it makes you to copy/past a lot of code and it leads to problems. Also it shifts focus from <i class="">what to decode</i> to <i class="">how to decode</i>.</div></div><div class=""><br class=""></div><div class=""><span style="font-size: 14px;" class="">The suggested solution:</span></div><div class=""><span style="font-size: 14px;" class=""><br class=""></span></div><div class="">I suggest that we have to introduce protocols for classes that will encapsulate transformation from a source data type to a destination data type. The source data types are all existing data that support of decoding.&nbsp;</div><div class=""><br class=""></div><div class="">We will provide only protocols (one for decoding and one for encoding) and users will be able to create transformers for their own data types.</div><div class=""><br class=""></div><div class=""><span style="font-size: 14px;" class="">The implementation:</span></div><div class=""><span style="font-size: 14px;" class=""><br class=""></span></div><div class="">The implementation is fairly easy. We only need introduce two protocols (encoding/decoding) and add a method to&nbsp;KeyedEncodingContainerProtocol that will accept a key and a transformer. In the method we will extract source data from JSON and then will ask the transformer to try to convert it to the desire data type.</div><div class=""><br class=""></div><div class="">Example of the decoding protocol:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #36568a" class="">protocol</span> DecodingTransformer {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(54, 86, 138);" class=""><span style="" class="">&nbsp; &nbsp; </span>associatedtype<span style="" class=""> Input: </span><span style="color: #c35900" class="">Decodable</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(54, 86, 138);" class=""><span style="" class="">&nbsp; &nbsp; </span>associatedtype<span style="" class=""> Output</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">&nbsp; &nbsp; <span style="color: #36568a" class="">func</span> transform(<span style="color: #36568a" class="">_</span> decoded: <span style="color: #c35900" class="">Input</span>) <span style="color: #36568a" class="">throws</span> -&gt; <span style="color: #c35900" class="">Output</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">}</div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><span style="font-size: 14px;" class="">Inspired by:&nbsp;<a href="https://github.com/Hearst-DD/ObjectMapper#custom-transforms" class="">https://github.com/Hearst-DD/ObjectMapper#custom-transforms</a></span></div><div class=""><span style="font-size: 14px;" class=""><br class=""></span></div><div class="">Cheers,</div><div class="">Arsen</div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>