diff --git a/Sources/SwiftFormat/API/Configuration.swift b/Sources/SwiftFormat/API/Configuration.swift index cb5dfb025..98a1b9b70 100644 --- a/Sources/SwiftFormat/API/Configuration.swift +++ b/Sources/SwiftFormat/API/Configuration.swift @@ -292,7 +292,11 @@ public struct Configuration: Codable, Equatable { /// Creates a new `Configuration` by decoding it from the UTF-8 representation in the given data. public init(data: Data) throws { - self = try JSONDecoder().decode(Configuration.self, from: data) + let jsonDecoder = JSONDecoder() + #if canImport(Darwin) || compiler(>=6) + jsonDecoder.allowsJSON5 = true + #endif + self = try jsonDecoder.decode(Configuration.self, from: data) } public init(from decoder: Decoder) throws { diff --git a/Tests/SwiftFormatTests/API/ConfigurationTests.swift b/Tests/SwiftFormatTests/API/ConfigurationTests.swift index 9c6977db8..d983e9cb9 100644 --- a/Tests/SwiftFormatTests/API/ConfigurationTests.swift +++ b/Tests/SwiftFormatTests/API/ConfigurationTests.swift @@ -23,6 +23,9 @@ final class ConfigurationTests: XCTestCase { let emptyDictionaryData = "{}\n".data(using: .utf8)! let jsonDecoder = JSONDecoder() + #if canImport(Darwin) || compiler(>=6) + jsonDecoder.allowsJSON5 = true + #endif let emptyJSONConfig = try! jsonDecoder.decode(Configuration.self, from: emptyDictionaryData) @@ -79,7 +82,11 @@ final class ConfigurationTests: XCTestCase { } """.data(using: .utf8)! - let config = try JSONDecoder().decode(Configuration.self, from: jsonData) + let jsonDecoder = JSONDecoder() + #if canImport(Darwin) || compiler(>=6) + jsonDecoder.allowsJSON5 = true + #endif + let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior) } } @@ -99,9 +106,33 @@ final class ConfigurationTests: XCTestCase { } """.data(using: .utf8)! - let config = try JSONDecoder().decode(Configuration.self, from: jsonData) + let jsonDecoder = JSONDecoder() + #if canImport(Darwin) || compiler(>=6) + jsonDecoder.allowsJSON5 = true + #endif + let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior) } } + func testConfigurationWithComments() throws { + #if !canImport(Darwin) && compiler(<6) + try XCTSkipIf(true, "JSONDecoder does not support JSON5") + #else + let expected = Configuration() + + let jsonData = """ + { + // Indicates the configuration schema version. + "version": 1, + } + """.data(using: .utf8)! + + let jsonDecoder = JSONDecoder() + + jsonDecoder.allowsJSON5 = true + let config = try jsonDecoder.decode(Configuration.self, from: jsonData) + XCTAssertEqual(config, expected) + #endif + } }