Skip to content

Commit 5115cb8

Browse files
committed
Don't escape slashes in various JSONEncoders
This makes the JSON easier to work with, especially for paths.
1 parent 1045a38 commit 5115cb8

File tree

5 files changed

+8
-12
lines changed

5 files changed

+8
-12
lines changed

Sources/Diagnose/RequestInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ package struct RequestInfo: Sendable {
4040

4141
package func request(for file: URL) throws -> String {
4242
let encoder = JSONEncoder()
43-
encoder.outputFormatting = .prettyPrinted
43+
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
4444
guard var compilerArgs = String(data: try encoder.encode(compilerArgs), encoding: .utf8) else {
4545
throw GenericError("Failed to encode compiler arguments")
4646
}

Sources/SKLogging/CustomLogStringConvertible.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ extension Optional where Wrapped: CustomLogStringConvertible {
101101
extension Encodable {
102102
package var prettyPrintedJSON: String {
103103
let encoder = JSONEncoder()
104-
encoder.outputFormatting.insert(.prettyPrinted)
105-
encoder.outputFormatting.insert(.sortedKeys)
104+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
106105
guard let data = try? encoder.encode(self) else {
107106
return "\(self)"
108107
}
@@ -137,7 +136,7 @@ extension Encodable {
137136
let jsonObject = try? JSONSerialization.jsonObject(with: encoded),
138137
let data = try? JSONSerialization.data(
139138
withJSONObject: redact(subject: jsonObject),
140-
options: [.prettyPrinted, .sortedKeys]
139+
options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
141140
),
142141
let string = String(data: data, encoding: .utf8)
143142
else {

Sources/SKTestSupport/CheckCoding.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ package func checkCoding<T: Codable & Equatable>(
2424
line: UInt = #line
2525
) {
2626
let encoder = JSONEncoder()
27-
encoder.outputFormatting.insert(.prettyPrinted)
28-
encoder.outputFormatting.insert(.sortedKeys)
27+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
2928

3029
let data = try! encoder.encode(WrapFragment(value: value))
3130
let wrappedStr = String(data: data, encoding: .utf8)!
@@ -64,8 +63,7 @@ package func checkEncoding<T: Encodable & Equatable>(
6463
line: UInt = #line
6564
) {
6665
let encoder = JSONEncoder()
67-
encoder.outputFormatting.insert(.prettyPrinted)
68-
encoder.outputFormatting.insert(.sortedKeys)
66+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
6967
let data = try! encoder.encode(value)
7068
let str = String(data: data, encoding: .utf8)!
7169
// Remove trailing whitespace to normalize between corelibs and Apple Foundation.
@@ -101,8 +99,7 @@ package func checkCoding<T: Codable>(
10199
body: (T) -> Void
102100
) {
103101
let encoder = JSONEncoder()
104-
encoder.outputFormatting.insert(.prettyPrinted)
105-
encoder.outputFormatting.insert(.sortedKeys)
102+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
106103
let data = try! encoder.encode(value)
107104
let str = String(data: data, encoding: .utf8)!
108105
// Remove trailing whitespace to normalize between corelibs and Apple Foundation.

Sources/SKTestSupport/IndexedSingleSwiftFileTestProject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ package struct IndexedSingleSwiftFileTestProject {
130130
]
131131
)
132132
let encoder = JSONEncoder()
133-
encoder.outputFormatting = .prettyPrinted
133+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
134134
try encoder.encode(compilationDatabase).write(
135135
to: testWorkspaceDirectory.appendingPathComponent(JSONCompilationDatabaseBuildSystem.dbName)
136136
)

Tests/BuildSystemIntegrationTests/CompilationDatabaseTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class CompilationDatabaseTests: XCTestCase {
3232
line: UInt = #line
3333
) throws {
3434
let encoder = JSONEncoder()
35-
encoder.outputFormatting.insert(.sortedKeys)
35+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
3636
let encodedString = try String(data: encoder.encode(cmd), encoding: .utf8)
3737
XCTAssertEqual(encodedString, expected, file: file, line: line)
3838
}

0 commit comments

Comments
 (0)