{"id":26638,"date":"2019-09-18T17:21:51","date_gmt":"2019-09-18T21:21:51","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=26638"},"modified":"2019-09-18T17:21:51","modified_gmt":"2019-09-18T21:21:51","slug":"serializing-heterogenous-arrays-with-codable","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/09\/18\/serializing-heterogenous-arrays-with-codable\/","title":{"rendered":"Serializing Heterogenous Arrays With Codable"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/nicklockwood\/status\/1173751474385563649\">Nick Lockwood<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/nicklockwood\/status\/1173751474385563649\">\n<p>Something I&rsquo;ve spent time on recently is the problem of serializing heterogenous arrays (arrays containing multiple types) in Swift using <code>Codable<\/code>.<\/p>\n<p>Here&rsquo;s <a href=\"https:\/\/gist.github.com\/nicklockwood\/833fabacbc4b2d11ae7c7d4752b8fd18\">a pattern<\/a> I&rsquo;ve found that works pretty well, using a protocol and a type-erased wrapper.<\/p>\n<p>Normally in Swift you do polymorphism by <em>either<\/em> using a protocol <em>or<\/em> an enum (for open or closed sets, respectively). This approach requires you to use <em>both<\/em>, which is slightly odd, and it inherently only supports closed sets, but it&rsquo;s relatively little code to add new cases.<\/p>\n<p>[&#8230;]<\/p>\n<p>The other disadvantage is that containing types must use Array&lt;AnyFoo&gt; rather than Array&lt;Foo&gt;, otherwise they can&rsquo;t use automatic Codable synthesis.<\/p>\n<\/blockquote>\n\n<p>This is necessary because, unlike <code>NSCoding<\/code>, <code>Codable<\/code> does not store the type information in the archive. It has to be provided when decoding.<\/p>\n\n<p><a href=\"https:\/\/twitter.com\/drewmccormack\/status\/1173843580491894784\">Drew McCormack<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/drewmccormack\/status\/1173843580491894784\"><p>I used a <a href=\"https:\/\/github.com\/mentalfaculty\/Anomalii\/blob\/master\/Sources\/Anomalii\/Expression.swift\">similar approach<\/a> when messing with genetic programming last year. Began with a switch like you,  but in the end went to a dictionary mapping of types.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/IanKay\/status\/1173911045984989184\">Ian Keen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/IanKay\/status\/1173911045984989184\"><p>If the underlying format doesn&rsquo;t matter I have a fairly nice generic solution <a href=\"https:\/\/gist.github.com\/IanKeen\/a2f461d975152ab234de14e7d0ca19aa\">here<\/a>.<\/p>\n<p>the <code>DecodingRoutine<\/code> is just a wrapper around <code>Decodable.inits<\/code> that you&rsquo;d normally create <em>specific<\/em> boxes for. And the ones I&rsquo;ve provided take advantage of the fact AV cases without values are functions<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Nick Lockwood: Something I&rsquo;ve spent time on recently is the problem of serializing heterogenous arrays (arrays containing multiple types) in Swift using Codable. Here&rsquo;s a pattern I&rsquo;ve found that works pretty well, using a protocol and a type-erased wrapper. Normally in Swift you do polymorphism by either using a protocol or an enum (for open [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-09-18T21:21:56Z","apple_news_api_id":"f8604ac0-3980-4176-9de8-3ff1e9dbf80e","apple_news_api_modified_at":"2019-09-18T21:21:57Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A-GBKwDmAQXad6D_x6dv4Dg","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,1610,30,1609,71,1774,901],"class_list":["post-26638","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-programming","tag-swift-codable","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=26638"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26638\/revisions"}],"predecessor-version":[{"id":26640,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26638\/revisions\/26640"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=26638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=26638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=26638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}