A place to finally learn about custom encoders and decoders.
- https://www.whynotestflight.com/excuses/how-do-custom-encoders-work/
- https://www.whynotestflight.com/excuses/and-what-can-i-make-a-custom-encoder-do/
To work on this package I primarily used 3 resources, duplicating them line by line. No copy-paste. Xcode does a ton of providing missing code to be in compliance so that was less tedious than one may think. Some of the variable names will be changed from the example code. This is on purpose to make sure I don't start copy pasting and that I really do under stand what is connected to what.
- https://forums.swift.org/t/future-of-codable-and-json-coders-in-swift-6-hoping-for-a-rework/69542
- https://forums.swift.org/t/serialization-in-swift/46641
Paulo Mattos provided an epic answer.
-
StringsCoder/KeyValueEncoder.swift
-
Tests:
StringsCoderTests.swift
-
Strings
- https://talk.objc.io/episodes/S01E348-routing-with-codable-encoding
- https://github.com/objcio/S01E348-routing-with-codable-encoding/blob/main/Sources/CodableRouting/Encoding.swift
Very good video on making a router coder from an enum.
objcCoder/RouterExample.swift
- Tests:
RouterExampleTests.swift
- https://github.com/apple/swift-foundation/blob/main/Sources/FoundationEssentials/JSON/JSONEncoder.swift#L629
- https://github.com/apple/swift-foundation/blob/14815845f3531505311e9ae30e66055a46a4eb12/Sources/FoundationEssentials/CodableWithConfiguration.swift#L20
- https://github.com/apple/swift-corelibs-foundation/blob/7d40966ed21dc39846103a429f84f426be1f28da/Darwin/Foundation-swiftoverlay/JSONEncoder.swift#L448
- https://github.com/search?q=repo%3Aapple%2Fswift-evolution%20Encoder&type=code
- https://github.com/apple/swift-evolution/blob/main/proposals/0166-swift-archival-serialization.md
- https://github.com/apple/swift-evolution/blob/main/proposals/0167-swift-encoders.md
- https://github.com/apple/swift-evolution/blob/main/proposals/0239-codable-range.md
- https://github.com/apple/swift-evolution/blob/main/proposals/0295-codable-synthesis-for-enums-with-associated-values.md -- https://github.com/apple/swift-evolution/blob/proposals/0320-codingkeyrepresentable.md
A beast, but very instructive. Started from line 226 of the one in 5.10 and worked my way out until it worked.
open func encode<T: Encodable>(_ value: T) throws -> Data {
let value: JSONValue = try encodeAsJSONValue(value)
let writer = JSONValue.Writer(options: self.outputFormatting)
let bytes = writer.writeValue(value)
return Data(bytes)
}
That got ripped apart and turned into the first LineCoder. Then I found the one in FoundationEssentials and liked the way it had been reworked so tore everything out again.
-
https://github.com/apple/swift-openapi-runtime/tree/release/0.2.x/Sources/OpenAPIRuntime/URICoder
-
https://github.com/apple/swift-http-structured-headers/blob/main/Sources/StructuredFieldValues/
-
This one is extra interesting because its encoded straight to an output stream. No stored data at all. https://github.com/apple/swift-package-manager/blob/a5f9b6cf7ceeea13b7db828b5eece2ca9e0df445/Sources/Commands/Utilities/PlainTextEncoder.swift
-
https://forums.swift.org/t/how-can-i-encode-a-struct-to-data-binary/68652
-
String.propertyListFromStringsFileFormat()
-
Dictionary.descriptionInStringsFileFormat
-
2017 Mike Ash article: https://www.mikeash.com/pyblog/friday-qa-2017-07-28-a-binary-coder-for-swift.html
-
https://www.fivestars.blog/articles/codable-swift-dictionaries/
-
https://www.swiftbysundell.com/articles/customizing-codable-types-in-swift/
-
https://github.com/swiftcsv/SwiftCSV (does not use custom encoder)
-
https://www.donnywals.com/writing-custom-json-encoding-and-decoding-logic/
-
https://forums.swift.org/t/proper-way-to-structure-containers-in-new-coders/11712/2
-
https://forums.swift.org/t/se-0295-codable-synthesis-for-enums-with-associated-values/42408/15
-
https://forums.swift.org/t/codable-requirements-mystery/68690
-
https://github.com/search?q=org%3AApple%20.nestedContainer(keyedBy%3A&type=code
-
https://github.com/search?q=org%3AApple%20.nestedUnkeyedContainer(forKey&type=code