Skip to content

Instantly share code, notes, and snippets.

@mash
Created September 14, 2015 03:01
Show Gist options
  • Save mash/7a4f0047c2891ab3ed26 to your computer and use it in GitHub Desktop.
Save mash/7a4f0047c2891ab3ed26 to your computer and use it in GitHub Desktop.
//
// 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