SafeDecoder
SafeDecoder is a swift package that set defaults when Codable fails to decode a field. SafeDecoder supports configurable default values, See SafeDecoder.Configuration.
Installation
Swift Package Manager
Swift Package Manager is Apple's decentralized dependency manager to integrate libraries to your Swift projects. It is now fully integrated with Xcode 11
To integrate SafeDecoder into your project using SPM, specify it in your Package.swift file:
let package = Package(
    …
    dependencies: [
        .package(url: "[email protected]:GodL/SafeDecoder.git", from: "1.0.0"),
    ],
    targets: [
        .target(name: "YourTarget", dependencies: ["SafeDecoder", …])
        …
    ]
) 
Usage
normal
let model: Data1 = try SafeDecoder().decode(Data1.self, from: try encode(testData))
 
custom
struct CustomConfiguration: SafeConfigurationType {
    static var configuration: SafeDecoder.Configuration {
        var configuration: SafeDecoder.Configuration = .default
        configuration.register([Data1.Epidemic].self, value: [.init(title: "aaa", num: 20, routeUri: "test://url")])
        return configuration
    }
}
let model: Data1 = try SafeDecoder().decode(Data1.self, CustomConfiguration.self, from: try encode(testData)) 
Author
GodL, [email protected]. Github GodL
I am happy to answer any questions you may have. Just create a new issue.