Created
September 14, 2015 03:01
-
-
Save mash/7a4f0047c2891ab3ed26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// JSONCoder.swift | |
// | |
// Created by Masakazu Ohtsuka on 2015/09/14. | |
// Copyright © 2015年 maaash.jp. All rights reserved. | |
// | |
import Foundation | |
import SwiftyJSON | |
struct JSONArchiver<V :JSONCoding> { | |
static func archiveValue(value :V, toFile fileURL :NSURL) throws -> Bool { | |
let json = value.serialize() | |
let data = try json.rawData() // rethrows | |
return data.writeToURL(fileURL, atomically: true) | |
} | |
} | |
protocol JSONCoding { | |
init (json :JSON) | |
func serialize () -> JSON | |
} | |
extension JSONCoding { | |
init? (jsonURL :NSURL) { | |
guard let data = NSData(contentsOfURL: jsonURL) else { return nil } | |
let json = JSON(data: data) | |
self.init(json: json) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment