I need to serialize and deserialize objective-c objects into JSON to store in CouchDB. Do people have any example code for best practice for a general solution? I looked at a few JSON framework and they are stopped at the NSDictionary/NSArray level. i.e. A lot of framework will serialize and deserialize NSDictionary/NSArray into JSON. But I still have to do the work to convert NSDictionary into Objective-C objects.
To make things more complex, my Object A can have reference to an NSArray/NSDictionary of Object Bs.
My question is very similar to this question with addition of the collection requirement.
I have a simple model class, which I wanted to turn into a JSON-Object.
For this purpose I added a „jsonData“-method to my model class: The method turns the model properties into foundation objects (int numbers into NSNumber objects etc.) Then a dictionary is populated with these objects and the corresponding keys (also the later JSON keys). After an (optional) check for validity, the JSON data object ist created with the NSJSONSerialization class „dataWithJSONObject“ method and returned.
}
This is possible using the RestKit library's object mapping system.
http://restkit.org/
It sounds like you're looking for a serialization library that can let you convert objects of your own custom classes into JSON, and then reconstitute them back. Serialization of property-list types (NSArray, NSNumber, etc.) already exists in 3rd party libraries, and is even built into OS X 10.7 and iOS 5.
So, I think the answer is basically "no". I asked this exact question a month or two ago on the cocoa-dev mailing list, and the closest I got to a hit was from Mike Abdullah, pointing to an experimental library he'd written:
https://github.com/mikeabdullah/KSPropertyListEncoder
This archives objects to in-memory property lists, but as I said there are already APIs for converting those into JSON.
There's also a commercial app called Objectify that claims to be able to do something similar:
http://tigerbears.com/objectify/
It's possible I'll end up implementing what you're asking for as part of my CouchCocoa library, but I haven't dived into that task yet.
https://github.com/couchbaselabs/CouchCocoa
Did you mean this? ;)
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946
You can easily add JSON capability to NSObject class with the help of NSDictionary,NSArray and NSJSONSerialization
Serialization:
Just see the example it will be very easy to understand.
Adding JSON Capability to NSObject Class:-
JSON String Generator:-
In iOS 5, Apple introduced NSJSONSerialization, for parsing JSON strings so by using that we will generate JSON string.
Moving towards Apple’s implementation is always safer to use since you have the guarantee that it will be maintained and kept up to date.
Way to use:-
Reference
Deserialization:
It's usual way of getting the deserialized data into NSDictionary or NSArray then assign it to class properties.
I am sure using the methods and ideas used above you can serialize & deserialize complex json easily.
Finally we can solve this problem easily using JSONModel. This is the best method so far. JSONModel is a library that generically serialize/deserialize your object based on Class. You can even use non-nsobject based for property like
int
,short
andfloat
. It can also cater nested-complex JSON.Considering this JSON example:
1) Deserialize example. in header file:
in implementation file:
2) Serialize Example. In implementation file:
And this is NSLog result from Serialize example: