My own code: How to treat JSON Deserialization objects and DB objects at the same time

So I’ve been using JSON Deserialization with the SwiftyJSON framework for my latest project, and it works like a charm. Thing is, this project is still very open for changes along the way, one of them being that the team is still not sure which information should be persisted in a Database. Because of this I was looking for a way to integrate ORMShark framework with SwiftyJSON in order to create persisted objects right after they are deserialized.

This is what I came up with:

1) The SwiftyJSONAccelerator Class

This class is easily created using CocoaPods’ pod 'SwiftyJSON' installed in the project and the JSONAccelerator app from their GitHub page.
The Application turns any valid JSON into an entire ready-to-use class, like the choice below:

The Class itself manages key-value data from JSON and creates an object from it. Great, huh?

But I still needed to work with Shark, and they have their own Type: SRKObject, and so I couldn’t possibly use the same Class for both of them.

2) The Shark helper Class

So I decided to create a new helper Class for the SRKObject and create a method that will take in an SwiftyJSON object as argument and pass its values to the SRKObject.

This is what the Class looks like:

3) Implementing the classes

Now whenever I need to persist an object after the request, I can just call the helper’s init() and update() methods and, voilá! It’s done!

And when I need to call the object back or get any of it’s properties, I just need to:

Hope you liked it and feel free to use my solution if you want!
See ya!

Leave a Reply

Your email address will not be published. Required fields are marked *