Default Value propertyWrapper for Codable Value
Add the dependency in your Package.swift
file:
let package = Package(
name: "myproject",
dependencies: [
.package(url: "[email protected]:DanboDuan/Codable.git", .upToNextMajor(from: "1.0.0")),
],
targets: [
.target(
name: "myproject",
dependencies: ["Codable"]),
]
)
see Example code
- Supprt basic type,
Int64
,Bool
,Double
- Support basic collection,
String
,Array
,Set
,Dictionary
- Support custom type extended to
Codable & Equatable & DefaultValue
import Codable
struct Car: Codable, Equatable, DefaultValue {
@Default<Empty>
public var name: String
@Default<EmptyValue>
public var age: Int64
public init() {}
public static func == (left: Self, right: Self) -> Bool {
return left.name == right.name
}
}
struct Pet: Codable, Equatable, DefaultValue {
@Default<Empty>
public var name: String
@Default<Int64>
public var age: Int64
public init() {}
public static func == (left: Self, right: Self) -> Bool {
return left.name == right.name
}
}
struct Person: Codable, Equatable, DefaultValue {
@Default<Empty>
public var name: String
@Default<EmptyDictionary>
public var cars: [String: Car]
@Default<Empty>
public var pets: [Pet]
public init() {}
public static func == (left: Self, right: Self) -> Bool {
return left.name == right.name
}
}
struct RequestPayload: Codable {
@Default<Empty>
public var method: String
@Default<EmptyValue>
public var payload: Person
}
var json: String = ""
json = """
{
"payload": {
"name":"bob",
"pets":[
{
"name":"Snoopy"
}
],
"cars":{
"first_car":{
"name":"Tesla",
"age":1
},
"second_car":{
}
}
}
}
"""
if let data = json.data(using: .utf8),
let requestPayload = try? JSONDecoder().decode(RequestPayload.self, from: data)
{
if let pet = requestPayload.payload.pets.first {
print("pet:", pet.name, pet.age)
}
if let car = requestPayload.payload.cars["second_car"] {
print("second_car:", car.name, car.age)
}
}
- Git clone the repository
[email protected]:DanboDuan/Codable.git
. - Open
Package.swift
.
Codable is released under the Apache License license. See LICENSE for details.