人気の面接質問と回答・オンラインテスト
面接対策、オンラインテスト、チュートリアル、ライブ練習のための学習プラットフォーム

集中型学習パス、模擬テスト、面接向けコンテンツでスキルを伸ばしましょう。

WithoutBook は、分野別の面接質問、オンライン練習テスト、チュートリアル、比較ガイドをひとつのレスポンシブな学習空間にまとめています。

面接準備
ホーム / 面接科目 / Google Gson
WithoutBook LIVE 模擬面接 Google Gson 関連する面接科目: 39

Interview Questions and Answers

Google Gson の人気面接質問と回答を確認し、新卒者や経験者が就職面接の準備を進められます。

合計 8 問 Interview Questions and Answers

面接前に確認しておきたい最高の LIVE 模擬面接

Google Gson の人気面接質問と回答を確認し、新卒者や経験者が就職面接の準備を進められます。

Interview Questions and Answers

質問を検索して回答を確認できます。

経験者 / エキスパート向けの質問と回答

質問 1

What is Instance Creator? Why and when do we require this?

In most of the cases, Gson library is smart enough to create instances even if any class does not provide default no-args constructor. But, if you found any problem using a class having no no-args constructor, you can use InstanceCreator support. You need to register the InstanceCreator of a java class type with Gson first before using it.

For example, Department.java does not have any default constructor.
public class Department
{
   public Department(String deptName)
   {
      this.deptName = deptName;
   }
 
   private String deptName;
 
   public String getDeptName()
   {
      return deptName;
   }
 
   public void setDeptName(String deptName)
   {
      this.deptName = deptName;
   }
    
   @Override
   public String toString()
   {
      return "Department [deptName="+deptName+"]";
   }
}

And our Employee class has reference of Department as:
public class Employee
{
   private Integer id;
   private String firstName;
   private String lastName;
   private List<String> roles;
   private Department department; //Department reference
    
   //Other setters and getters
}

To use Department class correctly, you need to register an InstanceCreator for Department.java as below:
class DepartmentInstanceCreator implements InstanceCreator<Department> {
   public Department createInstance(Type type)
   {
      return new Department("None");
   }
}
 
//Now use the above InstanceCreator as below
 
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Department.class, new DepartmentInstanceCreator());
Gson gson = gsonBuilder.create();
 
System.out.println(
            gson.fromJson("{'id':1,'firstName':'Arindam','lastName':'Ghosh','roles':['FINANCE','MANAGER'],'department':{'deptName':'Finance'}}", 
            Employee.class));
             
Output:
Employee [id=1, firstName=Arindam, lastName=Ghosh, roles=[FINANCE, MANAGER], department=Department [deptName=Finance]]
復習用に保存

復習用に保存

この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。

マイ学習ライブラリを開く
役に立ちましたか?
コメントを追加 コメントを見る
質問 2

How to custom Serialization and De-serialization in Gson?

Many times, we need to write/read the JSON values which are not default representation of java object. In that case, we need to write custom serializer and deserializer of that java type.

In our example, I am writing serializer and deserializer for java.util.Date class, which will help writing the Date format in "dd/MM/yyyy" format.

DateSerializer.java
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
 
public class DateSerializer implements JsonSerializer<Date>
{
   private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   public JsonElement serialize(Date date, Type typeOfSrc, JsonSerializationContext context)
   {
      return new JsonPrimitive(dateFormat.format(date));
   }
}

DateDeserializer.java
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
 
public class DateDeserializer implements JsonDeserializer<Date>
{
   private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   public Date deserialize(JsonElement dateStr, Type typeOfSrc, JsonDeserializationContext context)
   {
      try
      {
         return dateFormat.parse(dateStr.getAsString());
      } 
      catch (ParseException e)
      {
         e.printStackTrace();
      }
      return null;
   }
}

Now you can register these serializer and deserializer with GsonBuilder as below:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());

Complete example of serializer and deserializer is as below:
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("Arindam");
employee.setLastName("Ghosh");
employee.setRoles(Arrays.asList("FINANCE", "MANAGER"));
employee.setBirthDate(new Date());
 
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();
 
//Convert to JSON
System.out.println(gson.toJson(employee));
 
//Convert to java objects
System.out.println(gson.fromJson("{'id':1,'firstName':'Arindam','lastName':'Ghosh','roles':['FINANCE','MANAGER'],'birthDate':'17/06/2014'}", Employee.class));
                             
Output:

{

    "id":1,

    "firstName":"Bob",

    "lastName":"Richardson",

    "roles":["FINANCE","MANAGER"],

    "birthDate":"17/06/2014"

}

Employee [id=1, firstName=Arindam, lastName=Ghosh, roles=[FINANCE, MANAGER], birthDate=Tue Jun 17 00:00:00 IST 2014]
復習用に保存

復習用に保存

この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。

マイ学習ライブラリを開く
役に立ちましたか?
コメントを追加 コメントを見る
質問 3

How to provide Pretty Printing for JSON Output Format?

The default JSON output that is provide by Gson is a compact JSON format. This means that there will not be any white-space in the output JSON structure. To generate a more readable and pretty looking JSON use setPrettyPrinting() in GsonBuilder.

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(employee);
 
Output:

{
  "id": 1,
  "firstName": "Lokesh",
  "lastName": "Gupta",
  "roles": [
    "ADMIN",
    "MANAGER"
  ],
  "birthDate": "17/06/2014"
}
復習用に保存

復習用に保存

この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。

マイ学習ライブラリを開く
役に立ちましたか?
コメントを追加 コメントを見る
質問 4

What is Versioning Support in Gson?

This is excellent feature you can use, if the class file you are working has been modified in different versions and fields has been annotated with @Since. All you need to do is to use setVersion() method of GsonBuilder.

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
//Specify the version like this
gsonBuilder.setVersion(1.0);
Gson gson = gsonBuilder.create();

Fields added in various versions in Employee.java
public class Employee
{
   @Since(1.0)
   private Integer id;
   private String firstName;
   private String lastName;
    
   @Since(1.1)
   private List<String> roles;
    
   @Since(1.2)
   private Date birthDate;
    
   //Setters and Getters
}

Now test the version feature:
//Using version 1.0 fields
gsonBuilder.setVersion(1.0);
 
Output:

{

    "id":1,

    "firstName":"Bob",

    "lastName":"Richardson"

}

  
//Using version 1.1 fields
gsonBuilder.setVersion(1.1);
 
Output:

{

    "id":1,

    "firstName":"Bob",

    "lastName":"Richardson",

    "roles":["FINANCE","MANAGER"]

}

  
//Using version 1.2 fields
gsonBuilder.setVersion(1.2);
 
Output:

{

    "id":1,

    "firstName":"Bob",

    "lastName":"Richardson",

    "roles":["FINANCE","MANAGER"],

    "birthDate":"17/06/2014"

}

復習用に保存

復習用に保存

この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。

マイ学習ライブラリを開く
役に立ちましたか?
コメントを追加 コメントを見る

ユーザー評価で最も役立つ内容:

著作権 © 2026、WithoutBook。