There are a number of questions and answers on SO that ask how do I serialise an object to in objective c.
- Serialize and Deserialize Objective-C objects into JSON
- Objective C serialize list of complex objects
- Serialize custom object to JSON which contains NSMutableArray
- How to serialize a class in IOS sdk (Objective-c)?
The following 3 methods are all mentioned in the above links.
1) Use NSJSONSerialization
to serialise the object to JSON. Seems good but this requires the object in question to be either an array or dictionary at its top level. Common solution is to declare custom toDictionary
or serialise
method that loops over the properties and sets the relevant key and values.
2) Conform to the NSCoder
protocol, a little like the approach above but there seems to be some confusion around whether or not this can serialise to JSON or just to NSData
.
3) Third party library.
I'm getting slightly confused as to what approach to take. I want to serialise to JSON, there are contradictory answers some stating you can use NSCoder some saying not. I know that a third party app will work however I'd rather implement something simple like options 1 or 2.
Thoughts?