turowicz / swift-serializer Goto Github PK
View Code? Open in Web Editor NEWApple Swift Strong Type Object Serialization to JSON
License: MIT License
Apple Swift Strong Type Object Serialization to JSON
License: MIT License
Thank you for your time and effort in creating this library. Would it be possible for you to publish the latest version of your code to Cocoapods?
subject
'reflect' is unavailable in xcode 7 beta 4
public func toDictionary() -> NSDictionary {
let propertiesDictionary = SortedDictionary()
let mirror = Mirror(reflecting: self)
for (propName, propValue) in mirror.children {
if let propValue: AnyObject = self.unwrap(propValue) as? AnyObject, propName = propName {
if let serializablePropValue = propValue as? Serializable {
setValue(propertiesDictionary, value: serializablePropValue.toDictionary(), forKey: propName)
} else if let arrayPropValue = propValue as? [Serializable] {
let subArray = arrayPropValue.toNSDictionaryArray()
setValue(propertiesDictionary, value: subArray, forKey: propName)
} else if propValue is Int || propValue is Double || propValue is Float || propValue is Bool {
setValue(propertiesDictionary, value: propValue, forKey: propName)
} else if let dataPropValue = propValue as? NSData {
setValue(propertiesDictionary,
value: dataPropValue.base64EncodedStringWithOptions(.Encoding64CharacterLineLength), forKey: propName)
} else if let datePropValue = propValue as? NSDate {
setValue(propertiesDictionary, value: datePropValue.timeIntervalSince1970, forKey: propName)
} else {
setValue(propertiesDictionary, value: propValue, forKey: propName)
}
} else if let propValue: Int8 = propValue as? Int8 {
setValue(propertiesDictionary, value: NSNumber(char: propValue), forKey: propName!)
} else if let propValue: Int16 = propValue as? Int16 {
setValue(propertiesDictionary, value: NSNumber(short: propValue), forKey: propName!)
} else if let propValue: Int32 = propValue as? Int32 {
setValue(propertiesDictionary, value: NSNumber(int: propValue), forKey: propName!)
} else if let propValue: Int64 = propValue as? Int64 {
setValue(propertiesDictionary, value: NSNumber(longLong: propValue), forKey: propName!)
} else if let propValue: UInt8 = propValue as? UInt8 {
setValue(propertiesDictionary, value: NSNumber(unsignedChar: propValue), forKey: propName!)
} else if let propValue: UInt16 = propValue as? UInt16 {
setValue(propertiesDictionary, value: NSNumber(unsignedShort: propValue), forKey: propName!)
} else if let propValue: UInt32 = propValue as? UInt32 {
setValue(propertiesDictionary, value: NSNumber(unsignedInt: propValue), forKey: propName!)
} else if let propValue: UInt64 = propValue as? UInt64 {
setValue(propertiesDictionary, value: NSNumber(unsignedLongLong: propValue), forKey: propName!)
} else if isEnum(propValue) {
setValue(propertiesDictionary, value: "\(propValue)", forKey: propName!)
}
}
return propertiesDictionary
}
I'm trying to serialize objects that contain a CLLocation
object as a property and toDictionary
does not seem to cover that case yet. So I'm contemplating what the best solution here is. I thought I could probably just add a propValue is CLLocation
case to the function and extract coordinate.latitude
and coordinate.longitude
and store them as Double
values in there. But that seems kinda messy to me. Is there a nicer way you could suggest?
We need to make this available through cocoapods.
Leverage the possibility to use markdown in playgrounds to create a rich documentation for Swift Serializer.
References:
ex:
enum HeroType: String {
case First, Second
}
class Hero: Serializable {
var type: HeroType?
}
toDictionary() will skip this "type"
The Cocoapod hasn't been updated to the latest version yet, which caused me some problems after auto-converting to Swift 1.2; see issue #11
Hi,
Once I convert the object to string using toJsonString, how can I generate object form that same json string ? Basically How do i reverse from String -> Object
Also please update in pod too
We need to have a deserializer as well.
I try to .toJson()
a [String]
type var. Is that normal? Should I extend String with Serializable protocol?
Hey guys
As you have probably noticed this git repo isn't well maintained: code is not being reviewed, requests are not merged etc.
This is all because because we have shifted our development focus away from iOS/Swift. That said we are happy to accept anyone who wish to become a contributor to the project in terms of code review and PR merging.
Please apply below this post.
I am using SwiftSerializer 0.1 and Xcode 7.2. I have installed SwiftSerializer using pods:
platform :ios, '9.0'
use_frameworks!
target 'PodTest' do
pod 'SwiftSerializer', '~> 0.1'
end
I am getting many compilation errors in Serializable.swift
file. Here are a few examples.
/Users/xxx/Documents/PodTest/Pods/SwiftSerializer/src/Serializable.swift:27:59: 'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?
/Users/xxx/Documents/PodTest/Pods/SwiftSerializer/src/Serializable.swift:30:58: 'AnyObject!' is not convertible to 'Serializable'; did you mean to use 'as!' to force downcast?
/Users/xxx/Documents/PodTest/Pods/SwiftSerializer/src/Serializable.swift:62:61: Extra argument 'error' in call
Any plan for swift 3 support?
All variables, including description (Printable), debugDescription(debugPrintable) and hashValue (Hashable) end up in the JSON. Is there a way to exclude these that I have overlooked? It would be a great addition.
Hi,
I'm new to swift, I try to create my first app and to do so I'm trying to save a class (with strings, int and an array of another class) to .json file (so I when app is closed/opened, data remains). I found your serializer very interesting but when I try to import XCTest, I have this issue :
I also found errors on your Serializable.swift :
Here is how I fixed that :
public func toDictionary() -> NSDictionary {
var aClass : AnyClass? = self.dynamicType
var propertiesCount : CUnsignedInt = 0
let propertiesInAClass : UnsafeMutablePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount)
var propertiesDictionary : NSMutableDictionary = NSMutableDictionary()
for var i = 0; i < Int(propertiesCount); i++ {
var property = propertiesInAClass[i]
var propName = NSString(CString: property_getName(property), encoding: NSUTF8StringEncoding)!
var propType = property_getAttributes(property)
var propValue : AnyObject! = self.valueForKey(propName as String);
if propValue is Serializable {
propertiesDictionary.setValue((propValue as! Serializable).toDictionary(), forKey: propName as String)
} else if propValue is Array<Serializable> {
var subArray = Array<NSDictionary>()
for item in (propValue as! Array<Serializable>) {
subArray.append(item.toDictionary())
}
propertiesDictionary.setValue(subArray, forKey: propName as String)
} else if propValue is Double {
propertiesDictionary.setValue((propValue as! Double), forKey: propName as String)
} else if propValue is Int {
propertiesDictionary.setValue((propValue as! Int), forKey: propName as String)
} else if propValue is Float {
propertiesDictionary.setValue((propValue as! Float), forKey: propName as String)
} else if propValue is NSData {
propertiesDictionary.setValue((propValue as! NSData).base64EncodedStringWithOptions(nil), forKey: propName as String)
} else if propValue is Bool {
propertiesDictionary.setValue((propValue as! Bool).boolValue, forKey: propName as String)
} else {
propertiesDictionary.setValue(propValue, forKey: propName as String)
}
}
// class_copyPropertyList retaints all the
propertiesInAClass.dealloc(Int(propertiesCount))
return propertiesDictionary
}
Thanks for reading !
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.