     }
 
     public func decodeArray<T: JSONDecodable>(type: T.Type) -> [T]? {
-        return array?.reduce([T](), combine: { array, element in
-            switch T.decode(element) {
+        return decodeArray(T.decode)
+    }
+
+    public func decodeArray<T>(transform: JSON -> T?) -> [T]? {
+        let block: ([T], AnyObject) -> [T] = { array, element in
+            switch JSON(object: element).flatMap(transform) {
             case .Some(let object):
                 return array + CollectionOfOne(object)
             case .None:
                 return array
             }
-        })
+        }
+        return (object as? [AnyObject]).map({ reduce($0, [T](), block) })
+    }
+}
+
+extension JSON {
+    public init?(data: NSData, options: NSJSONReadingOptions = .allZeros) {
+        if let object: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: options, error: nil) {
+            self.object = object
+        }
+        else {
+            return nil
+        }
     }
 
-    public func decodeArray<T>(transform: JSON -> T?) -> [T]? {
-        return array?.reduce([T](), combine: { array, element in
-            switch transform(element) {
-            case .Some(let object):
-                return array + CollectionOfOne(object)
-            case .None:
-                return array
-            }
-        })
+    func data(options: NSJSONWritingOptions = .allZeros) -> NSData? {
+        return NSJSONSerialization.dataWithJSONObject(object, options: options, error: nil)
     }
 }

    }

    public var dictionary: [String: JSON]? {
        return (object as? [String: AnyObject])?.map({ JSON(object: $0) })
    }

    public var array: [JSON]? {
        return (object as? [AnyObject])?.map({ JSON(object: $0) })
    }

    public var int: Int? {

//  Copyright (c) 2015 Hodinkee. All rights reserved.
//

import Foundation

public protocol JSONDecodable {
    static func decode(JSON: Alexander.JSON) -> Self?
}

public struct JSON {
    public var object: AnyObject

    public init?(object: AnyObject) {
        if NSJSONSerialization.isValidJSONObject(object) {
            self.object = object
        }
        return nil
    }

    public subscript(index: Int) -> JSON? {
        let array = object as? [AnyObject]
        return (array?[index]).flatMap({ JSON(object: $0) })
    }

    public subscript(key: String) -> JSON? {
        let dictionary = object as? [String: AnyObject]
        return (dictionary?[key]).flatMap({ JSON(object: $0) })
    }

    public var string: String? {
