thoughtbot / Argo

Functional JSON parsing library for Swift
https://thoughtbot.com
MIT License
3.49k stars 198 forks source link

Root Array decoding to typealiased Array #105

Closed penland365 closed 9 years ago

penland365 commented 9 years ago

Howdy, first I wanted to say thanks so much for this library - we are getting a huge amount of use out of it.

We're hitting a third party API that is returning a root array much like how you have in your test suite. Currently, we are typealiasing the plural of the Model like so:

struct Person . . . 
typealias People = [Person]

My question is, is there an easy to way to decode into this? We haven't been able to figure it out syntactically yet. For the Person type, the syntax goes like this

 if let j: AnyObject = body {
                let value = JSONValue.parse(j)
                let person = Person.decode(value)
                callback(.Success(Box(person)))
            }

We even tried giving up on the typealias People = [Person] and used a simple struct w/ one data type and the JSONDecodable extension, but I just can't quite grasp how the global decode function works - Swift 1.2 is most certainly not a fan, claiming it can't find it.

Thanks!

tonyd256 commented 9 years ago

@penland365 Hello, What version of Argo are you using? The global decode function might not be available in your version. If it is, you should be able to decode the json into the array like so:

let people: People? = decode(j)

The decode function takes the AnyObject and returns the decoded type/types, which, in this case, is People or [Person]. If that isn't working for you, would you mind posting more code around the issue so I can see what might be wrong?

penland365 commented 9 years ago

Hi @tonyd256 - I'm currently on Using Argo (0.4.3) according to my Podfile - what version do I need to be at?

penland365 commented 9 years ago

Hmm, this may be a cocoa pods issue, I'm not sure - decode.swift is in the project, but Swift swears by all the compiler gods that it cannot find it. decode is the only thing I haven't been able to find however.

gfontenot commented 9 years ago

Can you share your podfile? ( Or at least the relevant line?)

On Apr 13, 2015, at 18:26, Jeffrey N. Davis notifications@github.com wrote:

Hmm, this may be a cocoa pods issue, I'm not sure - decode.swift is in the project, but Swift swears by all the compiler gods that it cannot find it. decode is the only thing I haven't been able to find however.

— Reply to this email directly or view it on GitHub.

penland365 commented 9 years ago
platform :ios, '8.0'
use_frameworks!

target 'jonah' do
  pod 'Argo'
end

target 'jonahTests' do
  pod 'Argo'
end

I've excluded additional pods, but that's the meat of it. That could be wrong format - I'm a Scala guy dipping in to another world here. But I do have access ( and do use ) most if not all of the operators in both JSONOperators.swift and Operators.swift

Running pod install gives Using Argo 0.4.3 (was 0.4.3)

penland365 commented 9 years ago

A bit more information - I tried running off of master w/ the following:

platform :ios, '8.0'
use_frameworks!

target 'jonah' do
  pod 'Argo', :git => 'https://github.com/thoughtbot/Argo.git'
end

target 'jonahTests' do
  pod 'Argo', :git => 'https://github.com/thoughtbot/Argo.git'
end

In this case, I can see the decode.swift file: screen shot 2015-04-13 at 8 00 38 pm

The compiler is still confused however and it can't see it. Running pod install gives the version here at 0.4.2

penland365 commented 9 years ago

To give a bit of an update, I can't compile w/ Swift 1.2 off of master, 0.4.2. I'm being told that no such module Box has been found in the file Decoded.swift

gfontenot commented 9 years ago

You really shouldn't be pointed at master. We'd rather you point at a specific release.

The reason you aren't finding Box is because it hasn't been added to the Podfile

penland365 commented 9 years ago

RIP penland365, stung by a build tool.

gfontenot commented 9 years ago

Hahaha. Glad that helped, sorry it took so long to get there.