I'm using the org.json.* library for turning my web services' result (obviously json) into json objects. My problem is that the JSONObject and JSONArray constructors take a long time to build out the objects. I'm not passing a very large amount of data (anywhere between 1 and 100 array items with 3-5 keys each), but even with just 4 or 5 it takes a few seconds for the constructor to finish.
Is there a way to speed this up? Is there a faster library I could be using?
There's not a whole lot of code to show.
JSONArray arrayjson = new JSONArray(json);
Where json is a String.
You might give gson a try. This article seems to indicate that it has pretty good performance on Android vs the other alternatives. Jackson might be another good alternative.
According to the performance results at https://github.com/eishay/jvm-serializers/wiki, for serialization with databind with strings, e.g., gson.toJson(myObject), Gson is over 10X slower than Jackson. FastJSON beat Jackson at this same test by 2-3%.
Deserialization performance is similar, with Gson over 9x slower than Jackson, and FastJSON about 0.5% faster than Jackson.
Note: The current test results used Gson 1.6. With Gson 1.7.1, the databind performance improved 10-15% over Gson 1.6 (but the manual and manual/tree solutions showed no improvement). The results will hopefully be updated accordingly soon.
Related
What are differences between this two classes?
If someone uses Gson library is it preferable to use com.google.json.JsonObject over org.json.JSONObject?
Could anybody list pros and cons of these 2 choices?
Following are the main differences:
1) GSON can use the Object definition to directly create an object of the desired type. JSONObject needs to be parsed manually.
2) org.json is a simple, tree-style API. It's biggest weakness is that it requires you to load the entire JSON document into a string before you can parse it. For large JSON documents this may be inefficient.
3) By far the biggest weakness of the org.json implementation is JSONException. It's just not convenient to have to place a try/catch block around all of your JSON stuff.
4) Gson is the best API for JSON parsing on Android. It has a very small binary size (under 200 KiB), does fast databinding, and has a simple easy-to-use API.
5) GSON and Jackson are the most popular solutions for managing JSON data in the java world.
Many JSON implementations are available in the market and most of them are open source. Each one has specific advantages and disadvantages.
Google GSON
Jackson
org.json etc.
Google GSON click for official documents
Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
Allow pre-existing unmodifiable objects to be converted to and from JSON
Extensive support of Java Generics
Allow custom representations for objects
Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)
Jackson click for official documents
Streaming API or incremental parsing/generation: reads and writes JSON content as discrete events
Tree model: provides a mutable in-memory tree representation of a JSON document
Data binding: converts JSON to and from POJO’s
Some comparison blogs click here blogs1, blog2
I personally done a benchmark for serialization and deserialization using GSON vs Jackson vs Simple JSON
Very small object: Google gson performs faster than Jackson and Simple JSON
Large objects : Google gson performs faster than Jackson and Simple JSON
I did experiment with 456 Kb json file on real Pixel 3 device Android 11.
I need to create a backup file from database. Model has 3 objects with one-to-many relationships: Note, Item, Alarm.
Note -> fields and List<Item>.
Item -> fields and List<Alarm>.
Alarm -> fields
Serialization result:
gson -> 75ms; org.json -> 63ms
gson -> 83ms; org.json -> 67ms
gson -> 73ms; org.json -> 62ms
As you can see the default android org.json is faster than GSON.
If you have a time to create a mapping for your model, I recommend using the default org.json.
If you want to create json faster than gson, but more easy than org.json try using moshi or maybe kotlinx-serialization.
I'm working with an app that connects to a PHP/MySQL server from which everything is returned in JSON format. For example, a list of users is returned as a JSONArray of JSONObject. Each object contains the individual user's information (name, location, phone number, etc).
In working with information in this format is is more efficient to leave everything in JSON format and only extract items from the array/objects as needed? Or is it better to extract everything from the JSONArrayand included objects to build a regular Array or ArrayList first?
JSONArray internally uses ArrayList. It's just wrapper over ArrayList. So I'd say there is no difference. JSONObject uses HashMap so again no real drawbacks.
In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects
JSON is smaller than corresponding XML
JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)
JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse and JSON.stringify when not supported by browsers.
While eval at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.
JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.
While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.
I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.
I want to parse a large JSON. How can I make it faster? Now it takes a lot of time. Please help me.
you can use some wrappers like http://code.google.com/p/json-simple/ it is optimised to handle large data from json and easy to use.
Also you can give it try to http://code.google.com/p/json-simple/ for better performance.
Following are the others.
Android built-in: http://developer.android.com/reference/org/json/package-summary.html
JSON.simple: http://code.google.com/p/json-simple/
Jackson: http://jackson.codehaus.org/
Gson: http://code.google.com/p/google-gson/
You can check comparison article over here.
According to some benchmarks Jackson is the fastest.
Use jackson JSON processor http://jackson.codehaus.org/
Most of the time people end up parsing the whole JSON and ignoring most of the data. This makes the process slow. If you are going to discard a lot of data, parsing in SAX mode is much faster, since you can stop parsing as soon as you have found all the required data.
I am going to be consuming real time twitter feed and parsing it into objects for list view display. I need robust solution is default json lib good enough for this task or do I need to use Gson / jackson lib?
Check Gson and JackSon:
GSON
JACKSON
EDIT: I did not see you mention this libraries. Yes, you should use them. This are robust
libraries, I used them to consume Json webservices and no problem so far.
GSON/Jackson are just the libraries that allow you to parse json responses into objects or the other way round. However, the default implementation of json provided in android framework is capable enough of dealing any response string. The only drawback or turn-off is that you need to write extra code for simple things to get done.
Following are some links with bench marking of JSON libs...
http://blog.novoj.net/2012/02/05/json-java-parsers-generators-microbenchmark/
http://danielywoo.blogspot.in/2011/04/json-java-libraries-benchmark-jsonlib.html
Jackson Vs. Gson
And from the benchmark it looks like Jackson is the best and the fastest library for the JSON parsing...
I have a large json file (~3.5 MB), with (~140) complex objects, and the max depth in the object graph is about 4-5. I use Gson to parse it, but it's really slow. I've tried some way to parse it (like mixed parsing or using stream to parse), but I couldn't increase performance.
I checked Memory Analizer, it kill memory (70-80%), if I only parse the base Id of the objects. While parsing there are 400-500k object in memory (mostly string and char).
Would be parsing more efficient if the object graph wouldn't be so deep? Do you have a good idea how could be better? I tried other libs too (like Jackson), but performance wasn't better.
I had some good performance boost by using Jackson - you should not load whole json into memory but use createJsonParser(Reader r). If it does not work for you then either try spliting data and use separate threads - it makes sense if CPU is not fully loaded by single threaded version. Otherwise try using JNI json parsing implementation.