Skip to content

Commit 1c551a2

Browse files
committed
remove all json serialization
1 parent 65b4f9c commit 1c551a2

18 files changed

Lines changed: 76 additions & 259 deletions

Sources/Compute/Cache.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,12 @@ public struct Cache: Sendable {
4848
return try .init(trx)
4949
}
5050

51-
public static func getOrSet(
52-
_ key: String, _ handler: () async throws -> ([String: Sendable], CachePolicy)
53-
) async throws -> Entry {
54-
let trx = try await Fastly.Cache.getOrSet(key) {
55-
let (json, cachePolicy) = try await handler()
56-
let data = try JSONSerialization.data(withJSONObject: json)
57-
return (.bytes(data.bytes), cachePolicy)
58-
}
59-
return try .init(trx)
60-
}
61-
62-
public static func getOrSet(
63-
_ key: String, _ handler: () async throws -> ([Sendable], CachePolicy)
51+
public static func getOrSet<T: Encodable>(
52+
_ key: String, _ handler: () async throws -> (T, CachePolicy)
6453
) async throws -> Entry {
6554
let trx = try await Fastly.Cache.getOrSet(key) {
6655
let (json, cachePolicy) = try await handler()
67-
let data = try JSONSerialization.data(withJSONObject: json)
56+
let data = try JSONEncoder().encode(json)
6857
return (.bytes(data.bytes), cachePolicy)
6958
}
7059
return try .init(trx)

Sources/Compute/Compute.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
// Created by Andrew Barba on 1/11/22.
66
//
77

8-
@_exported import Foundation
8+
#if canImport(FoundationEssentials)
9+
@_exported import FoundationEssentials
10+
#else
11+
@_exported import Foundation
12+
#endif
913

1014
@MainActor
1115
public func onIncomingRequest(

Sources/Compute/Console.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ public struct Console: Sendable {
2525
}
2626

2727
public func error(_ items: Any...) {
28-
var errorStream = StandardErrorOutputStream()
29-
let text = items.map { String(describing: $0) }.joined(separator: " ")
30-
if let prefix = prefix {
31-
print(prefix, text, to: &errorStream)
32-
} else {
33-
print(text, to: &errorStream)
34-
}
35-
}
36-
}
37-
38-
private struct StandardErrorOutputStream: TextOutputStream {
39-
40-
private let stderr = FileHandle.standardError
41-
42-
func write(_ string: String) {
43-
guard let data = string.data(using: .utf8) else {
44-
return
45-
}
46-
stderr.write(data)
28+
log("error:", items)
4729
}
4830
}

Sources/Compute/Fanout/FanoutClient.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,6 @@ public struct FanoutClient: Sendable {
5252
let content = try encoder.encode(value)
5353
return try await publish(content, to: channel)
5454
}
55-
56-
@discardableResult
57-
public func publish(_ json: Any, to channel: String) async throws -> FetchResponse {
58-
let data = try JSONSerialization.data(withJSONObject: json)
59-
let content = String(data: data, encoding: .utf8)
60-
return try await publish(content, to: channel)
61-
}
62-
63-
@discardableResult
64-
public func publish(_ jsonObject: [String: Any], to channel: String) async throws
65-
-> FetchResponse
66-
{
67-
let data = try JSONSerialization.data(withJSONObject: jsonObject)
68-
let content = String(data: data, encoding: .utf8)
69-
return try await publish(content, to: channel)
70-
}
71-
72-
@discardableResult
73-
public func publish(_ jsonArray: [Any], to channel: String) async throws -> FetchResponse {
74-
let data = try JSONSerialization.data(withJSONObject: jsonArray)
75-
let content = String(data: data, encoding: .utf8)
76-
return try await publish(content, to: channel)
77-
}
7855
}
7956

8057
extension FanoutClient {

Sources/Compute/Fanout/FanoutMessage.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,6 @@ extension FanoutMessage {
7474
return try decoder.decode(type, from: data())
7575
}
7676

77-
public func json<T: Sendable>() throws -> T {
78-
return try JSONSerialization.jsonObject(with: data()) as! T
79-
}
80-
81-
public func jsonObject() throws -> [String: Sendable] {
82-
return try json()
83-
}
84-
85-
public func jsonArray() throws -> [Sendable] {
86-
return try json()
87-
}
88-
8977
public func data() throws -> Data {
9078
guard let data = content.data(using: .utf8) else {
9179
throw FanoutMessageError.invalidFormat

Sources/Compute/Fanout/IncomingRequest+Fanout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension IncomingRequest {
3838
guard let token = headers[.gripSig] else {
3939
throw FanoutRequestError.invalidSignature
4040
}
41-
let jwt = try JWT(token: token)
41+
let jwt = try JWT<EmptyJWTPayload>(token: token)
4242
try jwt.verify(key: fanoutPublicKey)
4343
}
4444

Sources/Compute/Fastly/FastlyLogger.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ extension Fastly {
3737
return try write(data)
3838
}
3939

40-
@discardableResult
41-
public func write(
42-
_ jsonObject: [String: Any],
43-
options: JSONSerialization.WritingOptions = [.sortedKeys]
44-
) throws -> Int {
45-
let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)
46-
return try write(data)
47-
}
48-
49-
@discardableResult
50-
public func write(
51-
_ jsonArray: [Any],
52-
options: JSONSerialization.WritingOptions = [.sortedKeys]
53-
) throws -> Int {
54-
let data = try JSONSerialization.data(withJSONObject: jsonArray, options: options)
55-
return try write(data)
56-
}
57-
5840
@discardableResult
5941
public func write(_ data: Data) throws -> Int {
6042
let text = String(bytes: data, encoding: .utf8) ?? ""

Sources/Compute/Fastly/FastlyUtils.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ extension DataProtocol {
9494
}
9595
}
9696

97-
extension CharacterSet {
98-
99-
static let javascriptURLAllowed: CharacterSet =
100-
.alphanumerics.union(.init(charactersIn: "-_.!~*'()")) // as per RFC 3986
101-
}
97+
// extension CharacterSet {
98+
// static let javascriptURLAllowed: CharacterSet =
99+
// .alphanumerics.union(.init(charactersIn: "-_.!~*'()")) // as per RFC 3986
100+
// }

Sources/Compute/Fetch/FetchRequest.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ extension FetchRequest {
116116
return Body.json(data)
117117
}
118118

119-
public static func json(_ jsonObject: [String: Any]) throws -> Body {
120-
let data = try JSONSerialization.data(withJSONObject: jsonObject)
121-
return Body.json(data)
122-
}
123-
124-
public static func json(_ jsonArray: [Any]) throws -> Body {
125-
let data = try JSONSerialization.data(withJSONObject: jsonArray)
126-
return Body.json(data)
127-
}
128-
129119
public var defaultContentType: String? {
130120
switch self {
131121
case .json:

Sources/Compute/Fetch/FetchResponse.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@ extension FetchResponse {
4141
return try await body.decode(type, decoder: decoder)
4242
}
4343

44-
public func json<T: Sendable>() async throws -> T {
45-
return try await body.json()
46-
}
47-
48-
public func jsonObject() async throws -> [String: Sendable] {
49-
return try await body.jsonObject()
50-
}
51-
52-
public func jsonArray() async throws -> [Sendable] {
53-
return try await body.jsonArray()
54-
}
55-
5644
public func formValues() async throws -> HTTPSearchParams {
5745
return try await body.formValues()
5846
}

0 commit comments

Comments
 (0)