|
| 1 | +# Comparison to Swift Codable |
| 2 | + |
| 3 | +Binary Codable is similar to Swift [Codable](https://developer.apple.com/documentation/foundation/archives_and_serialization) in that both define an encodable and decodable type that concrete types are expected to implement and both provide encoders and decoders. |
| 4 | + |
| 5 | +Binary Codable is distinct from Swift Codable in the assumptions it makes about how external representations are structured. Swift Codable assumes that external representations have a pre-determined structure (JSON, PropertyList, etc...). Binary Codable, on the other hand, views external representations purely as binary data. This distinction is reflected in the difference of the APIs provided by the BinaryEncoder and BinaryCoder types. |
| 6 | + |
| 7 | +## Interface comparison |
| 8 | + |
| 9 | +Swift Codable and Binary Codable's related types are outlined below. |
| 10 | + |
| 11 | +| Swift Codable | Binary Codable | |
| 12 | +|:--------------|:------------------| |
| 13 | +| `Codable` | `BinaryCodable` | |
| 14 | +| `Encodable` | `BinaryEncodable` | |
| 15 | +| `Decodable` | `BinaryDecodable` | |
| 16 | + |
| 17 | +| Swift Codable Encoder | Binary Codable Encoder | |
| 18 | +|:-------------------------------|:------------------------------------| |
| 19 | +| `KeyedEncodingContainer` | No equivalent | |
| 20 | +| `UnkeyedEncodingContainer` | `SequentialBinaryEncodingContainer` | |
| 21 | +| `SingleValueEncodingContainer` | No equivalent | |
| 22 | + |
| 23 | +| Swift Codable Encoding Container | Binary Codable Encoding Container | |
| 24 | +|:---------------------------------|:--------------------------------------------------------------------------| |
| 25 | +| `encode(_ value: Int8)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 26 | +| `encode(_ value: Int16)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 27 | +| `encode(_ value: Int32)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 28 | +| `encode(_ value: Int64)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 29 | +| `encode(_ value: UInt8)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 30 | +| `encode(_ value: UInt16)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 31 | +| `encode(_ value: UInt32)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 32 | +| `encode(_ value: UInt64)` | `encode<T>(_ value: T) where T: FixedWidthInteger` | |
| 33 | +| `encode<T>(_ value: T)` | `encode<T>(_ value: T)` | |
| 34 | +| `encode(_ value: String)` | `encode(_ value: String, encoding: String.Encoding, terminator: UInt8?)` | |
| 35 | +| No equivalent | `encode<S>(sequence: S) throws where S: Sequence, S.Element == UInt8` | |
| 36 | + |
| 37 | +| Swift Codable Decoder | Binary Codable Decoder | |
| 38 | +|:-------------------------------|:------------------------------------| |
| 39 | +| `KeyedDecodingContainer` | No equivalent | |
| 40 | +| `UnkeyedDecodingContainer` | `SequentialBinaryDecodingContainer` | |
| 41 | +| `SingleValueDecodingContainer` | No equivalent | |
| 42 | + |
| 43 | +| Swift Codable Decoding Container | Binary Codable Decoding Container | |
| 44 | +|:------------------------------------------------|:------------------------------------------------------------------------| |
| 45 | +| `decode(_ type: Int8.Type) -> Int8` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 46 | +| `decode(_ type: Int16.Type) -> Int16` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 47 | +| `decode(_ type: Int32.Type) -> Int32` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 48 | +| `decode(_ type: Int64.Type) -> Int64` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 49 | +| `decode(_ type: UInt8.Type) -> UInt8` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 50 | +| `decode(_ type: UInt16.Type) -> UInt16` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 51 | +| `decode(_ type: UInt32.Type) -> UInt32` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 52 | +| `decode(_ type: UInt64.Type) -> UInt64` | `decode<T>(_ type: T.Type) -> T where T: FixedWidthInteger` | |
| 53 | +| `decode<T>(_ type: T.Type) where T : Decodable` | `decode<T>(_ type: T.Type) -> T where T: BinaryDecodable` | |
| 54 | +| `decode(_ type: String.Type) -> String` | `decodeString(encoding: String.Encoding, terminator: UInt8?) -> String` | |
| 55 | +| No equivalent | `decode(length: Int) -> Data` | |
| 56 | +| No equivalent | `peek(length: Int) -> Data` | |
0 commit comments