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

How best to extract raw dictionaries? #171

Closed
dcaunt opened this issue Mar 16, 2017 · 2 comments
Closed

How best to extract raw dictionaries? #171

dcaunt opened this issue Mar 16, 2017 · 2 comments

Comments

@dcaunt
Copy link

dcaunt commented Mar 16, 2017

I have some JSON where one key path returns an Array<Dictionary<String,Any>>, that is an array of [String:Any] and I want to pass this to a Transformer for manipulation into a different structure.

The issue I have is that Extractor's array and arrayOptional methods expect to return an array of Decodable T. I can see why this works for Transformer<String, URL> and the like thanks to the extensions in StandardLib.swift, but [String:Any] does not conform to Decodable.

The only way I can get this to work is by adding an extension on Dictionary as follows:

extension Dictionary: Decodable {
    public static func decode(_ e: Extractor) throws -> Dictionary<Key, Value> {
        return try castOrFail(e.rawValue)
    }
}

Is this correct? Should this be part of StandardLib.swift, or am I approaching this in the wrong way?

@danielmhanover
Copy link

+1

@dcaunt
Copy link
Author

dcaunt commented Apr 6, 2017

I figured this out with a little help from #149 (comment). As Extractor implements Decodable, we can retrieve a collection and map as described:

let extractors: [String:Extractor] = e <|-| "some_key"

@dcaunt dcaunt closed this as completed Apr 6, 2017
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