#JSONCodable Hassle-free JSON encoding and decoding in Swift
- Simply add the following to your
Cartfileand runcarthage update:
github "matthewcheok/JSONCodable" ~> 3.0.1
- or add the following to your
Podfileand runpod install:
pod 'JSONCodable', '~> 3.0.1'
or clone as a git submodule,
or just copy files in the
JSONCodablefolder into your project.
TLDR
- Uses Protocol Extensions
- Error Handling
- Supports
letproperties - Supports
enumproperties backed by compatible values
Change Log
- Moved encoding and decoding methods to a helper class
JSONCodable is made of two separate protocols JSONEncodable and JSONDecodable.
JSONEncodableallows your structs and classes to generateNSDictionaryor[String: AnyObject]equivalents for use withNSJSONSerialization.JSONDecodableallows you to generate structs fromNSDictionarycoming in from a network request for example.
##Decoding JSON Take these two types for example:
structUser{letid:Intletname:Stringvaremail:String?varcompany:Company?varfriends:[User]=[]}structCompany{letname:Stringvaraddress:String?}You'd simply add conformance to JSONDecodable (or to JSONCodable):
extensionUser:JSONDecodable{init(object:JSONObject)throws{letdecoder=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")}}extensionCompany:JSONDecodable{init(object:JSONObject)throws{letdecoder=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 init(object: JSONObject) throws where JSONObject is a typealias for [String:AnyObject].
As before, you can use this to configure the mapping between keys in the Dictionary to properties in your structs and classes.
letuser=tryUser(object: JSON)print("\(user)")Result:
User(
id:24,
name:"John Appleseed",
email:Optional("john@appleseed.com"),
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 also supports retrieving values using . separators for dictionaries and [index] for arrays. See below example:
name = try decoder.decode("value[0].properties.name")
Simply add conformance to JSONEncodable (or to JSONCodable):
extensionUser:JSONEncodable{func toJSON()throws->Any{returntryJSONEncoder.create({(encoder)->Voidintry 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")})}}extensionCompany:JSONEncodable{}The default implementation of func toJSON() inspects the properties of your type using reflection (see Company.) If you need a different mapping, you can provide your own implementation (see User.)
Instantiate your struct, then use the func toJSON() method to obtain a equivalent form suitable for use with NSJSONSerialization:
letdict=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;
})]The convenience initializer init?(JSONString: String) is provided on JSONDecodable. You may also use func toJSONString() throws -> String to obtain a string equivalent of your types.
To transform values, create an instance of JSONTransformer:
letJSONTransformerStringToNSURL=JSONTransformer<String,NSURL>(
decoding:{NSURL(string: $0)},
encoding:{$0.absoluteString})A JSONTransformer converts between 2 types, in this case, String and NSURL. It takes a closure for decoding and another for encoding, and in each case, you return an optional value of the corresponding type given an input type (you can return nil if a transformation is not possible).
Next, use the overloaded versions of func encode() and func decode() to supply the transformer:
structUser{...varwebsite:NSURL?}init(object:JSONObject)throws{...
website =tryJSONDictionary.decode("website", transformer: JSONTransformerStringToNSURL)}func toJSON()throws->AnyObject{returntryJSONEncoder.create({(encoder)->Voidin...try result.encode(website, key:"website", transformer: JSONTransformerStringToNSURL)})}The following transformers are provided by default:
JSONTransformers.StringToNSURL:String <-> NSURLJSONTransformers.StringToNSDate:String <-> NSDateISO format
Feel free to suggest more!
This allows for JSONDecoder extensions that allow the type system to better aid in decoding. For example, you could do:
extensionJSONDecoder{publicfunc decode(key:String)throws->NSURL{returntrydecode(key, transformer:JSONTransformers.StringToNSURL)}}then you only need to do:
try url = decoder.decode("url")instead of
try url = decoder.decode("url",JSONTransformers.StringToNSURL)Refer to the included playground in the workspace for more details.
JSONCodable is under the MIT license.
