tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.14k stars 1.03k forks source link

Digital deserialization is poorly compatible #1065

Open darvintang opened 5 years ago

darvintang commented 5 years ago

If the data returned in the background will be returned by the string type, the data will not be parsed.

E.g

{
  "int": "1",
  "double": "1"
}
struct Test: Mappable {
  var int: Int?
  var double: Double?

  init(_ map: Map) {
    double <- map["double"]
    int <- map["int"]
  }
}

Optimization

// IntegerOperators.swift
/// Convert any value to `SignedInteger`.
private func toSignedInteger<T: SignedInteger>(_ value: Any?) -> T? {
    guard
        case let number as NSNumber = value
    else {
        if case let stringValue as String = value, let tempInt64 = Int64(stringValue) {
            return T.init(tempInt64)
        }
        return nil
    }

    if T.self == Int.self, let x = Int(exactly: number.int64Value) {
        return T.init(x)
    }
    if T.self == Int8.self, let x = Int8(exactly: number.int64Value) {
        return T.init(x)
    }
    if T.self == Int16.self, let x = Int16(exactly: number.int64Value) {
        return T.init(x)
    }
    if T.self == Int32.self, let x = Int32(exactly: number.int64Value) {
        return T.init(x)
    }
    if T.self == Int64.self, let x = Int64(exactly: number.int64Value) {
        return T.init(x)
    }

    return nil
}

/// Convert any value to `UnsignedInteger`.
private func toUnsignedInteger<T: UnsignedInteger>(_ value: Any?) -> T? {
    guard
        case let number as NSNumber = value
    else {
        if case let stringValue as String = value, let tempUInt64 = UInt64(stringValue) {
            return T.init(tempUInt64)
        }
        return nil
    }

    if T.self == UInt.self, let x = UInt(exactly: number.uint64Value) {
        return T.init(x)
    }
    if T.self == UInt8.self, let x = UInt8(exactly: number.uint64Value) {
        return T.init(x)
    }
    if T.self == UInt16.self, let x = UInt16(exactly: number.uint64Value) {
        return T.init(x)
    }
    if T.self == UInt32.self, let x = UInt32(exactly: number.uint64Value) {
        return T.init(x)
    }
    if T.self == UInt64.self, let x = UInt64(exactly: number.uint64Value) {
        return T.init(x)
    }

    return nil
}
//
//  BinaryFloatingPointOperators.swift
//  ObjectMapper
//
//  Created by Suyeol xt-input on 06/07/2019.
//
//  The MIT License (MIT)
//
//  Copyright (c) 2014-2018 Tristan Himmelman
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

import Foundation

// MARK: - BinaryFloatingPoint

/// BinaryFloatingPoint mapping
public func <- <T: BinaryFloatingPoint>(left: inout T, right: Map) {
    switch right.mappingType {
    case .fromJSON where right.isKeyPresent:
        let value: T = toBinaryFloatingPoint(right.currentValue) ?? 0
        FromJSON.basicType(&left, object: value)
    case .toJSON:
        left >>> right
    default: ()
    }
}

/// Optional BinaryFloatingPoint mapping
public func <- <T: BinaryFloatingPoint>(left: inout T?, right: Map) {
    switch right.mappingType {
    case .fromJSON where right.isKeyPresent:
        let value: T? = toBinaryFloatingPoint(right.currentValue)
        FromJSON.basicType(&left, object: value)
    case .toJSON:
        left >>> right
    default: ()
    }
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
    /// ImplicitlyUnwrappedOptional BinaryFloatingPoint mapping
    public func <- <T: FloatingPoint>(left: inout T!, right: Map) {
        switch right.mappingType {
        case .fromJSON where right.isKeyPresent:
            let value: T! = toBinaryFloatingPoint(right.currentValue)
            FromJSON.basicType(&left, object: value)
        case .toJSON:
            left >>> right
        default: ()
        }
    }
#endif

// MARK: - Casting Utils

/// Convert any value to `BinaryFloatingPoint`.
private func toBinaryFloatingPoint<T: BinaryFloatingPoint>(_ value: Any?) -> T? {
    guard
        case let number as NSNumber = value
    else {
        if case let stringValue as String = value, let tempDouble = Double(stringValue) {
            return T.init(tempDouble)
        }
        return nil
    }

    if T.self == Float.self, let x = Float(exactly: number.doubleValue) {
        return T.init(x)
    }
    if T.self == Float32.self, let x = Float32(exactly: number.doubleValue) {
        return T.init(x)
    }
    if T.self == Float64.self, let x = Float64(exactly: number.doubleValue) {
        return T.init(x)
    }
    if T.self == Double.self, let x = Double(exactly: number.doubleValue) {
        return T.init(x)
    }

    return nil
}