{"id":19135,"date":"2017-10-06T16:34:11","date_gmt":"2017-10-06T20:34:11","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=19135"},"modified":"2019-01-18T10:29:52","modified_gmt":"2019-01-18T15:29:52","slug":"when-jsondecoder-meets-the-real-world","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/10\/06\/when-jsondecoder-meets-the-real-world\/","title":{"rendered":"When JSONDecoder Meets the Real World"},"content":{"rendered":"<p><a href=\"http:\/\/davelyon.net\/2017\/08\/16\/jsondecoder-in-the-real-world\">Dave Lyon<\/a>:<\/p>\n<blockquote cite=\"http:\/\/davelyon.net\/2017\/08\/16\/jsondecoder-in-the-real-world\"><p>Take for example the &ldquo;empty&rdquo; object in JSON. You might expect that an object nested under another object would either be a complete and valid object or would be <code>null<\/code> &#x2013; and this is what JSONDecoder expects as well. But in many cases an API might return an empty object (that looks like <code><span>{}<\/span><\/code>) and cause your JSON decoding to fail even if you&rsquo;re decoding in to an Optional.<\/p><p>When faced with this issue recently I came up with a protocol based solution to work around this issue. I also made sure that the behavior was &ldquo;opt-in&rdquo; so that its possible to be flexible if there are differences between API endpoints.<\/p><p>[&#8230;]<\/p><p>Another issue that I hope doesn&rsquo;t come up too often, is that APIs that have survived for a long time in the wild can sometimes have &ldquo;versions&rdquo; (if you&rsquo;re lucky) or even potentially just have different representations for the same object based on which endpoint is requested. One way to handle this is to &ldquo;smuggle&rdquo; some context in to your decoding methods via the Decoder&rsquo;s <code>userInfo<\/code> dictionary. Of course we would prefer to have a more structured contract for these and avoid any &ldquo;Stringly Typed&rdquo; interfaces, so we&rsquo;ll want to leverage extensions to add a real interface to the JSONDecoder.<\/p><\/blockquote>\n\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2017\/07\/14\/swift-codable\/\">Swift.Codable<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2017\/06\/30\/swift-4-json-with-encoder-and-encodable\/\">Swift 4: JSON With Encoder and Encodable<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Dave Lyon: Take for example the &ldquo;empty&rdquo; object in JSON. You might expect that an object nested under another object would either be a complete and valid object or would be null &#x2013; and this is what JSONDecoder expects as well. But in many cases an API might return an empty object (that looks like [&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-01-18T15:29:54Z","apple_news_api_id":"a40b1440-c4a7-4e84-88cb-4bbadb3802fa","apple_news_api_modified_at":"2019-01-18T15:29:55Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ApAsUQMSnToSIy0u62zgC-g","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":[507,71,1774,901],"class_list":["post-19135","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-json","tag-programming","tag-swift-codable","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19135","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=19135"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19135\/revisions"}],"predecessor-version":[{"id":19136,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19135\/revisions\/19136"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=19135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=19135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=19135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}