开发者

JSON: Jackson stream parser - is it really worth it? [closed]

开发者 https://www.devze.com 2022-12-23 13:30 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

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 d开发者_如何学Goon'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.


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.


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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号