CodableX

0.1.1

Make Codable easier using Property Wrappers in Swift.
dscyrescotti/CodableX

What's New

Version 0.1.1

2021-02-13T09:06:26Z

Changed the conformed type of Element or Value of defaultable Collection to AnyCodable that makes more sense than using DefaultCodable.

CodableX

CodableX provides the property wrappers that are pretty handy to decode and encode structs or classes as you desire without implementing your custom codable structs or classes from scratch.

Installation

Swift Package Manager

Add it as a dependency within your Package.swift,

dependencies: [
    .package(url: "https://github.com/dscyrescotti/CodableX.git", from: "0.1.0")
]

Currently, CodableX is able to be installed via only Swift Package Manager.

@Anyable

@Anyable is designed to decode and encode any value that matches one of the types that you pre-configure. It is very handy when the value of API response will be sure one of the values that API sends.

Usage

Using the default options of CodableX.

struct AnyValue: Codable {
    @Anyable<DefaultOptions> var value: Any // Int, String, Bool or Double
}
let data = #"{ "value": 1 }"#.data(using: .utf8)!
let decoded = try JSONDecoder().decode(AnyValue.self, from: data)
print(decoded) // AnyValue(value: 1)

Note: DefaultOptions only supports for Int, String, Bool and Double.

Using the custom options.

struct Custom: AnyCodable {
    let value: String
}

struct CustomOptions: OptionConfigurable {
    static var options: [Option] = [
        .init(Int.self),
        .init(Custom.self),
        // add more
    ]
}

struct AnyValue: Codable {
    @Anyable<CustomOptions> var value: Any // Int, Custom or types you specify
}

Note: All the options of structs or classes must conform to AnyCodable.

For the array of Anyable and the optional Anyable, you can use @ArrayAnyable and @OptionalAnyable.

@Forcable

All credits to BetterCodable.

@Forcable is useful to force the value to be the specific type that you set when it decodes.

Usage

struct ForceValue: Codable {
    @Forcable<Bool, DefaultOptions> var value: Bool
}
let data = #"{ "value": "true" }"#.data(using: .utf8)!
let decoded = try JSONDecoder().decode(ForceValue.self, from: data)
print(decoded) // ForceValue(value: true)

It allows you to customize the list of options just like @Anyable. It will find the type that match the data from API response from your list and then force to a specific type that you want. For the array of Forcable and the optional Forcable, you can use @ArrayForcable and @OptionalForcable.

@Nullable

@Nullable serves as the traditional Optional (aka ?) of Swift. When encoding, it is able to encode nil as null in JSON.

Usage

struct NullValue: Codable {
    @Nullable var value: Int?
}
let data = #"{ "value": null }"#.data(using: .utf8)!
let decoded = try JSONDecoder().decode(NullValue.self, from: data)
print(decoded) // NullValue(value: nil)

@Defaultable

@Defaultable provides the default value when the coding key is not found or the value is missing.

Usage

For Swift built-in types, it will use the default init() method. For your custom structs or classes, you must make them conform to DefaultCodable and set the default value.

struct DefaultValue: Codable {
    @Defaultable var value: String
}
let data = #"{ "value": null }"#.data(using: .utf8)!
let decoded = try JSONDecoder().decode(DefaultValue.self, from: data)
print(decoded) // DefaultValue(value: "")

If you want different default values of the same struct or class, or you need the custom default value for built-in types, @CustomDefaultable will solve it.

struct CustomDefault: DefaultConfigurable {
    static var defaultValue: String = "dope"
}

struct DefaultValue: Codable {
    @CustomDefaultable<String, CustomDefault> var value: String
}

@Compactable

@Compactable is designed to decode the array of optional values and store values that are not null. Its name is come from compactMap(_:) of Swift because it removes null and invalid values from array.

Usage

struct CompactValue: Codable {
    @Compactable var array: [Int]
}

@Jsonable

@Jsonable is handy to decode data into JSON object structure using dictionary of Swift. Literally, it works like JSON.parse() in JavaScript.

Usage

struct JsonValue: Codable {
    @Jsonable var json: Any
}

Author

Dscyre Scotti (@dscyrescotti)

Credits

CodableX is inspired by BetterCodable and AnyCodable.

Contributions

CodableX welcomes all developers to contribute if you have any idea to improve and open an issue if you find any kind of bug.

License

CodableX is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.3.0

Dependencies

  • None
Last updated: Sun Mar 07 2021 12:35:00 GMT-0500 (GMT-05:00)