Gson Basics

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 …

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.