json vs gson vs jackson

Post on 15-Apr-2017

4.593 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JSON vs GSON vs JACKSON

- Vinaykumar Hebballi

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

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

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

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

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

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

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

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

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

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

COMPARISON-Big File

12JSON vs GSON vs JACKSON05/03/23

COMPARISON-Small File

13JSON vs GSON vs JACKSON05/03/23

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

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

Thank you

top related