Skip to content

Data Modelling Framework for JSON in Swift. Data model classes and instances can be easily created.

License

Notifications You must be signed in to change notification settings

acn001/SwiftJSONModel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SwiftJSONModel

Data Modelling Framework for JSON in Swift. Data model classes and instances can be easily created.

Release Notes

Version 0.1.1

Bool is support.

Version 0.1

Construct a model instance with a dictionary;
Update a model with a dictionary;
Convert a model to a dictionary.

Adding SwiftJSONModel to your project

Currently, SwiftJSONModel should be integrated manually. Follow these steps:

  1. Download the SwiftJSONModel repository as a zip file or clone it;
  2. Copy the SwiftJSONModel sub-folder into your Xcode project, and add it to your project.

Basic usage

Consider a JSON string like:

{
    "isOpen" : true,
    "shopId" : 200010000,
    "shopName" : "Wisdom Bookshop",
    "shopAddress" : "No 7, Fortune Rd."
}

To model this JSON, you should:

  1. Create a new class which inherit the SwiftJSONModel class;
  2. Declare properties with the name of the JSON keys:
class Shop: SwiftJSONModel {

    var isOpen: Bool = false
    var shopId: Int = 0
    var shopName: String?
    var shopAddress: String?
  
}

Currently, SwiftJSONModel can convert these types automatically in Swift:
Bool, Int, Float, Double, String / NSString, NSNumber, SwiftJSONModel or its subclass, and Array / NSArray. Remember that Bool, Int, Float, and Double properties cannot be optional.
If you want to use properties of other types,you should add these properties into ignored list. See Ignored properties in detail.
3. To construct a model instance from JSON data, you can use following constructor:

convenience init(dictionary: [String : AnyObject])

For example:

if let JSONData: NSData = /* obtained by Internet */ {
    if let JSONDictionary = try? NSJSONSerialization.JSONObjectWithData(JSONData, options: .MutableContainers) as? [String : AnyObject] {
        let shop = Shop(dictionary: JSONDictionary!)
        if shop.shopId == 200010000 {
            shop.shopName? = "New Wisdom Bookshop"
            // ...
        }
    }
}

Examples

####Model cascading (models including other models)

{
    "isOpen" : true,
    "shopId" : 200010000,
    "shopName" : "Wisdom Bookshop",
    "shopAddress" : "No 7, Fortune Rd."
    "shopOwner" : {
        "ownerId" : 30001001
        "ownerName" : "Alax Hackathon"
        "mobilephone" : "800-888-7777"
    }
}
class BookShop: Shop {
var shopOwner: Owner?

}

class Owner: SwiftJSONModel {

var ownerId: Int = 0
var ownerName: String?
var mobilephone: String?

}

Ignored properties

You can specify the property to be ignored when converting by one of these following ways:

  • Composite a subclass of SwiftJSONModelIgnored which has the property to ignore:
class BookShop: Shop {
    
    var shopLogo: ShopLogo?
    
}

class ShopLogo: SwiftJSONModelIgnored {
    
    var image: UIImage?
    
}
  • Override the method of func ignoredProperties() -> [String] to return an array of classname string:
class BookShop: Shop {
    
    var shopLogo: UIImage?
    
    override func ignoredProperties() -> [String] {
        return super.ignoredProperties() + ["shopLogo"]
    }
    
}

Remember that in an inheritance system, you should call super.ignoredProperties() and merge superclass's ignore list.

Model collections

You should specify the generic types of array collections by method of func genericTypes() -> [String : String]. The valid generic type can be: Bool, Int, Float, Double, String / NSString, NSNumber, SwiftJSONModel or its subclass.

{
    "isOpen" : true,
    "shopId" : 200010000,
    "shopName" : "Wisdom Bookshop",
    "shopAddress" : "No 7, Fortune Rd."
    "books" : [
        {
            "name" : "Data Mining",
            "price" : 825
        },
        {
            "name" : "Data Analysis",
            "price" : 750
        }
    ]
}
class BookShop: Shop {
var books: [Book]?

override func genericTypes() -> [String : String] {
    var result = ["books" : "Book"]
    for (k, v) in super.genericTypes() {
        result[k] = v
    }
    return result
}

}

class Book: SwiftJSONModel {

var name: String?
var price: Int = 0

}

Remember that in an inheritance system, you should call `super.genericTypes()` and merge superclass's generic type list.

Computed properties

Computed properties are supported in SwiftJSONModel. You can add computed properties for a class as usual, but remember to make them ignored:

class Shop: SwiftJSONModel {
    
    var isOpen: Bool = false
    var shopId: Int = 0
    var shopName: String?
    var shopAddress: String?
    var isOldShop: Bool {
        return shopId > 0 && shopId < 100000000
    }
    
    override func ignoredProperties() -> [String] {
        return super.ignoredProperties() + ["isOldShop"]
    }
    
}

Misc

Author: Zhu Yue

License

This code is distributed under the terms and conditions of the MIT license.

About

Data Modelling Framework for JSON in Swift. Data model classes and instances can be easily created.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages