working with a very large JSON object in iOS - ios

I am working with a huge JSON object and i need to extract a single parameter from it.
Is there a way to query the JSON object for the parameter?

You need a streaming JSON parser for that, i.e. a parser that produces events to which you listen as it goes through the JSON input, as opposed to document-based parsers, such as NSJSONSerialization of iOS 5+.
One of such parsers is YAJL: although it is a C library, you can use it from Objective C as well: all you need to do is defining a yajl_callbacks, put pointers to the handlers for the type of the item that you wish to extract, call the parser, and let the parser do the rest.

Related

How to parse a JSON file line by line in objective c

I am working with very large JSON files, so I do not want to read the entire file and then iterate and parse each data entry.
Instead, I would like to iterate on the JSON file itself (for example: line-by-line/one object at a time).
I thought about holding the next line location as part of the current line data, so the JSON is a semi linked list, but I did not manage to extract a specific line from the JSON file.
Am I missing an easier way to achieve that? Is it even possible to extract and parse a specific line from a JSON file?
Thanks a lot!
JSON is not a line oriented format, so the idea of parsing "line by line" doesn't really make sense.
That said, there is at least one event-driven JSON parser for iOS that I know of, https://github.com/stig/json-framework. The built-in parser NSJSONSerialization only works on entire files.

XML parsing as pagination

I have an XML file which i get downloaded from server which is having 50k elements. I need to display those 50k elements in a tableView.
But It consumes more memory.
So i thought is there any XML parser available in swift which allows me kind of pagination like parse 1 to 10 next 10-20 and so on.
All u need is a SAX xml parser like libxml2. DOM parser will not be able to parse the data with 50K elements because DOM parsers loads the entire Document Object Model into memory to construct the tree and then parses the nodes. Where as SAX parsers parses the xml in chunk.
Unfortunately most of the SAX parsers I am aware of are in C. So u have to write the wrapper around them to use it swift project. Good news there are tutorials explaining how to use them.
here are few of the useful links to integrate libxml2 to swift project.
http://redqueencoder.com/wrapping-libxml2-for-swift/
https://www.cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html
EDIT:
You can make use of NSXMLParser as well which is a SAX parser written in Objective-C. You can find loads of tutorials on how to use it with Swift
https://medium.com/#lucascerro/understanding-nsxmlparser-in-swift-xcode-6-3-1-7c96ff6c65bc

Should I use one XML Parser for every XML feed I have, or should I write a parser for each XML feed I have?

So I help write an app for my university and I'm wondering what's the best way to handle multiple XML feeds (like scores for sports, class information, etc.).
Should I have one XML parser that can handle all feeds? Or should I write a parser for each feed? We're having trouble deciding the best way to implement it.
This is iOS and we use a mix of Swift 3 and Objective-C
I think the right strategy is to write a base class that handles common data types like integers, booleans, strings, etc., and then write derived classes for each type of feed. This is the strategy I use in my XML parser, which is based on the data structures and Apple's XML parser as described here:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/NSXML_Concepts/NSXML.html
Personally I prefer to use the XPath data models where you can query the XML tree for a specific node using a path-like string.

NSXMLParser vs JSON Parser

What is the pros/cons of NSXMLParser & JSON parser?
Which one is good in which scenario?
Currently, my app uses NSXMLParser. I'm planning to move JSON parser if it is more efficient.
Thanks
NSXMLParser is an "event driven" parser which basically notifies a delegate about the occurrence of certain elements in the XML document.
Event driven parsers do not create a representation of the XML document by itself. The actual processing of the elements has to be done by some delegate. Properly utilizing event driven parsers is elaborate and error prone and requires experience how to approach such a task. Well, you know it.
NSJSONSerialization on the other hand, and all other third party JSON parsers that I know of, create a foundation object (a NSArray or NSDictionary) from the JSON input. Parsing a JSON document and getting a NSDictionary or a NSArray object back is a matter of one statement. A few also support the "event driven" mode.
XML is far more complex than JSON. Inherently, a JSON parser is much more simpler and also almost always more efficient in parsing documents.
Despite it's simplicity, JSON is almost always sufficient to express your data.
So, when you can express your data in JSON, by any means, use JSON. If possible, use NSJSONSerialization.
Other third party JSON parsers may offer additionally features, like an event driven API, an improved way to handle chunks of data, have more sophisticated options to customize certain edge cases, like the handling of Unicode NULL character, Unicode noncharacters, how to convert JSON numbers, etc., and may be possibly faster than NSJSONSerialization.
Today, NSJSONSerialization is about as fast as JSONKit. (For some input, JSONKit is a bit faster). AFAIK, there are two third party parsers which are for any input almost always faster than NSJSONSerialization, especially on arm, and when it comes to convert Numbers. You can expect them to be faster for a factor in the range of 1 to 2. But consider parsing JSON is almost never the culprit for performance issues.

Adobe Alchemy returning C data structures

I have lexer/parser (Generated from an ANTLR grammar file) which (for performance reasons) I have compiled to C code which will be included into my actionscript project using Adobe Alchemly.
The parser will generate an abstract syntax tree (In C) from an input string (passed from Actionscript) - I wish to return the C AST back into actionscript for further processing. How can I convert the tree structure of the AST to a format which I can return to actionscript?
Thanks,
Unfortunately you can't just send a C data structure across. You've got three options, in increasing order of madness:
Serialize the data on the C side and reconstitute it on the AS3 side.
Pack up the data into Objects and return those.
Pass a pointer and size back to AS3 and pull out the data from Alchemy's ram ByteArray.
I only include #3 for completeness-- I think it would be crazy to try it for any kind of complex data structure. The code would be fragile. Following pointers would be clunky. Bleah.
For #2 you could use dynamic Objects (via AS3_Object) or concrete ones (via AS3_Get, AS3_New). This is fairly complex code also and not so fast. Can be hard to maintain.
For #1, the type of serialization is what matters. You could have your C code render the structures to a binary 'file', return that, and have your AS3 parse the file format via ByteArray. Or you could render it to XML and have AS3's XML class parse it. This has the benefit of being fairly fast (since XML is implemented natively), at least on the de-serialization end. If you have a fast XML renderer on the C side (or, ahem, sprintfs), its not so bad.

Resources