GithubHelp home page GithubHelp logo

swift-serializer's Introduction

Swift Serializer

Build Status codecov.io

Apple Swift Strong Type Object Serialization to JSON

Usage

import XCTest

class Person:Serializable {
    var Name : String
    var Surname : String
    var BirthTimestamp : NSNumber
    var Animals : Array<Animal>


    init(Name:String, Surname:String, BirthTimestamp:NSNumber) {
        self.Name = Name
        self.Surname = Surname
        self.BirthTimestamp = BirthTimestamp
        self.Animals = Array<Animal>()
    }
}

class Animal:Serializable {
    var Nickname : String
    var Kind : String
    var Trick : String?

    init(Nickname : String, Kind : String, Trick : String?) {
        self.Nickname = Nickname
        self.Kind = Kind
        self.Trick = Trick
    }
}

class SerializationTests: XCTestCase {
    func test_serialization_works() {
        var john = Person(Name: "John", Surname: "Doe", BirthTimestamp: 512463600000)
        
        john.Animals.append(Animal(Nickname: "Fluffy", Kind: "Dog", Trick: "Rollover"))
        john.Animals.append(Animal(Nickname: "Purry", Kind: "Cat", Trick: nil))
        
        println(john.toJson()) //will give binary data to include in HTTP Body
        println(john.toJsonString()) //will give the exact string in JSON
        
        var expected = "{\"BirthTimestamp\":512463600000,\"Name\":\"John\",\"Animals\":[{\"Trick\":\"Rollover\",\"Kind\":\"Dog\",\"Nickname\":\"Fluffy\"},{\"Kind\":\"Cat\",\"Nickname\":\"Purry\"}],\"Surname\":\"Doe\"}";
        
        XCTAssertEqual(john.toJsonString(), expected,"")
    }
}

Contributing

We will accept all valid Pull Requests, Feature Requests and any other Issues made on this project. Any questions just ask.

swift-serializer's People

Contributors

basthomas avatar briananderson1222 avatar ismaeldcom avatar krzyzanowskim avatar maciek2w avatar mpavlinsky avatar naeemkhedarun avatar nazywamsiepawel avatar nonouf avatar nwoolls avatar petard avatar sjturley avatar turowicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-serializer's Issues

RESURRECTION

Hey guys,

This repository is a bit out of date. I will be sorting out the remaining issues when I get a chance.

Any help will be greatly appreciated.

Planned work:
#39
#40
#41

If time allows:
#2 + #35 (same thing)

Any other suggestions?

@sjturley are you still up?

Build error in Xcode 7.2

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

Exclude variables from JSON data

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.

Maintainers wanted!

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.

XCTest

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 :
capture d ecran 2015-05-11 a 12 03 44

I also found errors on your Serializable.swift :
capture d ecran 2015-05-11 a 12 01 02

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 !

This is a crime

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
}

Update Cocoapod

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

enumeration can't be serialized

ex:

enum HeroType: String {
case First, Second
}

class Hero: Serializable {
var type: HeroType?
}

toDictionary() will skip this "type"

Converting String back to object

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

Serialize CLLocation Objects

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?

Publish to Cocoapods?

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.