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.
Add it as a dependency within your Package.swift
,
dependencies: [
.package(url: "https://github.com/dscyrescotti/CodableX.git", from: "0.2.0")
]
Inside your Podfile,
pod 'CodableX', :git => 'https://github.com/dscyrescotti/CodableX.git'
Currently, CodableX is able to be installed via Swift Package Manager and CocoaPods.
@AnyValuable
is pretty similar to the original @Anyable
property wrapper. It wraps decoded value inside AnyValue
which can hold the type that conforms to AnyCodable
and also provide easier access to value than Any
.
Using the default options of CodableX.
struct AnyValuableExample: Codable {
@AnyValuable<DefaultOptions> var value: AnyValue // Int, String, Bool or Double inside AnyValue
}
let data = #"{ "value": 1 }"#.data(using: .utf8)!
let decoded = try JSONDecoder().decode(AnyValuableExample.self, from: data)
print(decoded) // AnyValuableExample(value: AnyValue(value: 1))
Note: DefaultOptions
only supports for Int
, String
, Bool
and Double
.
// You can directly access data via value
print(decoded.value) // 1
Or
// You can access data via type casting. It's helpful for optional unwrapping and is also clear to read.
print(decoded.value.int) // Optional(1)
Note: AnyValue
already has type casting for Swift built-in types. For your custom types, you can extend AnyValue
to declare them.
Using the custom options.
struct Custom: AnyCodable {
let value: String
}
// For type casting
extension AnyValue {
var custom: Custom? {
value as? Custom
}
}
struct CustomOptions: OptionConfigurable {
static var options: [Option] = [
.init(Int.self),
.init(Custom.self),
// add more
]
}
struct AnyValuableExample: Codable {
@AnyValuable<CustomOptions> var value: AnyValue // Int, Custom or types you specify inside AnyValue
}
Note: All the options of structs or classes must conform to AnyCodable
.
For the array of AnyValuable
and the optional AnyValuable
, you can use @ArrayAnyValuable
and @OptionalAnyValuable
.
@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.
Using the default options of CodableX.
struct AnyableExample: Codable {
@Anyable<DefaultOptions> var value: Any // Int, String, Bool or Double
}
let data = #"{ "value": 1 }"#.data(using: .utf8)!
let decoded = try JSONDecoder().decode(AnyableExample.self, from: data)
print(decoded) // AnyableExample(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 AnyableExample: 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
.
All credits to BetterCodable.
@Forcable
is useful to force the value to be the specific type that you set when it decodes.
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
serves as the traditional Optional
(aka ?) of Swift
. When encoding, it is able to encode nil
as null
in JSON.
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
provides the default value when the coding key is not found or the value is missing.
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
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.
struct CompactValue: Codable {
@Compactable var array: [Int]
}
@Jsonable
is handy to decode data into JSON object structure using dictionary of Swift
. Literally, it works like JSON.parse()
in JavaScript
.
struct JsonValue: Codable {
@Jsonable var json: Any
}
Dscyre Scotti (@dscyrescotti)
CodableX is inspired by BetterCodable and AnyCodable.
CodableX welcomes all developers to contribute if you have any idea to improve and open an issue if you find any kind of bug.
CodableX is available under the MIT license. See the LICENSE file for more info.