JSONAccess

protocol JSONAccess

A protocol for decoding JSON data into models.

The protocol has an extension that provides a default implementation of two non required methods that decode instances of a generic decoding service type from JSON files.

  • A protocol method that is fired if an attempt to decode a JSON file by one of the protocol extension methods returns nil.

    Declaration

    Swift

    static func printAlertForCorruptJSON(for file: String)

    Parameters

    file

    The last path component of the URL to the JSON file that returned nil on an attempt to decode it.

  • A method that decodes an instance of a generic decoder service type from a single JSON file.

    The method is designed to be used where a project resource directory contains a JSON file identifiable by name that is known to be valid for a decoder service type belonging to the target. The file can be decoded even where the directory contains multiple JSON files that require different service types.

    Note

    Note The service type T used to decode the JSON file is inferred using the self keyword. self refers to the variable that the return value of the method is assigned.

    Important

    Important The method will only attempt to decode a JSON file that has the same name as passed to the parameter resourceName.

    Declaration

    Swift

    static func decoderServiceFromBundle<T>(_ bundle: Bundle, inSubdirectory subdirectory: String?, forResourceName resourceName: String) -> T? where T : Decodable

    Parameters

    bundle

    The bundle used to access the JSON file that requires decoding.

    subdirectory

    The name of an optional subdirectory in the main bundle containing the JSON file to be decoded.

    resourceName

    The name of the JSON file to be decoded (excluding the .json extension).

    Return Value

    The specific decoder service instance decoded from the given JSON data or nil if there are no valid JSON files that can be decoded by the service type T.

  • A method that decodes an array of instances of a generic decoder service type from multiple JSON files.

    The method is designed to be used where a project resource directory contains multiple JSON files some of which are known to be valid for a decoder service type belonging to the target.

    Valid JSON files can be decoded if other files contained in the directory that require a different service type are known by name. The names of the files that require a different service type must be added to the optional excludedFiles array parameter argument. The method will then not attempt to decode any JSON file named in the array.

    Note

    Note It is not necessary for the files of the service type to be decoded to be known by name.

    The service type T used to decode the JSON files is inferred using the self keyword. self refers to the variable that the return value of the method is assigned.

    The return value is not an optional and will simply return an empty array if there are no valid JSON files in the bundle.

    The excludedFiles array can also be used to name specific JSON files of the required service type to prevent them from being decoded.

    Declaration

    Swift

    static func decoderServicesFromBundle<T>(_ bundle: Bundle, inSubdirectory subdirectory: String, excludingFiles excludedFiles: [String]? = nil) -> [T] where T : Decodable

    Parameters

    bundle

    The bundle used to access the JSON files that require decoding.

    subdirectory

    The name of an optional subdirectory in the main bundle containing the JSON files to be decoded.

    excludedFiles

    An optional array of strings of the names of JSON files to exclude from being decoded ( including the .json extension)

    Return Value

    An array of decoder service instances decoded from the given JSON data or an empty array if there are no valid JSON files that can be decoded by the service type T.