Json stands for Java script Object Notation and Gson is like json which is developed bye Google. Jackson is also one type of Json which works similar to json. in this slide i am showing The performance comparison between Json, Gson and Jackson.
2. Topics to cover
Overview of JSON
The working of GSON
The working of JACKSON
Comparison of JSON,GSON and JACKSON.
Conclusion
2JSON vs GSON vs JACKSON06/21/16
3. What is JSON
JSON is a lightweight data interchange format .
The most important aspects of JSON are
Simplicity
Extensibility
Interoperability
Openness and Human readability.
3JSON vs GSON vs JACKSON06/21/16
4. The working of GSON
GSON is an Java library to serialize and deserialize Java
objects to (and from) JSON.
It provides two methods :-
Gson.toJson to serialize java objects.
Gson.fromJson to deserialize json objects.
4JSON vs GSON vs JACKSON06/21/16
5. GSON Example
Serialization:-
Gson gson = new Gson();
Car audi = new Car("Audi", "A4", 1.8, false);
Car skoda = new Car(“Skoda", "Octavia", 2.0, true);
Car[] cars = {audi, skoda};
Person johnDoe = new Person("John", "Doe", 245987453, 35,
cars);
System.out.println(gson.toJson(johnDoe));
5JSON vs GSON vs JACKSON06/21/16
6. GSON Example
Deserialization:-
Gson gson = new Gson();
String json = "{"name":"John","surname":"Doe","cars":
[{"manufacturer":"Audi","model":"A4","capacity":1.8,"a
ccident":false},
{"manufacturer":"Škoda","model":"Octavia","capacity":2.
0,"accident":true}], "phone":245987453}";
Person johnDoe = gson.fromJson(json, Person.class);
System.out.println(johnDoe.toString());
6JSON vs GSON vs JACKSON06/21/16
7. The working of JACKSON
It is a Java library for processing JSON.
Jackson aims to be the best possible combination of fast,
correct, lightweight, and friendly for developers.
Jackson offers three alternative methods:-
Stream API
Tree Model
Data Binding
7JSON vs GSON vs JACKSON06/21/16
8. JACKSON Example- Data Binding
Read the Values from JSON file:-
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new File("user.json"), User.class);
Write the values To the JSON file:-
mapper.writeValue(new File("user-modified.json"), user);
8JSON vs GSON vs JACKSON06/21/16
9. JACKSON Example- Tree Model
Tree Model:-
ObjectMapper mapper = new ObjectMapper();
ArrayNode arrayNode = mapper.createArrayNode();
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("Firstname", student.getFirstName());
objectNode.put("Lastname",student.getLastName());
objectNode.put("age", student.getAge());
objectNode.put("address", student.getAddress());
objectNode.put("studentId", student.getStudentId());
arrayNode.add(objectNode);
9JSON vs GSON vs JACKSON06/21/16
10. JACKSON Example- Stream API
Writing to File:-
JsonFactory f = new JsonFactory();
JsonGenerator g =f.createJsonGenerator(new File("user.json"));
g.writeStartObject();
g.writeStartArray();
g.writeEndArray();
g.writeEndObject();
g.close();
06/21/16 JSON vs GSON vs JACKSON 10
11. JACKSON Example- Stream API
JsonFactory jfactory = new JsonFactory();
JsonParser jParser = jfactory.createJsonParser(new File("c://temp/user.json"));
while (jParser.nextToken() != JsonToken.END_OBJECT) {
if ("name".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getText()); }
if ("age".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getIntValue()); }
if ("messages".equals(fieldname)) {
jParser.nextToken();
while (jParser.nextToken() != JsonToken.END_ARRAY) {
System.out.println(jParser.getText()); }}
06/21/16 JSON vs GSON vs JACKSON 11
14. CONCLUSION
If you are dealing with big JSON files, then Jackson is your
library of interest.
If you are dealing with with lots of small JSON requests then
GSON is your library of interest.
If you end up having to often deal with both types of files,
then JSON.simple. Neither Jackson nor GSON perform as
well across multiple files sizes.
14JSON vs GSON vs JACKSON06/21/16