JSONExportとは
JSONExportはJSONからモデルのコードを出力してくれるMac用のアプリケーションです。
![スクリーンショット 2016-04-05 11.14.08.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2F643b0065-538c-e734-f3d9-c912c5ae22a0.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=2d61ceb36bc47f8ffe07d030dad50dc6)
出力できるもの
- Java for Android.
- Java for Android - to use with Realm.
- Swift Classes.
- Swift Classes - To use with SwiftyJSON library.
- Swift Classes - To use with Realm.
- Swift - CoreData.
- Swift Sturcutres.
- Objective-C - iOS.
- Objective-C - MAC.
- Objective-C - CoreData.
- Objective-C - To use with Realm.
できること
{
"id": 123456,
“text”: “Hello!!“,
"user": {
"id": 1234,
"name": “Motokazu Sekine”
}
}
たとえば、このJSONから、以下のコードが出力されます。
//
// RootClass.swift
// Model file Generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport
import Foundation
class RootClass : NSObject, NSCoding{
var id : Int!
var text : String!
var user : User!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: NSDictionary){
id = dictionary["id"] as? Int
text = dictionary["text"] as? String
if let userData = dictionary["user"] as? NSDictionary{
user = User(fromDictionary: userData)
}
}
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> NSDictionary
{
var dictionary = NSMutableDictionary()
if id != nil{
dictionary["id"] = id
}
if text != nil{
dictionary["text"] = text
}
if user != nil{
dictionary["user"] = user.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
id = aDecoder.decodeObjectForKey("id") as? Int
text = aDecoder.decodeObjectForKey("text") as? String
user = aDecoder.decodeObjectForKey("user") as? User
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encodeWithCoder(aCoder: NSCoder)
{
if id != nil{
aCoder.encodeObject(id, forKey: "id")
}
if text != nil{
aCoder.encodeObject(text, forKey: "text")
}
if user != nil{
aCoder.encodeObject(user, forKey: "user")
}
}
}
//
// User.swift
// Model file Generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport
import Foundation
class User : NSObject, NSCoding{
var id : Int!
var name : String!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: NSDictionary){
id = dictionary["id"] as? Int
name = dictionary["name"] as? String
}
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> NSDictionary
{
var dictionary = NSMutableDictionary()
if id != nil{
dictionary["id"] = id
}
if name != nil{
dictionary["name"] = name
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
id = aDecoder.decodeObjectForKey("id") as? Int
name = aDecoder.decodeObjectForKey("name") as? String
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encodeWithCoder(aCoder: NSCoder)
{
if id != nil{
aCoder.encodeObject(id, forKey: "id")
}
if name != nil{
aCoder.encodeObject(name, forKey: "name")
}
}
}
超絶に便利です!!
使い方
JSONExportはバイナリで提供されていませんので、プロジェクトをダウンロードして自分でビルドする必要があります。
ビルドには、XcodeのインストールされたMacが必要です。
https://github.com/Ahmed-Ali/JSONExport からCloneするなりダウンロードするなりして、プロジェクトをゲットしましょう。
JSONExport.xcodeprojをXcodeで開きます。
![スクリーンショット 2016-04-05 11.24.41.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2Ff089c8e9-ccd9-7090-3d62-47a1f31b2b99.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=750faa0e6445c3478db2ebf4f7d05ca0)
メニューから、[Product]->[Archive]を選択します
![スクリーンショット 2016-04-05 11.25.20.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2F8a5b17ac-3287-e268-b720-4d8265ef0af0.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=cc4315b40ab389393b7c7414606749a5)
ビルドが開始されます。
ビルドが終了すると、オーガナイザが開くので[Export]ボタンを押します。
![スクリーンショット 2016-04-05 11.28.45.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2F6d405e30-0106-5442-4eeb-565f30638a13.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=86ae36dfc7b82cb3e3c96f4cf0877f82)
「Export as a Mac Application」を選択して、[Next]ボタンを押します。
![スクリーンショット 2016-04-05 11.30.14.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2Fa86bf9fa-4c5c-cdc9-22ea-6145a70605b6.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=afc6510380ac0b9e9e0f0a8538e2875b)
出力先を聞かれるので、とりあえずデスクトップを指定しましょう。
![スクリーンショット 2016-04-05 11.32.41.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2F647a15b0-b682-059b-c34b-10e61e468839.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=2258bbfb339b976a4d0d0c5ec1863831)
![スクリーンショット 2016-04-05 11.34.12.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2Fad009726-0e07-f6a7-9aa4-30fb4727022c.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c5af3578dff7ab9ef498a0e010ed72a8)
[Export]ボタンを押すと、JSONExportの入ったフォルダができます。
![スクリーンショット 2016-04-05 11.35.01.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2Fedd364f5-8aa6-ce77-675e-ec56e0c04b0e.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c1f0f2f718e3b3fa29a6efee770f68b9)
このフォルダの中に入っているJSONExportを使うことになります。
![スクリーンショット 2016-04-05 11.35.24.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2F4899ac5e-4f19-8fce-1ac4-49bb2178e0b6.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f19386830f306446e7effc53da81bad2)
あとは、JSONExportを起動して、左ペインにJSONのコードを貼り付けて、右下で出力するコードの種類を指定して、[Save]ボタンを押すだけです!
![スクリーンショット 2016-04-05 11.42.43.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F16973%2F93471b5e-886d-1149-523f-efcfd1f25378.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=4e882da2e990c3c19046a1879e33e990)
ファイルに出力せずに、そのままコードをコピーしてもOKです。
おしまい
JSONからコードをちまちま書いていくのはミスの元ですし、結構辛い作業です。
JSONExportを使って、楽しましょう!