GSON - 串行化和反串行化通用类型

x33g5p2x  于2022-10-07 转载在 其他  
字(2.6k)|赞(0)|评价(0)|浏览(723)

在这篇快速的文章中,我们将看到如何使用GSON来序列化和反序列化一个通用类。由于Java Type Erasure的存在,泛型类型信息在序列化过程中会丢失。你可以通过为你的通用类型指定正确的参数化类型来解决这个问题。Gson提供了TypeToken类。

GSON的Maven依赖性

要在Maven2/3中使用Gson,你可以通过添加以下依赖项来使用Maven Central中的Gson版本。

  1. <dependencies>
  2. <!-- Gson: Java to Json conversion -->
  3. <dependency>
  4. <groupId>com.google.code.gson</groupId>
  5. <artifactId>gson</artifactId>
  6. <version>2.8.5</version>
  7. <scope>compile</scope>
  8. </dependency>
  9. </dependencies>

让我们创建Box通用类,我们将在接下来的例子中使用它。

  1. class Box < T > {
  2. private T t;
  3. public void set(T t) {
  4. this.t = t;
  5. }
  6. public T get() {
  7. return t;
  8. }
  9. }

让我们创建一个User POJO类。

  1. package net.javaguides.gson;
  2. public class User {
  3. private int id;
  4. private String firstName;
  5. private String lastName;
  6. private int age;
  7. private String gender;
  8. private String password;
  9. public int getId() {
  10. return id;
  11. }
  12. public void setId(int i) {
  13. this.id = i;
  14. }
  15. public String getFirstName() {
  16. return firstName;
  17. }
  18. public void setFirstName(String firstName) {
  19. this.firstName = firstName;
  20. }
  21. public String getLastName() {
  22. return lastName;
  23. }
  24. public void setLastName(String lastName) {
  25. this.lastName = lastName;
  26. }
  27. public int getAge() {
  28. return age;
  29. }
  30. public void setAge(int age) {
  31. this.age = age;
  32. }
  33. public String getGender() {
  34. return gender;
  35. }
  36. public void setGender(String gender) {
  37. this.gender = gender;
  38. }
  39. public String getPassword() {
  40. return password;
  41. }
  42. public void setPassword(String password) {
  43. this.password = password;
  44. }
  45. @Override
  46. public String toString() {
  47. return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", gender=" +
  48. gender + "]";
  49. }
  50. }

序列化和反序列化通用类型的例子

  1. package net.javaguides.gson;
  2. import java.lang.reflect.Type;
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.reflect.TypeToken;
  6. public class GSONGenericsExample {
  7. public static void main(String[] args) {
  8. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  9. Box < User > type2 = new Box < > ();
  10. User user = new User();
  11. user.setId(100);
  12. user.setFirstName("Ramesh");
  13. user.setLastName("Fadatare");
  14. user.setGender("Male");
  15. user.setAge(28);
  16. type2.set(user);
  17. // Serialization of generic User type to json
  18. Type fooType2 = new TypeToken < Box < User >> () {}.getType();
  19. String userJson = gson.toJson(type2, fooType2);
  20. System.out.println(userJson);
  21. // De-serialization of generic User type to json
  22. Box < User > box = gson.fromJson(userJson, fooType2);
  23. System.out.println(box.get().toString());
  24. }
  25. }
  26. class Box < T > {
  27. private T t;
  28. public void set(T t) {
  29. this.t = t;
  30. }
  31. public T get() {
  32. return t;
  33. }
  34. }

输出。

  1. {
  2. "t": {
  3. "id": 100,
  4. "firstName": "Ramesh",
  5. "lastName": "Fadatare",
  6. "age": 28,
  7. "gender": "Male"
  8. }
  9. }
  10. User [id=100, firstName=Ramesh, lastName=Fadatare, age=28, gender=Male]

相关文章