Gson TypeAdapters for Java 8 LocalDate/Time Classes
Learn to fix the InaccessibleObjectException or “Failed making field ‘java.time.LocalDate#year’ accessible” type errors when serializing or deserializing the new Java 8 Date time classes.
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.
Learn to fix the InaccessibleObjectException or “Failed making field ‘java.time.LocalDate#year’ accessible” type errors when serializing or deserializing the new Java 8 Date time classes.
Learn to use custom serialization and deserialization using two interfaces Gson JsonSerializer and JsonDeserializer to convert between json and java object.
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 allows us to exclude or ignore the fields from Java classes using @Expose annotation, exclude fields with modifiers and custom exclusion strategies.
In Gson, multiple versions of the same object can be maintained by using @Since annotation. It takes single parameter – ignoreVersionsAfter.
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 …
All modern applications generally fetch data from remote services (e.g. REST or SOAP) that are mostly either XML or JSON format. Gson helps applications in Java-JSON serialization and deserialization automatically as well as manually, if needed, using simple toJson() and fromJson() methods. 1. Maven Refer to the latest version of …
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 …
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 …
To customize the behavior of Gson, use GsonBuilder to create a new customized Gson instance. e.g. pretty printing, FieldNamingPolicy, ExclusionStrategy etc.
Gson @SerializedName is used to change the name of fields between JSON and Java objects while the serialization and deserialization process.
Learn to use Google GSON library to deserialize or parse JSON to Set (e.g. HashSet) in java. Also, learn to serialize Set to JSON.
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 …
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 …
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.