📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
GSON Maven Dependency
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
Serialize Primitives Types
// Serialization
Gson gson = new Gson();
// Serialization of integer
System.out.println(gson.toJson(1));
// Serialization of String
System.out.println(gson.toJson("abcd"));
// Serialization of Long
System.out.println(gson.toJson(new Long(10)));
// Serialization of int array
int[] values = { 1 };
System.out.println(gson.toJson(values));
// Serialization of Double
System.out.println(gson.toJson(new Double(10.0d)));
// Serialization of Character
System.out.println(gson.toJson(new Character('A')));
Deserialize Primitives Types
// Deserialization of int
System.out.println(gson.fromJson("1", int.class));
// Deserialization of Integer
System.out.println(gson.fromJson("1", Integer.class));
// Deserialization of Long
System.out.println(gson.fromJson("1", Long.class));
// Deserialization of Boolean
System.out.println(gson.fromJson("false", Boolean.class));
// Deserialization of String
System.out.println(gson.fromJson("\"abc\"", String.class));
// Deserialization of String[]
System.out.println(gson.fromJson("[\"abc\"]", String[].class));
// Deserialization of Character
System.out.println(gson.fromJson("A", Character.class));
Complete Java program to Serialize and Deserialize Primitives Types using GSON
package net.javaguides.gson;
import com.google.gson.Gson;
public class GSONPrimitiveExamples {
public static void main(String[] args) {
// Serialization
Gson gson = new Gson();
// Serialization of integer
System.out.println(gson.toJson(1));
// Serialization of String
System.out.println(gson.toJson("abcd"));
// Serialization of Long
System.out.println(gson.toJson(new Long(10)));
// Serialization of int array
int[] values = {
1
};
System.out.println(gson.toJson(values));
// Serialization of Double
System.out.println(gson.toJson(new Double(10.0 d)));
// Serialization of Character
System.out.println(gson.toJson(new Character('A')));
// Deserialization of int
System.out.println(gson.fromJson("1", int.class));
// Deserialization of Integer
System.out.println(gson.fromJson("1", Integer.class));
// Deserialization of Long
System.out.println(gson.fromJson("1", Long.class));
// Deserialization of Boolean
System.out.println(gson.fromJson("false", Boolean.class));
// Deserialization of String
System.out.println(gson.fromJson("\"abc\"", String.class));
// Deserialization of String[]
System.out.println(gson.fromJson("[\"abc\"]", String[].class));
// Deserialization of Character
System.out.println(gson.fromJson("A", Character.class));
}
}
1
"abcd"
10
[1]
10.0
"A"
1
1
1
false
abc
[Ljava.lang.String;@2b71fc7e
A
Comments
Post a Comment
Leave Comment