java Android JSON 库的性能和可用性比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7935078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Performance and Usability comparison of Android JSON libraries
提问by Kurtis Nusbaum
What is the best library out there for parsing JSON on android. I know the Android framework has a JSON library built in, and I've heard of GSON. But I'm wondering if anyone has compared the various JSON options on android and come up with a justification for choosing one over the other. I'm thinking of performance and usability as the main criteria.
在 android 上解析 JSON 的最佳库是什么?我知道 Android 框架有一个内置的 JSON 库,我听说过 GSON。但我想知道是否有人比较了 android 上的各种 JSON 选项,并提出了选择其中一个选项的理由。我认为性能和可用性是主要标准。
回答by Programmer Bruce
While there is a handful of Java-to/from-JSON APIs that provide for easy (basic) binding between JSON and an arbitrary Java data structure, Hymanson and Gson are by far the most sophisticated (capable) and feature-rich options.
虽然有少数 Java 到/来自 JSON 的 API 提供了 JSON 和任意 Java 数据结构之间的简单(基本)绑定,但 Hymanson 和 Gson 是迄今为止最复杂(功能强大)和功能最丰富的选项。
Back in July (2011), I posted a multi-part series on my blog comparing using Gson versus Hymanson, stepping through features as they were organized in the Gson User Guide, including examples of comparable implementations in Hymanson (and of implementations in Gson, where the Gson User Guide was incomplete or broken).
早在 2011 年 7 月,我在我的博客上发布了一个多部分系列,比较使用 Gson 和 Hymanson,逐步介绍 Gson 用户指南中组织的功能,包括 Hymanson 中可比较的实现示例(以及 Gson 中的实现, Gson 用户指南不完整或损坏的地方)。
The sixth part of the series provides easy navigation links for the comparison, as well as a summary comparison listing. http://programmerbruce.blogspot.com/2011/07/gson-v-Hymanson-part-6.html
本系列的第六部分提供了用于比较的简单导航链接,以及总结比较列表。 http://programmerbruce.blogspot.com/2011/07/gson-v-Hymanson-part-6.html
This is the most comprehensive comparison of using Gson versus Hymanson for common JSON-to/from-Java tasks known.
这是将 Gson 与 Hymanson 用于已知的常见 JSON-to/from-Java 任务的最全面的比较。
As mentioned, performance comparisons of various Java-to/from-JSON APIs are available at https://github.com/eishay/jvm-serializers/wiki. The current release of Gson (2.0) shows to be about 16 times slower than the current release of Hymanson (1.9.2) at serializing and deserializing a 500 byte JSON structure, using very comparable implementation efforts (just one or two lines of code).
如前所述,各种 Java-to/from-JSON API 的性能比较可在https://github.com/eishay/jvm-serializers/wiki 上获得。在序列化和反序列化 500 字节 JSON 结构方面,Gson (2.0) 的当前版本显示比 Hymanson (1.9.2) 的当前版本慢 16 倍,使用非常相似的实现工作(只需一两行代码) .
Martin Adamek posted some performance results of various APIs running on Android at http://martinadamek.com/2011/01/31/comparison-of-json-parsers-performance-on-android/and http://martinadamek.com/2011/02/04/json-parsers-performance-on-android-with-warmup-and-multiple-iterations/
Martin Adamek 在http://martinadamek.com/2011/01/31/comparison-of-json-parsers-performance-on-android/和http://martinadamek.com/上发布了在 Android 上运行的各种 API 的一些性能结果2011/02/04/json-parsers-performance-on-android-with-warmup-and-multiple-iterations/
回答by kabuko
GSON is really simple to use and if performance isn't an issue, it's quite nice. Hymansonis much, much fasterthough and is not really that much more effort to use. As I've said in the pastin another related SO question, I've gotten huge performance gains (when working with large sets of JSON) by simply switching to Hymanson from GSON.
GSON 使用起来真的很简单,如果性能不是问题,那就太好了。Hyman逊是多少,要快得多,但并是不是真的那么更多的努力来使用。正如我过去在另一个相关的 SO 问题中所说的那样,通过简单地从 GSON 切换到 Hymanson,我已经获得了巨大的性能提升(在使用大量 JSON 时)。
回答by ThomasRS
This answer is seen from a memory perspective, as that can also be considered performance :-P.
这个答案是从内存的角度来看的,因为这也可以被视为性能:-P。
I just implemented parsing of a 70KB JSON file in Android, parsing an array of objects which should either load from scratch or update previously present data binding objects, all over HTTP.
我刚刚在 Android 中实现了一个 70KB JSON 文件的解析,解析了一个对象数组,这些对象要么从头开始加载,要么更新以前存在的数据绑定对象,全部通过 HTTP。
The biggest drawback with the built-in org.json packagefor android runtime 3 was
android 运行时 3的内置 org.json 包的最大缺点是
- the JSONTokener constructor only accepts a String, no Reader
- the lack of practical support for pull-parsing
- JSONTokener 构造函数只接受一个字符串,没有 Reader
- 缺乏对拉式解析的实际支持
This typically means you need to keep the whole file in memory (think 2x byte size) and in addition hold all the resulting JSON objects in memory at the same time, before you start making your data binding. So worst case is 2x file size, JSON objects and data-binding objects - usually at least 2x the memory requirement.
这通常意味着在开始进行数据绑定之前,您需要将整个文件保存在内存中(想想 2 倍字节大小),并且同时将所有生成的 JSON 对象保存在内存中。所以最坏的情况是 2 倍的文件大小、JSON 对象和数据绑定对象——通常至少是内存需求的 2 倍。
If you pull-parse from a reader, you can get this down to 1x. That is the most important thing you can do from memory perspective.
如果您从阅读器中提取解析,您可以将其降低到 1 倍。从记忆的角度来看,这是您可以做的最重要的事情。
And, surprise surprise, if you go with some more modern classes, like Hymanson or just the latest sources from org.json, you will be able to get around both these constraints without a problem, a later android runtime also seems to have some utility classes for JSON pull-parsing.
而且,出人意料的是,如果你使用一些更现代的类,比如 Hymanson 或者只是来自 org.json 的最新源,你将能够毫无问题地绕过这两个限制,后来的 android 运行时似乎也有一些实用程序用于 JSON 拉解析的类。
If you are stuck with an old runtime, and want to keep the footprint of the application down, as did I, you can copy the JSONTokener from org.json and modify your top level parse loop (in my case the array parse loop) and do data binding on each member of the array instead of the whole array at once. This way you reuse the JSON objects already in the android runtime as much as possible and still get the streaming effect (at the price of adding ids to each top-level object).
如果您坚持使用旧的运行时,并希望像我一样减少应用程序的占用空间,您可以从 org.json 复制 JSONTokener 并修改您的顶级解析循环(在我的情况下是数组解析循环)和一次对数组的每个成员而不是整个数组进行数据绑定。通过这种方式,您可以尽可能多地重用 android 运行时中已有的 JSON 对象,并且仍然可以获得流式效果(以向每个顶级对象添加 id 为代价)。
回答by Mike Marshall
Well sometimes performance and usability can be at odds, but I've found GSON to be easier to use than alternatives like Hymanson, org.jsonand others. I'm currently serializing JSON data that has objects and lists 3 or 4 levels deep. That being said, I have tailored my JSON to be more suitable for serialization, but overall GSON has been great.
好吧,有时性能和可用性可能会不一致,但我发现 GSON 比Hymanson、org.json等替代品更易于使用。我目前正在序列化具有对象并列出 3 或 4 级深度的 JSON 数据。话虽如此,我已经调整了我的 JSON 以使其更适合序列化,但总体上 GSON 已经很棒了。
回答by Fraggle
I'm testing out GSON and have tried some others.
我正在测试 GSON 并尝试了其他一些。
GSON does an excellent job of serializing (converting object to json) complex objects with basically no changes or thought at all on your part, but is a bit slow and memory intensive.
GSON 在序列化(将对象转换为 json)复杂对象方面做得非常出色,您基本上没有任何更改或想法,但有点慢且占用大量内存。
The GSON Roadmapwebsite indicates they expect version 2.0 to address some performance issues and that it will be out in Oct 2011 (ending soon). So I'm hoping they deliver that because I really need it.
GSON路线图网站表示,他们预计 2.0 版将解决一些性能问题,并将于 2011 年 10 月发布(即将结束)。所以我希望他们能做到这一点,因为我真的需要它。
Other libraries (sorry can't recall names right now) don't seem to serialize as well. Some may only look at public variables in your classes or just call on public methods that look like getters/setters. Gson doesn't do it that way, and will grab everything.
其他库(抱歉现在想不起来名字)似乎也没有序列化。有些人可能只查看类中的公共变量,或者只是调用看起来像 getter/setter 的公共方法。Gson 不会那样做,并且会抓住一切。
I haven't done much yet on the deserializing side (converting JSON back to Java objects).
我在反序列化方面还没有做太多(将 JSON 转换回 Java 对象)。
回答by Plastic Sturgeon
the Android JSON is very functional, but has not bells a whistles. Where as GSON allows you to specify mappings between your classes and their json representation. It also has very nice automatic conversion of any primitive values to json, with no additional work on your part.
Android JSON 非常实用,但没有任何问题。GSON 允许您指定类与其 json 表示之间的映射。它还具有非常好的将任何原始值自动转换为 json 的功能,您无需进行额外的工作。
The payoff with GSON is if you have a lot of JSON communcaion, or complex objects that require value checking to prevent illegal values (such as NaN) and other cases where java-to-JSON is less than straight forward.
使用 GSON 的好处是,如果您有大量 JSON 通信,或者需要检查值以防止非法值(例如 NaN)的复杂对象以及其他 java-to-JSON 不那么直接的情况。
But if you just need to send and recieve a simple json object, the native library does the trick quite respectably. I am using it in my current project to post high scores to a server.
但是如果你只需要发送和接收一个简单的 json 对象,本地库就可以很好地完成这个技巧。我在我当前的项目中使用它来将高分发布到服务器。
回答by Miracle
- Make asynchronous HTTP requests, handle responses in anonymous callbacks
- HTTP requests happen outside the UI thread
- Requests use a threadpool to cap concurrent resource usage
- GET/POST params builder (RequestParams)
- Multipart file uploads with no additional third party libraries
- Tiny size overhead to your application, only 25kb for everything
- Automatic smart request retries optimized for spotty mobile connections
- Automatic gzip response decoding support for super-fast requests
- Binary file (images etc) downloading with BinaryHttpResponseHandler
- Built-in response parsing into JSON with JsonHttpResponseHandler
- Persistent cookie store, saves cookies into your app's SharedPreferences
- 发出异步 HTTP 请求,处理匿名回调中的响应
- HTTP 请求发生在 UI 线程之外
- 请求使用线程池来限制并发资源使用
- GET/POST 参数构建器 (RequestParams)
- 分段文件上传,无需额外的第三方库
- 您的应用程序的开销很小,所有内容只有 25kb
- 针对不稳定的移动连接优化的自动智能请求重试
- 对超快速请求的自动 gzip 响应解码支持
- 使用 BinaryHttpResponseHandler 下载二进制文件(图像等)
- 使用 JsonHttpResponseHandler 内置响应解析为 JSON
- 持久性 cookie 存储,将 cookie 保存到您的应用程序的 SharedPreferences
and also you can try fastjsonwhich is a fast json processor.
你也可以尝试fastjson这是一个快速的 json 处理器。