Skip to content

Commit a11a78a

Browse files
committed
Allow JSON5 in JSONDecoder
I hadn't realized JSONDecoder has gained JSON5 support, so we don't need to do the manual // stripping and can just enable 5 mode when decoding JSON :-)
1 parent 99ae030 commit a11a78a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
3838
let config: Configuration?
3939

4040
if let configData = try? Data(contentsOf: configFile) {
41-
config = try? JSONDecoder().decode(Configuration.self, from: configData)
41+
let decoder = JSONDecoder()
42+
decoder.allowsJSON5 = true
43+
config = try? decoder.decode(Configuration.self, from: configData)
4244
} else {
4345
config = nil
4446
}

Sources/SwiftJavaConfigurationShared/Configuration.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,14 @@ public func readConfiguration(configPath: URL, file: String = #fileID, line: UIn
174174
}
175175

176176
public func readConfiguration(string: String, configPath: URL?, file: String = #fileID, line: UInt = #line) throws -> Configuration? {
177-
let cleanedConfigString = string
178-
.split(separator: "\n")
179-
.filter { line in
180-
!line.trimmingCharacters(in: .whitespaces).starts(with: "//")
181-
}.joined(separator: "\n")
182-
183-
guard let configData = cleanedConfigString.data(using: .utf8) else {
177+
guard let configData = string.data(using: .utf8) else {
184178
return nil
185179
}
186180

187181
do {
188-
return try JSONDecoder().decode(Configuration.self, from: configData)
182+
let decoder = JSONDecoder()
183+
decoder.allowsJSON5 = true
184+
return try decoder.decode(Configuration.self, from: configData)
189185
} catch {
190186
throw ConfigurationError(message: "Failed to parse SwiftJava configuration at '\(configPath.map({ $0.absoluteURL.description }) ?? "<no-path>")'! \(#fileID):\(#line)", error: error,
191187
file: file, line: line)

0 commit comments

Comments
 (0)