Swiftpack.co - osrufung/SafeCodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by osrufung.
osrufung/SafeCodable v0.0.2
Swift Codable extensions
⭐️ 2
🕓 3 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/osrufung/SafeCodable.git", from: "v0.0.2")

SafeCodable

Swift

Collection of Decodable extensions to help you when JSON responses safety is not guaranteed

Feature

Safe base/alternative types

struct Book: Decodable {
    let year: Safe<Int, String>
    let name: String
}

let rawJson = """
[
    {
        "year": 1951,
        "name": "Foundation"
    },
    {
        "year": "1984",
        "name": "Neuromancer"
    }
]
"""
let decoder = JSONDecoder()
let books = try decoder.decode([Book].self, from: rawJson.data(using: .utf8)!)

print(books.first?.year.value) //Optional(1951)
print(books.last?.year.value) //Optional(1984)

Safe Date multi-format types

Some responses contains multiple types of Date formatting (iOS8601 with seconds, milliseconds or single dates)

let rawDates = """
[
    { "value" : "2015-08-29T11:22:09Z"},
    { "value" : "2015-08-29T11:22:09.129Z"},
    { "value" : "2020-11-27"},
    { "value" : "2019-08-03 23:59:59"}

]
""".data(using: .utf8)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601(extended: true)
struct DateContainer: Decodable {
    let value: Date?
}

let dates = try? decoder.decode([DateContainer].self, from: rawDates!)
XCTAssertEqual(dates?.count, 4)

GitHub

link
Stars: 2
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

3 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics