{"id":23867,"date":"2019-01-07T16:57:15","date_gmt":"2019-01-07T21:57:15","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23867"},"modified":"2019-01-07T16:57:15","modified_gmt":"2019-01-07T21:57:15","slug":"why-doesnt-jsonencoder-conform-to-the-encoder-protocol","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/01\/07\/why-doesnt-jsonencoder-conform-to-the-encoder-protocol\/","title":{"rendered":"Why Doesn&rsquo;t JSONEncoder Conform to the Encoder Protocol?"},"content":{"rendered":"<p><a href=\"https:\/\/kaitlinmahar.com\/2019\/01\/07\/jsonencoder-encoder.html\">Kaitlin Mahar<\/a>:<\/p>\n<blockquote cite=\"https:\/\/kaitlinmahar.com\/2019\/01\/07\/jsonencoder-encoder.html\"><p>Inspecting <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/stdlib\/public\/Darwin\/Foundation\/JSONEncoder.swift\">the source code<\/a> for <code>JSONEncoder<\/code>, we see it&rsquo;s a <a href=\"https:\/\/kaitlinmahar.com\/2018\/12\/29\/open-swift.html\"><code>open<\/code> type<\/a> that internally uses a <code>private<\/code> type <code>_JSONEncoder<\/code>, which <em>does<\/em> conform to <code>Encoder<\/code>.<\/p><p>[&#8230;]<\/p><p>But why were they designed that way? Why not just make <code>JSONEncoder<\/code> an <code>Encoder<\/code> too?<\/p><p>In short, the answer is that they provide very different APIs. The <code>JSONEncoder<\/code> API is designed to provide a single, simple entry point into encoding, and the <code>Encoder<\/code> protocol provides a completely different API for customizing how types are encoded.<\/p><\/blockquote>\n<p>This makes sense, though it&rsquo;s kind of odd that the facade and the protocol for the private types both use the same word (<code>Encoder<\/code>\/<code>Decoder<\/code>). Cocoa distinguishes between <code>NSArchiver<\/code>\/<code>NSUnarchiver<\/code>, which you use directly, and <code>NSCoder<\/code>, which is passed to you. Although, that&rsquo;s also a bit messy because the archivers are subclasses of <code>NSCoder<\/code>, and so all the other methods are still there.<\/p>","protected":false},"excerpt":{"rendered":"<p>Kaitlin Mahar: Inspecting the source code for JSONEncoder, we see it&rsquo;s a open type that internally uses a private type _JSONEncoder, which does conform to Encoder.[&#8230;]But why were they designed that way? Why not just make JSONEncoder an Encoder too?In short, the answer is that they provide very different APIs. The JSONEncoder API is designed [&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-07T21:57:17Z","apple_news_api_id":"3cc8942b-ca11-4b01-a519-9c7b4fd3d147","apple_news_api_modified_at":"2019-01-07T21:57:18Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/APMiUK8oRSwGlGZx7T9PRRw","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":[69,31,1610,30,1609,71,901],"class_list":["post-23867","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23867","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=23867"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23867\/revisions"}],"predecessor-version":[{"id":23868,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23867\/revisions\/23868"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}