I seek a iPhone json library that parse json strings into Custom Objects (such as Employee, Course, etc, not into NSDictionary or NSArray)
Thanks.
I don't think one exists. How would you design a general one, without knowing in advance about the kinds of objects that you'll need to parse into? And more critically, JSON is only designed to encode basic data types; it is not intended to be a full object serialization framework (those are usually language specific or have language-specific bindings). However, it should be very easy to write a converter to your custom objects.
Related
https://github.com/enter link description herePoojaB26/ParsingJSON-Flutter/blob/master/assets/page.json
That's a tiny JSON file and there is no reason for premature optimizations.
Just deserialize it and then access the items of the generated Dart data structures to get what you need. Everything else is probably equally expensive and won't buy you anything.
I use Core Data within my iOS 7 app to handle the editing and creation of entities. The entities have relationships between them, which all have inverses (as Apple advises).
For the sake of this question, let's pick any one of these interrelated entities and call it the Root entity: the thing that I want to encode with; the thing that logically lives on the 'top' of the hierarchy. I will call this the 'object graph'.
The question is:
What's the easiest way of encoding and decoding such an object graph to and from NSData?
The reason I want to do this is that I'd like my Core Data object graph to be persisted onto a cloud service, without the need of writing my own NSIncrementalStore subclass (it's a bit involved...!).
AutoCoding together with HRCoder almost looks like it could do the job, but I've experimented with this combination and it doesn't quite work with NSManagedObjects at the time of writing.
Still, I'm seeking alternatives. There can't only be one way to do this, surely.
It doesn't have to be JSON, but it'd be nice. Binary would be fine.
It seems to me you do not need to subclass NSIncrementalStore. You can create records and save them to your store with a plain vanilla store created via addPersistentStoreWithType:... with a NSPersistentStoreCoordinator.
The straight-forward way is to handle the incoming JSON by simply taking the data and copying it to the properties of your NSManagedObject subclasses, like this:
object.title = jsonDictionary[#"title"];
object.numericAttribute = [jsonDictionary[#"numericAttribute] integerValue];
If you take care about naming the attribute and entity names exactly the same you can maybe use some shortcuts using KVC, like
[object setValue:jsonDictionary[key] forKey:key];
I once did the above for a large legacy project where it was not feasible to repeat the old attribute names, so I used a custom property list (plist) to match around 800 attribute names.
I'm going to be generating CFUUID objects (if there's a better way to create a GUID on iOS, let me know) that need to be persisted using Core Data. There is no GUID data type in Core Data. What's the preferred type? It looks like CFUUID objects easily convert to and from a string, so I'm thinking that. Another option might be Binary Data.
Another consideration is that these GUIDs will going into and coming out of JSON objects (using built-in iOS5 JSON Serialization via NSJSONSerialization).
If I were doing it, I would just store it as a string.
You could store it as binary data, but since it's going to be going into and coming out of a string (which is what JSON is represented with) it's probably easier to store it as a string and not worry about converting it backwards and forwards.
I want to write object to file. But as per my study I found that it is not possible without object serialization. Other way is to convert object to array of bytes and then to write to file. But for this also object serialization is required. Is there any other way to write object to file? Because I want to use the same code in android and blackberry also. Please help me, I want to solve this problem as early as possible. Thanks in advance.
If you want to write an object to a file, then you by definition want to serialize it - that's what serialization means. If you're just looking for a way to save data to a file that doesn't rely on device-specific storage mechanisms, then you will need to write some custom serialization code. For every class that you want to store in a file, you'll need to do the following:
Write a method that stores the current state of the object in some writable structure, such as a string or byte array
Write a method that converts the string or byte array back into an object (it will probably be easiest to have this method take a stream as a parameter, and have it create a new object based on the data in that stream)
For example, you could save objects as XML or JSON strings, or in more efficient means - the best way to store it mainly depends on what sort of data you are storing and what you need to do with it.
It is good practice when doing this to include some version number that defines what version of the class you are using, and include this first when serializing the object. That way, when you deserialize, you can check that version number and know how it was serialized. This makes it easy to change your serialization scheme later on while still maintaining backwards compatibility with older files.
If you are just looking for a general purpose storage mechanism that is device-independent, then you could also look into using a SQLite database - they are supported on most if not all modern mobile operating systems. This will be easier than hand-rolling your own serialization, and will also generally have better performance.
There's this Transformable data type for attributes. What is it good for? Are there good examples?
I'd like to play around with this. So after searching a while I came across this: NSValueTransformer. Seems to be something I need for this.
So how would I get started with this? For example, if I wanted to store an UIColor object, would I make an transformer for that?
What exactly is this thing transforming to or from? An NSData? And must the object which I pass to the transformer follow any protocol?
Transformable attributes are useful for storing nonstandard object types within Core Data. For example, I provide code in this answer that lets you store UIImages as an attribute within Core Data. The image data is converted to and from an NSData instance that contains the image's PNG representation. This is all handled transparently for you by a custom NSValueTransformer.
You may also wish to encrypt individual attributes within your Core Data model, as I describe here. Using a transformable attribute for this makes this trivial to code.