New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FileManager jsonFromFile only support json dictionary #845
Comments
Thanks for reporting @tospery |
The problem is because of downcasting of the returned value.
Do we want to create another API that returns |
If we change the current API to return I think the current functions should be deprecated and replaced with a new function returning Alongside it I wouldn't mind having the same function for reading/writing. I actually have the following extension FileManager {
enum FileError: LocalizedError {
case failedToSave(path: String)
case failedToLoad(path: String)
var errorDescription: String? {
switch self {
case .failedToSave(let path):
return "Failed to save file to path \(path)"
case .failedToLoad(let path):
return "Failed to save file to path \(path)"
}
}
}
func saveToJSON<T: Encodable>(_ object: T?,
at url: URL,
attributes: [FileAttributeKey: Any]? = nil) throws {
guard let object = object else {
if fileExists(atPath: url.path) {
try removeItem(at: url)
}
return
}
let documentURL = url.deletingLastPathComponent()
if !fileExists(atPath: documentURL.path) {
try createDirectory(at: documentURL, withIntermediateDirectories: true, attributes: attributes)
}
let encoder = JSONEncoder()
let data = try encoder.encode(object)
let path = url.path
guard createFile(atPath: path,
contents: data,
attributes: attributes) else {
throw FileError.failedToSave(path: path)
}
}
func loadFromJSON<T: Decodable>(at url: URL) throws -> T? {
guard fileExists(atPath: url.path) else { return nil }
let decoder = JSONDecoder()
let path = url.path
guard let data = contents(atPath: path) else {
throw FileError.failedToLoad(path: path)
}
return try decoder.decode(T.self, from: data)
}
} |
FileManager.jsonFromFile extension do not support JSON array
e.g.
The text was updated successfully, but these errors were encountered: