1

there:

Our app uses JSON to talk to the backend server. And as time goes by, the amount of APIs are growing dramatically. For each new API that is added, or an existing API modified, I have to go through the following tedious process so far

  1. abstract an class properties into a class to fit into the logic model
  2. implement a construction of the request dictionary, serialize it and send
  3. for the responses, I deserialize them, and then look at the response definition(schema), and unpack them.

As you can imagine, for some complex APIs, where values that are again recursive dictionaries, it's extremely error prone and annoying to construct and parse...

After a while, I found that there's a software Objectify which creates code for JSON. For response parsing, it's working for me(even though, I have trouble mapping the code it generates using properties into my object model, which differs). But it's working for my requirements to some extent -- I can eliminate half of the tedious work.

However, I found my self stuck with Objectify. So my question is, if you know any way to "automatically construct the JSON request", using either iOS build-in tools, or external libs?

The tool Objectify actually parses JSON data/schema, and generates code with a bunck of properties and 2 functions which unpacks data into dictionary. After some thinking, I think it's not possible for this tool to do that.

If there's no existing way(which I think it's very difficult, since, no way to know and construct the requests dictionaries automatically -- there could be same key in an outer level and an internal level). I am now thinking about declaring a property, and force all new APIs and implementor to implement their own (Dictionary *)constructRequest method.

Anyone faced this problem before, any suggestions?

Thanks in advance!

RAX
  • 211
  • 1
  • 12
  • Have you looked into RestKit? – Sylvain Guillopé May 26 '13 at 01:44
  • @SylvainGuillopé Thank you! I'll checking it out -- looking at the first sight, it's what I need, I'll play with it a little bit. Thanks again! – RAX May 26 '13 at 02:01
  • Hi, check my method [here](http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class/16771574#16771574) . A generic, clean and less error-prone for JSON request and response – Alphapico May 28 '13 at 09:47

1 Answers1

0

I'll suggest you to checkout RestKit. Here is a quick tutorial to be comfortable with RestKit

http://www.raywenderlich.com/13097/intro-to-restkit-tutorial

Here are some official examples by RestKit. https://github.com/RestKit/RestKit/tree/development/Examples

See the RestKit wiki for Installation Guilde: https://github.com/RestKit/RestKit/wiki

I have used it in many of my projects, and it really has made my life easy.

Some more links to be familiar with RestKit

http://mobile.tutsplus.com/tutorials/iphone/restkit_ios-sdk/

http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/

Khawar
  • 9,151
  • 9
  • 46
  • 67