swift-serializer icon indicating copy to clipboard operation
swift-serializer copied to clipboard

Swift 3 Support

Open McGar opened this issue 8 years ago • 2 comments

Any plan for swift 3 support?

McGar avatar Sep 21 '16 21:09 McGar

Any update?

LuaiKalkatawi avatar Oct 30 '16 07:10 LuaiKalkatawi

Here is

SWIFT 3.0 Solution.

Sorry was not able to make Solution for Sorted Dictionary.

`class Serializable: NSObject {

func toDictionary() -> NSDictionary {
    var dictionaryN = [String: Any]()
    let mirror = Mirror(reflecting: self)
    for (propName, propValue) in mirror.children {
        if let propValue: AnyObject = self.unwrap(any: propValue) as AnyObject?, let propName = propName {
            if let serializablePropValue = propValue as? Serializable {
                dictionaryN[propName] = serializablePropValue.toDictionary()
            } else if let arrayPropValue = propValue as? [Serializable] {
                let subArray =   arrayPropValue.toNSDictionaryArray()
               dictionaryN[propName] = subArray
            } else if let dataPropValue = propValue as? NSData {
                dictionaryN[propName] =  dataPropValue.base64EncodedString(options: .lineLength64Characters)
            } else if propValue is NSDate {
                let date = propValue as! Date
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "Z"
                let dateString = NSString(format: "/Date(%.0f000%@)/", date.timeIntervalSince1970, dateFormatter.string(from: date))
                dictionaryN[propName] = dateString

            } else if propValue is BoolJ {
                   dictionaryN[propName] = (propValue as! BoolJ)
            } else if propValue is descriptioN {
                   dictionaryN[propName] = (propValue as! descriptioN)
            } else if propValue is NSDate {
                let date = propValue as! Date
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "Z"
                let dateString = NSString(format: "/Date(%.0f000%@)/", date.timeIntervalSince1970, dateFormatter.string(from: date))
                dictionaryN[propName] = dateString
            } else {
                dictionaryN[propName] = propValue
            }
            
        } else if isEnum(any: propValue) {
            dictionaryN[propName!] = "\(propValue)"
           
        }
        else
        {
             dictionaryN[propName!] = propValue
        }
    }
    
    return dictionaryN as NSDictionary
}

/**
 Converts the class to JSON.
 - returns: The class as JSON, wrapped in NSData.
 */
func toJson(prettyPrinted: Bool = false) -> Data? {
    let dictionary = self.toDictionary()
    
    if JSONSerialization.isValidJSONObject(dictionary) {
        do {
            let json = try JSONSerialization.data(withJSONObject: dictionary, options: (prettyPrinted ? .prettyPrinted: JSONSerialization.WritingOptions()))
            return json as Data?
        } catch let error as NSError {
            print("ERROR: Unable to serialize json, error: \(error)")
        }
    }
    
    return nil
}

/**
 Converts the class to a JSON string.
 - returns: The class as a JSON string.
 */
func toJsonString(prettyPrinted: Bool = false) -> String? {
    if let jsonData = self.toJson(prettyPrinted: prettyPrinted) {
        return NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as String?
    }
    
    return nil
}


/**
 Unwraps 'any' object. See http://stackoverflow.com/questions/27989094/how-to-unwrap-an-optional-value-from-any-type
 - returns: The unwrapped object.
 */
func unwrap(any: Any) -> Any? {
    let mi = Mirror(reflecting: any)
    if mi.displayStyle != .optional {
        return any
    }
    
    if mi.children.count == 0 { return nil }
    let (_, some) = mi.children.first!
    return some
}

func isEnum(any: Any) -> Bool {
    return Mirror(reflecting: any).displayStyle == .enum
}
override init() { }

} `

I have used EVReflection and HandyJSON But They were Buggy for Simply generating Json String. But they have Good Deserialise technique Enjoy I loved this Lib

jayb611 avatar Jan 30 '17 15:01 jayb611