Serialization and deserialization of Gson

Keywords: JSON Java Gradle Google

serialization and deserialization

Java serialization refers to the process of converting Java objects into transportable byte sequences, while Java deserialization refers to the process of restoring transmitted byte sequences to Java objects. These two processes make it very convenient for us to store and transfer data.

Overview of Gson

Gson is a Java library that can be used to convert Java objects to JSON representations, and it can also be used to convert JSON strings to equivalent Java objects. The main class used is gson. You can create new Gson() by calling. The gson instance does not maintain any state when calling the JSON operation. Therefore, you can freely reuse the same object to perform multiple JSON serialization and deserialization operations.

Use of Gson

AS Gradle Guide Package:

dependencies {
    compile 'com.google.code.gson:gson:2.8.2'
}

Gson confusion rule:

# When using Gson, you need to configure the parsing objects and variables of Gson without confusion. Otherwise, Gson will not find the variable.
-keep class com.example.bean.** { *; }

Basic use of Gson

// serialize
Gson gson = new Gson();
gson.toJson(1);             // ==> 1
gson.toJson("json");        // ==> "json"
gson.toJson(new Long(123)); // ==> 123
int[] values = { 1 };
gson.toJson(values);       // ==> [1]

// Deserialization
int one = gson.fromJson("1", int.class);
Integer one = gson.fromJson("1", Integer.class);
Long one = gson.fromJson("1", Long.class);
Boolean false = gson.fromJson("false", Boolean.class);
String str = gson.fromJson("\"abc\"", String.class);
String[] anotherStr = gson.fromJson("[\"abc\"]", String[].class);

Serialization and deserialization of objects

class UserInfo {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3; 

  UserInfo() {

  }
}

//serialize
UserInfo user = new UserInfo();
String json = new Gson().toJson(user);  

// ==> json is {"value1":1,"value2":"abc"}

//Deserialization
UserInfo user = new Gson().fromJson(json, UserInfo.class);
Note: when the object is serialized (write byte sequence to target file), transient prevents the variables declared with this keyword from being persisted in the instance; when the object is deserialized (read byte sequence from source file for reconstruction), such instance variable values will not be persisted and recovered. >Array serialization and deserialization
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};

// serialize
gson.toJson(ints);     // ==> [1,2,3,4,5]
gson.toJson(strings);  // ==> ["abc", "def", "ghi"]

// Deserialization
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); 
// ==> ints2 will be same as ints

Serialization and deserialization of collections

Gson gson = new Gson();
List list = new ArryList();
list.add(1);
list.add(2);
list.add(3);

// serialize
String json = gson.toJson(ints);  // ==> json is [1,2,3]

// Deserialization
List<String> name = new Gson().fromJson(json, new TypeToken<List<String>>() {}.getType());
//The TypeToken class is needed here

These are the common Gson transformations. For other requirements, such as custom serializer and deserializer, you can use the class GsonBuilder to create various settings, such as version control and other Gson instances, Personal blog Used by Gson. Welcome to study together.

Posted by Buglish on Mon, 04 May 2020 14:43:19 -0700