wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
266 stars 109 forks source link

Parse local json file #70

Closed wouterwisselink closed 8 years ago

wouterwisselink commented 8 years ago

Hi there,

Is there any way to parse a local json file instead of calling a API endpoint?

Thanks, Wouter

wvteijlingen commented 8 years ago

Hi Wouter,

This is certainly possible. I've added a new section to the README that shows how to do this: https://github.com/wvteijlingen/Spine#using-the-serializer-separately.

Basically you can give the Serializer some NSData, and it will parse this into a JSONAPIDocument for you, and vice versa.

wouterwisselink commented 8 years ago

Thanks @wvteijlingen for your quick reply. I tried this solution but Xcode is complaining about two identifiers he can't find:

Use of unresolved identifier 'Serializer' Use of unresolved identifier 'RomanNumeralValueFormatter'

I'm loading Spine with CocoaPods, also importing the Spine library on top of the file.

wvteijlingen commented 8 years ago

The part about the RomanNumeralValueFormatter and the AsIsKeyFormatter are just examples of how to register value and key formatters. You can leave this out. AsIsKeyFormatter is the default anyway.

The Serializer is public so there shouldn't be any problem there. Perhaps something is wrong with how CocoaPods embeds the framework? I have no experience with CocoaPods and Swift so I cannot advise on that point.

wouterwisselink commented 8 years ago

Ok reinstalling my pods fixed everything. Thanks!

wouterwisselink commented 8 years ago

Hi @wvteijlingen,

I almost have it working now.. But I have a JSONAPIDocument at the moment, but how can I run queries on this type of object?

wvteijlingen commented 8 years ago

What exactly do you mean with queries? You cannot really do anything with a JSONAPIDocument. It just contains all the data that was in the original JSON. You can access the data variable to get the Resource instances.

wouterwisselink commented 8 years ago

I have a resource object now, with lots of relationships and I want to query on the relationship. So for example, I have a object called House with relationships rooms, and every single room has a relationship with windows. I want to get the window with id 2 within room 5.

So I'm looking for something like this:

let room = house.withRelation("rooms", "5")
let window = room.withRelation("window", "2")
wvteijlingen commented 8 years ago

Ah I see. Such functionality is currently not really implemented. But you can pretty much achieve the same thing by using subscripting on ResourceCollection.

if let room = house.rooms?["rooms", "5"], window = room.windows?["windows", "2"] {
  // Do something with the room or window
}

This assumes your relationships as well as your resource types are called rooms and windows.