-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJSON.swift
52 lines (43 loc) · 925 Bytes
/
JSON.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// JSON.swift
// Noze.io
//
// Created by Helge Heß on 6/3/16.
// Copyright © 2016 ZeeZide GmbH. All rights reserved.
//
import streams
import http
import json
@_exported import Freddy
public extension ServerResponse {
// TODO: add jsonp
// TODO: be a proper stream
// TODO: Maybe we don't want to convert to a `JSON`, but rather stream real
// object.
func json(_ object: JSON) {
if canAssignContentType {
setHeader("Content-Type", "application/json; charset=utf-8")
}
writeJSON(object: object)
end()
}
}
// MARK: - Helpers
public extension ServerResponse {
func json(_ object: JSONEncodable) {
json(object.toJSON())
}
func json(_ object: Any?) {
if let o = object {
if let jsonEncodable = (o as? JSONEncodable) {
json(jsonEncodable)
}
else {
json(String(0))
}
}
else {
json(.Null)
}
}
}