Gson

Gson (pronounced as “gee-son”) is a Java library developed by Google for serializing and deserializing Java objects to and from JSON (JavaScript Object Notation) format.

JSON is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate.

Gson simplifies the process of converting Java objects into JSON and vice versa, making it a popular choice for working with JSON data in Java applications.

Related Tags

Tutorials

Gson JsonParser

Gson JsonParser is used to parse Json data into a parse tree of JsonElement and thus JsonObject, which can be used to get JSON values using their keys.

Gson – Exclude or Ignore Fields

Gson allows us to exclude or ignore the fields from Java classes using @Expose annotation, exclude fields with modifiers and custom exclusion strategies.

Gson JsonReader

Learn to work with Gson JsonReader class which is a pull-based streaming JSON parser. It helps in reading a JSON as a stream of tokens. 1. JsonReader Read More : Streaming parser of XML 2. Tokens In streaming mode, every JSON data is considered an individual token. When we use …

Gson Tutorial: Java JSON toJson and fromJson Examples

Learn Gson with examples. Convert Java objects, lists and maps to JSON and back with toJson() and fromJson(), configure GsonBuilder, use @SerializedName and @Expose, read JSON with JsonParser and write a custom TypeAdapter.

Gson – Serialize Null Values

The default behavior in Gson is that null fields are ignored. For example, if in Employee object, we do not specify the email (i.e. email is null) then the email will not be part of serialized JSON output. Gson ignores null fields because this behavior allows a more compact JSON …

Gson – Pretty Print JSON

By default, Gson prints the JSON in a compact format. It means there will not be any whitespace in between field names and their value, object fields, and objects within arrays in the JSON output etc. 1. GsonBuilder.setPrettyPrinting() To enable the Gson Pretty Print feature, we must configure the Gson …

Gson – Parse JSON Array to Java Array or List

Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists. 1. Parsing JSON Array as Root To parse JSON, with array as root, we …

Gson – Serialize and Deserialize a Map

Learn to serialize HashMap using Google Gson library. Also, learn to deserialize JSON string to HashMap containing custom Objects using Gson such that field values are copied into appropriate generic types. These conversions can be used to create deep cloning of the HashMap. 1. Maven Include the latest version of …

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.