Top Banner
JSON vs GSON vs JACKSON - Vinaykumar Hebballi
16

Json vs Gson vs Jackson

Apr 15, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Json vs Gson vs Jackson

JSON vs GSON vs JACKSON

- Vinaykumar Hebballi

Page 2: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 3: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 4: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 5: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 6: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 7: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 8: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 9: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 10: Json vs Gson vs Jackson

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();

05/03/23 JSON vs GSON vs JACKSON 10

Page 11: Json vs Gson vs Jackson

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()); }}

05/03/23 JSON vs GSON vs JACKSON 11

Page 12: Json vs Gson vs Jackson

COMPARISON-Big File

12JSON vs GSON vs JACKSON05/03/23

Page 13: Json vs Gson vs Jackson

COMPARISON-Small File

13JSON vs GSON vs JACKSON05/03/23

Page 14: Json vs Gson vs Jackson

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 JACKSON05/03/23

Page 15: Json vs Gson vs Jackson

References:

www.json.org

http://wiki.fasterxml.com/JacksonHome

https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html

15JSON vs GSON vs JACKSON05/03/23

Page 16: Json vs Gson vs Jackson

Thank you