view Simoleon/Functions/Read.swift @ 153:2590ee472aa9

Add test to check currency existence
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Mon, 23 Aug 2021 17:14:47 +0100
parents 6eac99e99b96
children
line wrap: on
line source

//
//  ParseJson.swift
//  Simoleon
//
//  Created by Dennis Concepción Martín on 11/07/2021.
//

import Foundation


// Read JSON File
func read<T: Decodable>(json filename: String) throws -> T {
    let data: Data
    
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
    else {
        throw JsonErrors.fileMissing
    }
    
    do {
        data = try Data(contentsOf: file)
    } catch {
        throw JsonErrors.loadFailed(cause: error.localizedDescription)
    }
    
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        throw JsonErrors.parseFailed(cause: error.localizedDescription)
    }
}