{"id":34625,"date":"2022-01-05T16:06:12","date_gmt":"2022-01-05T21:06:12","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=34625"},"modified":"2022-01-05T16:06:12","modified_gmt":"2022-01-05T21:06:12","slug":"encoding-and-decoding-references-with-codable","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/01\/05\/encoding-and-decoding-references-with-codable\/","title":{"rendered":"Encoding and Decoding References with Codable"},"content":{"rendered":"<p><a href=\"https:\/\/rambo.codes\/posts\/2022-01-04-encoding-and-decoding-references-to-other-types-with-codable\">Guilherme Rambo<\/a>:<\/p>\n<blockquote cite=\"https:\/\/rambo.codes\/posts\/2022-01-04-encoding-and-decoding-references-to-other-types-with-codable\">\n<p>The <code>CodableReference<\/code> property wrapper implements <code>Codable<\/code> and takes care of encoding only the <code>id<\/code> of the wrapped value, and decoding the <code>id<\/code> and resolving the reference when it&rsquo;s being decoded.<\/p>\n<p>There is a missing piece of the puzzle though: how do we figure out which <code>Category<\/code> corresponds to any given category ID when decoding <code>Post<\/code>?<\/p><p>The solution I came up with was to take advantage of the <code>userInfo<\/code> property in <code>JSONDecoder<\/code> in order to provide the collection of models that the property wrapper can use while decoding.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/05\/08\/resilient-decoding-in-swift\/\">Resilient Decoding in Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/09\/18\/serializing-heterogenous-arrays-with-codable\/\">Serializing Heterogenous Arrays With Codable<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/04\/08\/codextended-extension-for-swifts-codable\/\">Codextended: Extension for Swift&rsquo;s Codable<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Guilherme Rambo: The CodableReference property wrapper implements Codable and takes care of encoding only the id of the wrapped value, and decoding the id and resolving the reference when it&rsquo;s being decoded. There is a missing piece of the puzzle though: how do we figure out which Category corresponds to any given category ID when [&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":"2022-01-05T21:06:14Z","apple_news_api_id":"4d33a3a7-c3fa-4f15-b70f-35edf544b8ee","apple_news_api_modified_at":"2022-01-05T21:06:15Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ATTOjp8P6TxW3DzXt9US47g","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":[71,1774,901],"class_list":["post-34625","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-codable","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34625","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=34625"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34625\/revisions"}],"predecessor-version":[{"id":34626,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34625\/revisions\/34626"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=34625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=34625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=34625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}