Giter Site home page Giter Site logo

swift-serializer's Introduction

Swift Serializer

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

turowicz avatar krzyzanowskim avatar maciek2w avatar mpavlinsky avatar naeemkhedarun avatar nonouf avatar basthomas avatar ketzusaka avatar petard avatar

Watchers

James Cloos avatar Codalytics Build avatar Weebly OS Demo avatar

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.