Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 3 Support #47

Open
McGar opened this issue Sep 21, 2016 · 2 comments
Open

Swift 3 Support #47

McGar opened this issue Sep 21, 2016 · 2 comments

Comments

@McGar
Copy link

McGar commented Sep 21, 2016

Any plan for swift 3 support?

@LuaiKalkatawi
Copy link

Any update?

@jayb611
Copy link

jayb611 commented Jan 30, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants