A template for creating your own Swift Codable encoders and decoders
Switch branches/tags
Nothing to show
Clone or download
mattt Merge pull request #2 from jameshartt/patch-1
Add 'path' property to Swift 4 manifest.
Latest commit 53aa0f5 Nov 2, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources Initial commit May 7, 2018
Tests Initial commit May 7, 2018
.gitignore Initial commit May 7, 2018
LICENSE.md Initial commit May 7, 2018
Package.swift Add 'path' property to Swift 4 manifest. Oct 30, 2018
README.md Initial commit May 7, 2018


DIY Codable Encoder / Decoder Kit

In Swift 4, a type that conforms to the Codable protocol can be encoded to or decoded from representations for any format that implements a corresponding Encoder or Decoder type.

At the time of its release, the only reference implementations for these types were the Foundation framework's JSONEncoder / JSONDecoder and PropertyListEncoder and PropertyListDecoder. The implementation details of these types, however, are obfuscated by translation logic from JSONSerialization and PropertyListSerialization.

This repository provides a template that makes it easier for developers to create encoders and decoders for custom formats. The template includes stubbed placeholders for the required types and methods as well as simple tests for encoding and decoding Codable types.

This general structure was used to implement a Codable-compatible encoder and decoder for the MessagePack format.

For more information about the design and implementation of custom encoder and decoder types, see Chapter 7 of Flight School Guide to Swift Codable.


  • Clone this repository
  • Find all instances of the "<#Format#>" placeholder and replace with the name of your own format
  • Replace the leading underscores in the ___Decoder.swift and ___Encoder.swift files, as well as the source files in the Tests directory
  • Run the command swift package generate-xcodeproj in the root project directory
  • Fill in the missing implementation accordingly

Encoder Structure

public class <#Format#>Encoder {
    public func encode<T>(_ value: T) throws -> Data
                        where T : Encodable

final class _<#Format#>Encoder: Encoder {
    final class SingleValueContainer: SingleValueEncodingContainer
    final class UnkeyedContainer: UnkeyedEncodingContainer
    final class KeyedContainer<Key>: KeyedEncodingContainerProtocol
            where Key: CodingKey

protocol <#Format#>EncodingContainer: class {}

Decoder Structure

public class <#Format#>PackDecoder {
    public func decode<T>(_ type: T.Type,
                          from data: Data) throws -> T
                        where T : Decodable

final class _<#Format#>Decoder: Decoder {
    final class SingleValueContainer: SingleValueDecodingContainer
    final class UnkeyedContainer: UnkeyedDecodingContainer
    final class KeyedContainer<Key>: KeyedContainer
            where Key: CodingKey

protocol <#Format#>DecodingContainer: class {}




Mattt (@mattt)