In my ongoing internship opportunity, I came across the use of JSON Strings. This was something I hadn't really paid much attention to before. My mentor told me that it would be a good idea to investigate various JSON Libraries, and decide which would be good to use.
He personally recommended 3 Libraries: GSON, Jackson, and
Json.org java library.
I pretty much learnt how to implement the basics of the three in three short days. So if you want to tell me how to improve the tests, feel free to do so.
System Configuration:
Processor: Intel® Core™ i5-2450M CPU @ 2.50GHz × 4
Software: JDK 1.7.0, Eclipse Luna 4.4.1on Kali Linux 1.0.8
The JSON file used is medium.json available in the source directory at Github project page.
The JVM was heated up using 1000 iterations of the program, then from thereon counted the time taken for the next 500 iterations. Then an average of these 500 was taken to give the resultant time of serializing and deserializing.
So here goes, these are for a medium length JSON file that has upto 3 level indentation:
GSON:(211 kB)
Pretty much easy to set up, one single library does it all.
Creation time:0.03006 ms
POJO:Serializing size:1342
Deserializing size:1312
Deserializing time:0.04008 ms
Serializing time:0.03206 ms
String:Serializing size:2412
Deserializing size:2189
Deserializing time:0.04409 ms
Serializing time:0.03206 ms
Jackson: (1.3 MB)
Easy to set up, though needs 3 separate libraries to really work. Added afterburner to get faster speed(optional) - another 136 kB
Creation time:0.03206 ms
POJO:Serializing size:1796
Deserializing size:1796
Deserializing time:0.49298 ms
Serializing time:0.014028 ms
String:Serializing size:2382
Deserializing size:2189
Deserializing time:0.05811 ms
Serializing time:0.05210 ms
Org.Json java lib: (159 kB)
Quite difficult to manage, and external dependencies are hard to set up. It requires POJO to be set up before parsing, but that's neither here, neither there.
Creation size:0.00601 ms
POJO:Serializing size:1302
Deserializing size:1758
Deserializing time:0.26853 ms
Serializing time:0.47695 ms
POJO:
From the above, it is clear that if you are after speed, GSON and Jackson are the faster libraries. Jackson has been said to contain a lot of features, though not in the know about the comparison of features right now. While size consumption is much better in the GSON library.
I won't be using Org.Json java library much as it doesn't have many features as it is a small and basic implementation and would probably lead to tearing your hair out. You probably will have to re-implement everything, so would be quite detrimental to your project.
Deserializing time for Jackson is much higher as we are writing to a string, and it takes quite an amount of time. I have learnt that writing to a file takes less time, still have to try that out though.
String:
From the above, it is clear that if you are after speed, GSON and
Jackson are the faster libraries. Jackson has been said to contain a lot
of features, though not in the know about the comparison of features
right now. While size consumption is a tad better in Jackson library.
I couldn't get Org.Json to work without using POJO, so can't comment on that, but the difficulty shies me away from using it much.
Here Gson turns out to be a tad faster than Jackson.
At the end it is a close call, but my vote shall go for use of Jackson due to its much more developed function base and time factor in POJOs, as mostly we do know the type of JSON sent.
Find the work and contribute at:
https://github.com/th3dark0n3/json_benchmark