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.
pod 'CodableX', :git => 'https://github.com/dscyrescotti/CodableX.git'
Currently, CodableX can be installed via Swift Package Manager and CocoaPods.
@AnyValuable
@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.
Usage
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.
Accessing the value inside AnyValue
// 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
@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 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.
@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.
@Compactable is designed to decode the array of optional values and store values that are not null. Its name comes 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
}
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
,CocoaPods
Inside your Podfile,
Currently, CodableX can be installed via Swift Package Manager and CocoaPods.
@AnyValuable
@AnyValuable
is pretty similar to the original@Anyable
property wrapper. It wraps decoded value insideAnyValue
which can hold the type that conforms toAnyCodable
and also provide easier access to value thanAny
.Usage
Using the default options of CodableX.
Note:
DefaultOptions
only supports forInt
,String
,Bool
andDouble
.Accessing the value inside
AnyValue
Or
Note:
AnyValue
already has type casting for Swift built-in types. For your custom types, you can extendAnyValue
to declare them.Using the custom options.
Note: All the options of structs or classes must conform to
AnyCodable
.For the array of
AnyValuable
and the optionalAnyValuable
, you can use@ArrayAnyValuable
and@OptionalAnyValuable
.@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.
Note:
DefaultOptions
only supports forInt
,String
,Bool
andDouble
.Using the custom options.
Note: All the options of structs or classes must conform to
AnyCodable
.For the array of
Anyable
and the optionalAnyable
, 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
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 optionalForcable
, you can use@ArrayForcable
and@OptionalForcable
.@Nullable
@Nullable
serves as the traditionalOptional
(aka ?) ofSwift
. When encoding, it is able to encodenil
asnull
in JSON.Usage
@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 defaultinit()
method. For your custom structs or classes, you must make them conform toDefaultCodable
and set the default 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.@Compactable
@Compactable
is designed to decode the array of optional values and store values that are not null. Its name comes fromcompactMap(_:)
of Swift because it removes null and invalid values from array.Usage
@Jsonable
@Jsonable
is handy to decode data into JSON object structure using dictionary ofSwift
. Literally, it works likeJSON.parse()
inJavaScript
.Usage
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.