@@ -24,7 +24,18 @@ public protocol ExCodable: Codable {
2424 static var keyMapping : [ KeyMap < Root > ] { get }
2525}
2626
27+ // default implementation for Encodable
28+ public extension ExCodable where Root == Self {
29+ func encode( to encoder: Encoder ) throws {
30+ try encode ( to: encoder, with: Self . keyMapping)
31+ }
32+ }
33+
34+ // MARK: - keyMapping
35+
36+ // encode/decode
2737public extension ExCodable {
38+ static var keyMapping : [ KeyMap < Root > ] { [ ] } // default implementation for optional property
2839 func encode( to encoder: Encoder , with keyMapping: [ KeyMap < Self > ] , nonnull: Bool = false , throws: Bool = false ) throws {
2940 try keyMapping. forEach { try $0. encode ( self , encoder, nonnull, `throws`) }
3041 }
@@ -35,13 +46,6 @@ public extension ExCodable {
3546 try keyMapping. forEach { try $0. decodeReference ? ( self , decoder, nonnull, `throws`) }
3647 }
3748}
38- public extension ExCodable where Root == Self {
39- func encode( to encoder: Encoder ) throws {
40- try encode ( to: encoder, with: Self . keyMapping)
41- }
42- }
43-
44- // MARK: -
4549
4650public final class KeyMap < Root: Codable > {
4751 fileprivate let encode : ( _ root: Root , _ encoder: Encoder , _ nonnullAll: Bool , _ throwsAll: Bool ) throws -> Void
@@ -119,7 +123,7 @@ public extension Decoder { // , abortIfNull nonnull: Bool = false, abortOnError
119123 }
120124}
121125
122- // MARK: -
126+ // MARK: - Encoder&Decoder
123127
124128public extension Encoder {
125129
@@ -225,13 +229,18 @@ public extension Decoder {
225229 }
226230}
227231
228- private struct ExCodingKey : CodingKey {
229- let stringValue : String , intValue : Int ?
232+ // MARK: - ExCodingKey
233+
234+ private struct ExCodingKey {
235+ public let stringValue : String , intValue : Int ?
230236 init ( _ stringValue: String ) { ( self . stringValue, self . intValue) = ( stringValue, nil ) }
231237 init ( _ stringValue: Substring ) { self . init ( String ( stringValue) ) }
232- init ? ( stringValue: String ) { self . init ( stringValue) }
233238 init ( _ intValue: Int ) { ( self . intValue, self . stringValue) = ( intValue, String ( intValue) ) }
234- init ? ( intValue: Int ) { self . init ( intValue) }
239+ }
240+
241+ extension ExCodingKey : CodingKey {
242+ public init ? ( stringValue: String ) { self . init ( stringValue) }
243+ public init ? ( intValue: Int ) { self . init ( intValue) }
235244}
236245
237246// MARK: - alternative-keys + nested-keys + type-conversion
@@ -498,31 +507,3 @@ extension JSONDecoder: DataDecoder {}
498507extension PropertyListEncoder : DataEncoder { }
499508extension PropertyListDecoder : DataDecoder { }
500509#endif
501-
502- // MARK: - #### DEPRECATED ####
503-
504- public extension ExCodable {
505- @available ( * , deprecated, renamed: " encode(to:with:nonnull:throws:) " )
506- func encode( with keyMapping: [ KeyMap < Self > ] , using encoder: Encoder ) {
507- try ? encode ( to: encoder, with: keyMapping)
508- }
509- @available ( * , deprecated, renamed: " decode(from:with:nonnull:throws:) " )
510- mutating func decode( with keyMapping: [ KeyMap < Self > ] , using decoder: Decoder ) {
511- try ? decode ( from: decoder, with: keyMapping)
512- }
513- @available ( * , deprecated, renamed: " decodeReference(from:with:nonnull:throws:) " )
514- func decodeReference( with keyMapping: [ KeyMap < Self > ] , using decoder: Decoder ) {
515- try ? decodeReference ( from: decoder, with: keyMapping)
516- }
517- }
518-
519- @available ( * , deprecated, renamed: " append(decodingTypeConverter:) " )
520- public protocol KeyedDecodingContainerCustomTypeConversion : ExCodableDecodingTypeConverter {
521- func decodeForTypeConversion< T: Decodable , K: CodingKey > ( _ container: KeyedDecodingContainer < K > , codingKey: K , as type: T . Type ) -> T ?
522- }
523- @available ( * , deprecated)
524- public extension KeyedDecodingContainerCustomTypeConversion {
525- func decode< T: Decodable , K: CodingKey > ( _ container: KeyedDecodingContainer < K > , codingKey: K , as type: T . Type ) throws -> T ? {
526- return decodeForTypeConversion ( container, codingKey: codingKey, as: type)
527- }
528- }
0 commit comments