English 中文(简体)
JSON: Jackson stream parser - is it really worth it? [closed]
原标题:

I m making pretty heavy use of JSON parsing in an app I m writing. Most of what I have done is already implemented using Android s built in JSONObject library (is it json-lib?).

JSONObject appears to create instances of absolutely everything in the JSON string... even if I don t end up using all of them.

My app currently runs pretty well, even on a G1.

My question is this: are the speed and memory benefits from using a stream parser like Jackson worth all the trouble?

By trouble, I mean this: As far as I can tell, there are three downsides to using Jackson instead of the built in library:

  1. Dependency on an external library. This makes your .apk bigger in the end. Not a huge deal.
  2. Your app is more fragile. Since the parsing is not done automatically, it is more vulnerable to changes in the JSON text that it s parsing (perhaps I m wrong about this).
  3. Writing code to parse JSON via a stream parser is ugly and tedious.
最佳回答

Guess you ve pretty much answered your own question. :)
Using the built-in JSON parser myself and have never looked for an alternative.

EDIT: Now I m using a thin annotation-based wrapper from DroidParts.

问题回答

I am also using the build-in JSON parser in most cases, but recently stepped into a scenario where it doesn t fit: For some web service requests I receive JSON documents of more than 1 MB. Loading these with the build-in JSON parser requires tremendous amounts of main memory and resulted in OutOfMemoryException several times. For these scenarios a streaming parser is the better choice (even though it is more inconvenient in use) and the built-in JSON parser does not provide streaming, but only the DOM-like style.

For anyone looking for a streaming JSON parser for Android I can strongly recommend to use Google s GSON. I ve tried Jackson JSON at first and it worked fine until I tried to build the release version of my app: ProGuard reported several issues and the running app crashed with mysterious NullPointerException in the constructor of Jackson s ObjectMapper (though everything worked fine in the debug version). Even after a few hours of trying around I wasn t able to fix this. I then switched to GSON then and everything worked like a charm.

BTW: The GSON streaming-only jar has a size of only 14kB -- so nothing to really worry about.

I think question is whether built-in one is good enough. If it is, sure, minimizing dependencies is often a good strategy. Good enough can refer to both efficiency and ease of use.

For what it s worth, Jackson also has a decent tree model as well as full data binding. Tree model is significantly faster than default parser (parsing is 3x-5x faster on J2SE, probably similarly on other platforms like Android, tree model itself is probably more efficient as well). Or: if you don t want dependency to second jar (mapper is needed for tree model and data binding), writing your own tree to cover your use cases is simple too. Either use basic HashMap/ArrayList/wrappers, or have your own classes if you prefer. Builder would be maybe 40 lines of code top.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签