 
 extension Double: Parsable {
     public static func value(from json: JSON) throws -> Double {
-        return try castNumber(from: json, \.doubleValue)
+        return try castNumber(from: json, \.doubleValue, Double.init(_:))
     }
 }
 
 extension Float: Parsable {
     public static func value(from json: JSON) throws -> Float {
-        guard let double = try? castNumber(from: json, \.doubleValue) else { throw JSON.Error.typeMismatch(expected: Float.self, actualValue: json.rawValue, path: []) }
+        guard let double = try? castNumber(from: json, \.doubleValue, Double.init(_:))
+            else { throw JSON.Error.typeMismatch(expected: Float.self, actualValue: json.rawValue, path: []) }
         
         guard Swift.abs(double) <= Double(Float.greatestFiniteMagnitude)
             else { throw JSON.Error.dataCorrupted(value: json.rawValue, description: "The parsed value(\(json.rawValue) does overflow in Float type.") }


extension Int: Parsable {
    public static func value(from json: JSON) throws -> Int {
        return try castNumber(from: json, \.intValue)
    }
}

extension UInt: Parsable {
    public static func value(from json: JSON) throws -> UInt {
        return try castNumber(from: json, \.uintValue)
    }
}

extension Int8: Parsable {
    public static func value(from json: JSON) throws -> Int8 {
        return try castNumber(from: json, \.int8Value)
    }
}

extension Int16: Parsable {
    public static func value(from json: JSON) throws -> Int16 {
        return try castNumber(from: json, \.int16Value)
    }
}

extension Int32: Parsable {
    public static func value(from json: JSON) throws -> Int32 {
        return try castNumber(from: json, \.int32Value)
    }
}

extension Int64: Parsable {
    public static func value(from json: JSON) throws -> Int64 {
        return try castNumber(from: json, \.int64Value)
    }
}

extension UInt8: Parsable {
    public static func value(from json: JSON) throws -> UInt8 {
        return try castNumber(from: json, \.uint8Value)
    }
}

extension UInt16: Parsable {
    public static func value(from json: JSON) throws -> UInt16 {
        return try castNumber(from: json, \.uint16Value)
    }
}

extension UInt32: Parsable {
    public static func value(from json: JSON) throws -> UInt32 {
        return try castNumber(from: json, \.uint32Value)
    }
}

extension UInt64: Parsable {
    public static func value(from json: JSON) throws -> UInt64 {
        return try castNumber(from: json, \.uint64Value)
    }
}

extension Decimal: Parsable {
    public static func value(from json: JSON) throws -> Decimal {
        return try castNumber(from: json, \.decimalValue)
    }
}


    return value
}

private func castNumber<T>(from json: JSON, _ keyPath: @autoclosure () -> KeyPath<NSNumber, T>, _ fromInt: @autoclosure () -> (Int) throws -> T) throws -> T {
    if let number = try? NSNumber.value(from: json) {
        return number[keyPath: keyPath()]
    }
    
    do {
        return try cast(from: json)
    } catch {
        return try fromInt()(cast(from: json))
    }
}
