Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// IndexHandler.swift
// PerfectJSONEncoder
//
// Created by Robert Bojor on 06/05/16.
// Copyright © 2016 Robert Bojor. All rights reserved.
//
import Foundation
import PerfectLib
public func PerfectServerModuleInit()
{
Routing.Handler.registerGlobally()
Routing.Routes["GET", ["/"]] = { (_:WebResponse) in return IndexHandler() }
}
class IndexHandler: RequestHandler {
func handleRequest(request: WebRequest, response: WebResponse) {
let car1:[String:AnyObject] = ["Wheels":4, "Color":"Black"]
let car2:[String:AnyObject] = ["Wheels":3, "Color":"Red"]
let cars = [car1, car2]
var finalData:NSData!
do {
finalData = try NSJSONSerialization.dataWithJSONObject(cars, options: NSJSONWritingOptions.init(rawValue: 0))
} catch {
finalData = NSData()
}
let final = NSString(data: finalData, encoding: NSUTF8StringEncoding)! as String
response.appendBodyString(final)
response.requestCompletedCallback()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment