From 275288ea61bbae3ffabf4e350da20123e338279c Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 18 Feb 2024 15:47:08 +0800 Subject: [PATCH] Hotenka // Deprecate NSJSONSerialization. --- .../Sources/Hotenka/HotenkaChineseConverter.swift | 4 +--- .../Tests/HotenkaTests/HotenkaTests_JSON.swift | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Packages/vChewing_Hotenka/Sources/Hotenka/HotenkaChineseConverter.swift b/Packages/vChewing_Hotenka/Sources/Hotenka/HotenkaChineseConverter.swift index 12305c12..9a8baf24 100644 --- a/Packages/vChewing_Hotenka/Sources/Hotenka/HotenkaChineseConverter.swift +++ b/Packages/vChewing_Hotenka/Sources/Hotenka/HotenkaChineseConverter.swift @@ -100,9 +100,7 @@ public class HotenkaChineseConverter { dictFiles = .init() do { let rawData = try Data(contentsOf: URL(fileURLWithPath: jsonDir)) - guard let rawJSON: [String: [String: String]] = try JSONSerialization.jsonObject(with: rawData) as? [String: [String: String]] else { - throw NSError() - } + let rawJSON = try JSONDecoder().decode([String: [String: String]].self, from: rawData) dict = rawJSON } catch { NSLog("// Exception happened when reading dict json at: \(jsonDir).") diff --git a/Packages/vChewing_Hotenka/Tests/HotenkaTests/HotenkaTests_JSON.swift b/Packages/vChewing_Hotenka/Tests/HotenkaTests/HotenkaTests_JSON.swift index 1c52ef37..1de4a7b4 100644 --- a/Packages/vChewing_Hotenka/Tests/HotenkaTests/HotenkaTests_JSON.swift +++ b/Packages/vChewing_Hotenka/Tests/HotenkaTests/HotenkaTests_JSON.swift @@ -40,7 +40,10 @@ extension HotenkaTests { let testInstance: HotenkaChineseConverter = .init(dictDir: testDataPath) NSLog("// Loading complete. Generating json dict file.") do { - try JSONSerialization.data(withJSONObject: testInstance.dict, options: .sortedKeys).write(to: URL(fileURLWithPath: testDataPath + "convdict.json")) + let urlOutput = URL(fileURLWithPath: testDataPath + "convdict.json") + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys + try encoder.encode(testInstance.dict).write(to: urlOutput, options: .atomic) } catch { NSLog("// Error on writing strings to file: \(error)") }