Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/JSONFeature' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lerbert committed Sep 22, 2021
2 parents 8055b86 + 4bb19fb commit 788105c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions WebService/Sources/Demo/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ struct DemoWebService: WebService {
Group("sensors") {
Sensor()
}

Group("hello") {
Hello()
}
}
}
47 changes: 47 additions & 0 deletions WebService/Sources/Demo/hello.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Apodini
import Foundation


struct Hello: Handler {

func handle() -> [String:String] {
let file = "/buoy/hello.json"
let fileData: String
var result: [String: String] = [:]

//check if file is valid
do {
fileData = try String(contentsOfFile: file, encoding: .utf8);
} catch {
fileData = "Error"
}

if fileData == "Error" {
result["Error"] = "Invalid File"
}

//Proceed with the dictionary conversion
else {
result = convertStringToDictionary(text: fileData)
//result["Error"] = "Invalid File"
}

return result

}
}


func convertStringToDictionary(text: String) -> [String:String] {
var result: [String:String] = [:]
if let data = text.data(using: .utf8) {

do {
result = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String:String]
} catch {
result["Error"] = "Invalid JSON"
}
}

return result
}

0 comments on commit 788105c

Please sign in to comment.