JSON Parsers - GitKaran/Sample-Boot GitHub Wiki

Jackson

Jackson is a Java JSON API which provides several different ways to work with JSON. Jackson is one of the most popular Java JSON APIs out there. You can find Jackson here:

https://github.com/FasterXML/jackson

Jackson contains 2 different JSON parsers:

  • The Jackson ObjectMapper which parses JSON into custom Java objects, or into a Jackson specific tree structure (tree model).
  • The Jackson JsonParser which is Jackson's JSON pull parser, parsing JSON one token at a time.

Jackson also contains two JSON generators:

  • The Jackson ObjectMapper which can generate JSON from custom Java objects, or from a Jackson specific tree structure (tree model).
  • The Jackson JsonGenerator which can generate JSON one token at a time.

GSON

GSON is a Java JSON API from Google. That is where the G in GSON comes from. GSON is reasonably flexible, but in some benchmark, Jackson was faster than GSON. Which you choose to use is up to you. You can find GSON here:

https://github.com/google/gson

GSON contains 3 Java JSON parsers:

  • The Gson class which can parse JSON into custom Java objects.
  • The GSON JsonReader which is GSON's pull JSON parser - parsing JSON one token at a time.
  • The GSON JsonParser which can parse JSON into a tree structure of GSON specific Java objects.

GSON also contains a JSON generator:

  • The Gson class which can generate JSON from custom Java classes.

Let's see Jackson in more detail -

The Jackson ObjectMapper class (com.fasterxml.jackson.databind.ObjectMapper) is the simplest way to parse JSON with Jackson.

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON.

The Jackson ObjectMapper can also create JSON from Java objects. Generating JSON from Java objects is also referred to as to serialize Java objects into JSON.

The Jackson Object mapper can parse JSON into objects of classes developed by you, or into objects of the built-in JSON tree model.

By the way, the reason it is called ObjectMapper is because it maps JSON into Java Objects (deserialization), or Java Objects into JSON (serialization).

Read JSON Object

  • From JSON String -
    ObjectMapper objectMapper = new ObjectMapper();
    String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
    Car car = objectMapper.readValue(carJson, Car.class);
  • From Reader -
Reader reader = new StringReader(carJson);
Car car = objectMapper.readValue(reader, Car.class);
  • Ignore Unknown JSON Fields -
objectMapper.configure(
    DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  • Fail on Null JSON Values for Primitive Types -
ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(
    DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);

Write JSON From Objects

The Jackson ObjectMapper can also be used to generate JSON from an object. You do so using the one of the methods:

  • writeValue()
  • writeValueAsString()
  • writeValueAsBytes()
ObjectMapper objectMapper = new ObjectMapper();

Car car = new Car();
car.brand = "BMW";
car.doors = 4;

String json = objectMapper.writeValueAsString(car);
System.out.println(json);

The JSON output from this example would be:

{"brand":"BMW","doors":4}`