在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:matthewcheok/JSONCodable开源软件地址:https://github.com/matthewcheok/JSONCodable开源编程语言:Swift 98.4%开源软件介绍:#JSONCodable Hassle-free JSON encoding and decoding in Swift Installation
TLDR
Change Log
JSONCodable is made of two separate protocols
##Decoding JSON Take these two types for example: struct User {
let id: Int
let name: String
var email: String?
var company: Company?
var friends: [User] = []
}
struct Company {
let name: String
var address: String?
} You'd simply add conformance to extension User: JSONDecodable {
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
id = try decoder.decode("id")
name = try decoder.decode("full_name")
email = try decoder.decode("email")
company = try decoder.decode("company")
friends = try decoder.decode("friends")
}
}
extension Company: JSONDecodable {
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
name = try decoder.decode("name")
address = try decoder.decode("address")
}
} Note on Class Extensions: After the update to Swift 2.2 adding an initializer in an extension for classes is no longer supported. The current suggested work around for this is to just add the initializer in the class definition. For structs extensions still work as that had previously in this case. Then provide the implementations for let user = try User(object: JSON)
print("\(user)") Result: User(
id: 24,
name: "John Appleseed",
email: Optional("[email protected]"),
company: Optional(Company(
name: "Apple",
address: Optional("1 Infinite Loop, Cupertino, CA")
)),
friends: [
User(
id: 27,
name: "Bob Jefferson",
email: nil,
company: nil,
friends: []
),
User(
id: 29,
name: "Jen Jackson",
email: nil,
company: nil,
friends: []
)
]
) Decoding Nested Arrays and DictionaryDecoding also supports retrieving values using
Encoding JSONSimply add conformance to extension User: JSONEncodable {
func toJSON() throws -> Any {
return try JSONEncoder.create({ (encoder) -> Void in
try encoder.encode(id, key: "id")
try encoder.encode(name, key: "full_name")
try encoder.encode(email, key: "email")
try encoder.encode(company, key: "company")
try encoder.encode(friends, key: "friends")
})
}
}
extension Company: JSONEncodable {} The default implementation of Instantiate your struct, then use the let dict = try user.toJSON()
print("dict: \(dict)") Result: [full_name: John Appleseed, id: 24, email: john@appleseed.com, company: {
address = "1 Infinite Loop, Cupertino, CA";
name = Apple;
}, friends: (
{
friends = (
);
"full_name" = "Bob Jefferson";
id = 27;
},
{
friends = (
);
"full_name" = "Jen Jackson";
id = 29;
}
)] Working with JSON StringsThe convenience initializer Transforming valuesTo transform values, create an instance of let JSONTransformerStringToNSURL = JSONTransformer<String, NSURL>(
decoding: {NSURL(string: $0)},
encoding: {$0.absoluteString}) A Next, use the overloaded versions of struct User {
...
var website: NSURL?
}
init(object: JSONObject) throws {
...
website = try JSONDictionary.decode("website", transformer: JSONTransformerStringToNSURL)
}
func toJSON() throws -> AnyObject {
return try JSONEncoder.create({ (encoder) -> Void in
...
try result.encode(website, key: "website", transformer: JSONTransformerStringToNSURL)
})
} The following transformers are provided by default:
Feel free to suggest more! Extending JSONCodable (thanks to @raylillywhite)This allows for JSONDecoder extensions that allow the type system to better aid in decoding. For example, you could do: extension JSONDecoder {
public func decode(key: String) throws -> NSURL {
return try decode(key, transformer: JSONTransformers.StringToNSURL)
}
} then you only need to do: try url = decoder.decode("url") instead of try url = decoder.decode("url", JSONTransformers.StringToNSURL) Example codeRefer to the included playground in the workspace for more details. License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论