Skip to content
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

Add convenience methods for reading Codable objects #25

Closed
dylanslewis opened this issue Sep 10, 2017 · 1 comment
Closed

Add convenience methods for reading Codable objects #25

dylanslewis opened this issue Sep 10, 2017 · 1 comment

Comments

@dylanslewis
Copy link

dylanslewis commented Sep 10, 2017

Note: This will require Codable from Swift 3.2/4, which is why I'm not submitting a PR for this yet.

This is a proposal for adding convenience methods to use the Codable protocol, to allow reading a File directly to a decoded object type.

Example JSON Decodable convenience:

extension File {
	func readAsDecodedJSON<D: Decodable>(using decoder: JSONDecoder = .init()) throws -> D {
		let fileData = try read()
		return try decoder.decode(D.self, from: fileData)
	}
}

Example JSON Decodable call site:

let myObjectFile = File(path: ...)
let myObject: DecodedJSONObject = try myObjectFile.readAsDecodedJSON()
@JohnSundell
Copy link
Owner

Hi @dylanslewis! 👋

I consider this out of scope for Files. Since it only requires a few lines in application code, I don't see a big benefit to implement (& maintain) such an implementation in the framework itself. I also don't want to tie Files too much to one way of parsing JSON, since people use many different libraries and techniques to do so.

Keep the suggestions coming though 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants