 }
 
 extension Optional {
-  public static func decodeJson(decodeWrapped: AnyObject throws -> Wrapped, _ json: AnyObject) throws -> Wrapped? {
-    do {
-      return try decodeWrapped(json)
-    }
-    catch let error as JsonDecodeError {
-      if case let .WrongType(rawValue: rawValue, expectedType: expectedType) = error {
-        throw JsonDecodeError.WrongType(rawValue: rawValue, expectedType: "\(expectedType)?")
-      }
-
-      throw error
+  public static func decodeJson(decodeWrapped: AnyObject throws -> Wrapped) -> AnyObject throws -> Wrapped? {
+    return { json in
+      do {
+        return try decodeWrapped(json)
+      }
+      catch let error as JsonDecodeError {
+        if case let .WrongType(rawValue: rawValue, expectedType: expectedType) = error {
+          throw JsonDecodeError.WrongType(rawValue: rawValue, expectedType: "\(expectedType)?")
+        }
+
+        throw error
+      }
     }
   }
 

  case StructErrors(type: String, errors: [String: JsonDecodeError])
}

// Decode function for JsonObject.
// Would be nicer as an extension method.
public func JsonObject_decodeJson(json: AnyObject) throws -> JsonObject {
  guard let result = json as? JsonObject else {
    throw JsonDecodeError.WrongType(rawValue: json, expectedType: "JsonObject")
  }

  return result
}

extension String {

}

extension Array {
  public static func decodeJson(decodeElement: AnyObject throws -> Element) -> AnyObject throws -> [Element] {
    return { json in
      guard let arr = json as? [AnyObject] else {
        throw JsonDecodeError.WrongType(rawValue: json, expectedType: "Array")
      }

      var errors: [Int: JsonDecodeError] = [:]
      var result: [Element] = []

      for (index, element) in arr.enumerate() {
        do {
          result.append(try decodeElement(element))
        }
        catch let error as JsonDecodeError {
          errors[index] = error
        }
      }

      if errors.count > 0 {
        throw JsonDecodeError.ArrayElementErrors(errors)
      }

      return result
    }
  }

  public func encodeJson(encodeJsonElement: Element -> AnyObject) -> [AnyObject] {
