Coder Social home page Coder Social logo

jordanhamill / swiftyjsonmilk Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 20 KB

Milk for your Swift JSON serialization. A wrapper around SwiftyJSON for easy serialization of classes and structs.

License: MIT License

Swift 94.49% Ruby 2.97% Objective-C 2.54%

swiftyjsonmilk's Introduction

A SwiftyJSON wrapper that implements Milk Serializer.

Note: Milk requires Swift 2.0 as it uses protocol extensions to provide default implementations of common methods.

Example

struct Person {
    let name: String
    let age: Int
    let weight: Float

    init(age: Int, name: String, height: weight) {
        self.age = age
        self.name = name
        self.weight = weight
    }
}

extension Person: Serializable {
    private init?(name: String?, age: Int?, weight: Float?) {
        if let name = name, age = age, weight = weight {
            self.age = age
            self.name = name
            self.weight = weight
        } else {
            return nil
        }
    }

    func serialize(serializer: Serializer) {
        serializer["age"] <- age
        serializer["name"] <- name
        serializer["weight"] <- weight
    }

    static func deserialize(deserializer: Serializer) -> Patient? {
        return Person(name:   <-deserializer["name"],
                      age:    <-deserializer["age"],
                      weight: <-deserializer["weight"])
    }
}
let jsonSerializer: Serializer = JSONSerializer()
let person: Person = Person(name: "Matt", age: 12, weight: 71.2)
person.serialize(jsonSerializer)

if let jsonString = jsonSerializer.json.rawString() {
    print(jsonString)
	// {
	//    "name": "Matt",
	//    "age": 12,
	//    "weight": 71.2
	// }
}

swiftyjsonmilk's People

Stargazers

Franco Rondini avatar

Watchers

James Cloos 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.